Merge branch 'eugenp:master' into master
This commit is contained in:
+10
-7
@@ -64,13 +64,13 @@ core-java-io/hard_link.txt
|
||||
core-java-io/target_link.txt
|
||||
core-java/src/main/java/com/baeldung/manifest/MANIFEST.MF
|
||||
ethereum/logs/
|
||||
jmeter/src/main/resources/*-JMeter.csv
|
||||
jmeter/src/main/resources/*-Basic*.csv
|
||||
jmeter/src/main/resources/*-JMeter*.csv
|
||||
jmeter/src/main/resources/*ReportsDashboard*.csv
|
||||
jmeter/src/main/resources/dashboard/*ReportsDashboard*.csv
|
||||
jmeter/src/main/resources/*FileExtractionExample.csv
|
||||
jmeter/src/main/resources/*_Summary-Report.csv
|
||||
testing-modules/jmeter/src/main/resources/*-JMeter.csv
|
||||
testing-modules/jmeter/src/main/resources/*-Basic*.csv
|
||||
testing-modules/jmeter/src/main/resources/*-JMeter*.csv
|
||||
testing-modules/jmeter/src/main/resources/*ReportsDashboard*.csv
|
||||
testing-modules/jmeter/src/main/resources/dashboard/*ReportsDashboard*.csv
|
||||
testing-modules/jmeter/src/main/resources/*FileExtractionExample.csv
|
||||
testing-modules/jmeter/src/main/resources/*_Summary-Report.csv
|
||||
|
||||
ninja/devDb.mv.db
|
||||
|
||||
@@ -128,3 +128,6 @@ persistence-modules/neo4j/data/**
|
||||
/deep-shallow-copy/.mvn/wrapper
|
||||
/deep-shallow-copy/mvnw
|
||||
/deep-shallow-copy/mvnw.cmd
|
||||
|
||||
#spring-5-webflux-2
|
||||
**/testdb.mv.db
|
||||
@@ -14,16 +14,6 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
|
||||
-2
@@ -1,7 +1,5 @@
|
||||
package com.baeldung.algorithms;
|
||||
|
||||
|
||||
|
||||
import com.baeldung.algorithms.ga.dijkstra.Dijkstra;
|
||||
import com.baeldung.algorithms.ga.dijkstra.Graph;
|
||||
import com.baeldung.algorithms.ga.dijkstra.Node;
|
||||
|
||||
@@ -5,4 +5,5 @@
|
||||
- [Getting Pixel Array From Image in Java](https://www.baeldung.com/java-getting-pixel-array-from-image)
|
||||
- [Calculate Distance Between Two Coordinates in Java](https://www.baeldung.com/java-find-distance-between-points)
|
||||
- [Rotate Arrays in Java](https://www.baeldung.com/java-rotate-arrays)
|
||||
- [Find Missing Number From a Given Array in Java](https://www.baeldung.com/java-array-find-missing-number)
|
||||
- More articles: [[<-- prev]](/algorithms-miscellaneous-6)
|
||||
|
||||
+153
@@ -0,0 +1,153 @@
|
||||
package com.baeldung.algorithms.weightedaverage;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.BinaryOperator;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collector;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class WeightedAverageUnitTest {
|
||||
|
||||
private List<Values> values = Arrays.asList(
|
||||
new Values(1, 10),
|
||||
new Values(3, 20),
|
||||
new Values(5, 30),
|
||||
new Values(7, 50),
|
||||
new Values(9, 40)
|
||||
);
|
||||
|
||||
private Double expected = 6.2;
|
||||
|
||||
@Test
|
||||
void twoPass() {
|
||||
double top = values.stream()
|
||||
.mapToDouble(v -> v.value * v.weight)
|
||||
.sum();
|
||||
double bottom = values.stream()
|
||||
.mapToDouble(v -> v.weight)
|
||||
.sum();
|
||||
|
||||
double result = top / bottom;
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void onePass() {
|
||||
double top = 0;
|
||||
double bottom = 0;
|
||||
|
||||
for (Values v : values) {
|
||||
top += (v.value * v.weight);
|
||||
bottom += v.weight;
|
||||
}
|
||||
|
||||
double result = top / bottom;
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void expanding() {
|
||||
double result = values.stream()
|
||||
.flatMap(v -> Collections.nCopies(v.weight, v.value).stream())
|
||||
.mapToInt(v -> v)
|
||||
.average()
|
||||
.getAsDouble();
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void reduce() {
|
||||
class WeightedAverage {
|
||||
final double top;
|
||||
final double bottom;
|
||||
|
||||
public WeightedAverage(double top, double bottom) {
|
||||
this.top = top;
|
||||
this.bottom = bottom;
|
||||
}
|
||||
|
||||
double average() {
|
||||
return top / bottom;
|
||||
}
|
||||
}
|
||||
|
||||
double result = values.stream()
|
||||
.reduce(new WeightedAverage(0, 0),
|
||||
(acc, next) -> new WeightedAverage(
|
||||
acc.top + (next.value * next.weight),
|
||||
acc.bottom + next.weight),
|
||||
(left, right) -> new WeightedAverage(
|
||||
left.top + right.top,
|
||||
left.bottom + right.bottom))
|
||||
.average();
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void customCollector() {
|
||||
class WeightedAverage implements Collector<Values, WeightedAverage.RunningTotals, Double> {
|
||||
class RunningTotals {
|
||||
double top;
|
||||
double bottom;
|
||||
|
||||
public RunningTotals() {
|
||||
this.top = 0;
|
||||
this.bottom = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<RunningTotals> supplier() {
|
||||
return RunningTotals::new;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BiConsumer<RunningTotals, Values> accumulator() {
|
||||
return (current, next) -> {
|
||||
current.top += (next.value * next.weight);
|
||||
current.bottom += next.weight;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryOperator<RunningTotals> combiner() {
|
||||
return (left, right) -> {
|
||||
left.top += right.top;
|
||||
left.bottom += right.bottom;
|
||||
|
||||
return left;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Function<RunningTotals, Double> finisher() {
|
||||
return rt -> rt.top / rt.bottom;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Characteristics> characteristics() {
|
||||
return Collections.singleton(Characteristics.UNORDERED);
|
||||
}
|
||||
}
|
||||
|
||||
double result = values.stream()
|
||||
.collect(new WeightedAverage());
|
||||
assertEquals(expected, result);
|
||||
}
|
||||
|
||||
private static class Values {
|
||||
int value;
|
||||
int weight;
|
||||
|
||||
public Values(int value, int weight) {
|
||||
this.value = value;
|
||||
this.weight = weight;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,4 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<cxf.version>4.0.0</cxf.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -48,7 +48,6 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<cxf.version>4.0.0</cxf.version>
|
||||
<jakarta-xml.version>4.0.0</jakarta-xml.version>
|
||||
<jakarta.jws.version>3.0.0</jakarta.jws.version>
|
||||
</properties>
|
||||
|
||||
@@ -16,12 +16,12 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
|
||||
<version>4.0.0</version>
|
||||
<version>${cxf.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-transports-http-jetty</artifactId>
|
||||
<version>4.0.0</version>
|
||||
<version>${cxf.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.xml.ws</groupId>
|
||||
|
||||
@@ -115,6 +115,7 @@
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<cxf.version>3.1.8</cxf.version>
|
||||
<spring.version>5.3.25</spring.version>
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
<jstl.version>1.2</jstl.version>
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<cxf.version>3.1.8</cxf.version>
|
||||
<cxf.version>4.0.0</cxf.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -16,12 +16,12 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-rs-client</artifactId>
|
||||
<version>${cxf-version}</version>
|
||||
<version>${cxf.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-rs-sse</artifactId>
|
||||
<version>${cxf-version}</version>
|
||||
<version>${cxf.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.ws.rs</groupId>
|
||||
@@ -60,7 +60,6 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<cxf-version>4.0.0</cxf-version>
|
||||
<jakarta-ws.version>3.1.0</jakarta-ws.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -15,3 +15,4 @@ You can build the project from the command line using: *mvn clean install*, or i
|
||||
- [bootstrap-server in Kafka Configuration](https://www.baeldung.com/java-kafka-bootstrap-server)
|
||||
- [Introduction to Apache Kafka](https://www.baeldung.com/apache-kafka)
|
||||
- [Ensuring Message Ordering in Kafka: Strategies and Configurations](https://www.baeldung.com/kafka-message-ordering)
|
||||
- [Read Multiple Messages with Apache Kafka](https://www.baeldung.com/kafka-read-multiple-messages)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
## Relevant Articles
|
||||
- [How To Convert Excel Data Into List Of Java Objects](https://www.baeldung.com/java-convert-excel-data-into-list)
|
||||
- [Expand Columns with Apache POI](https://www.baeldung.com/java-apache-poi-expand-columns)
|
||||
- [Apply Bold Text Style for an Entire Row Using Apache POI](https://www.baeldung.com/appache-poi-apply-bold-text-style-entire-row)
|
||||
|
||||
@@ -76,7 +76,7 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<poi.version>5.2.3</poi.version>
|
||||
<poi.version>5.2.5</poi.version>
|
||||
<poiji.version>4.1.1</poiji.version>
|
||||
<fastexcel.version>0.15.7</fastexcel.version>
|
||||
<jxl.version>2.6.12</jxl.version>
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.baeldung.poi.rowstyle;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.Font;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
|
||||
public class PoiUtils {
|
||||
|
||||
private PoiUtils() {
|
||||
}
|
||||
|
||||
private static void newCell(Row row, String value) {
|
||||
short cellNum = row.getLastCellNum();
|
||||
if (cellNum == -1)
|
||||
cellNum = 0;
|
||||
|
||||
Cell cell = row.createCell(cellNum);
|
||||
cell.setCellValue(value);
|
||||
}
|
||||
|
||||
public static Row newRow(Sheet sheet, String... rowValues) {
|
||||
Row row = sheet.createRow(sheet.getLastRowNum() + 1);
|
||||
|
||||
for (String value : rowValues) {
|
||||
newCell(row, value);
|
||||
}
|
||||
|
||||
return row;
|
||||
}
|
||||
|
||||
public static CellStyle boldFontStyle(Workbook workbook) {
|
||||
Font boldFont = workbook.createFont();
|
||||
boldFont.setBold(true);
|
||||
|
||||
CellStyle boldStyle = workbook.createCellStyle();
|
||||
boldStyle.setFont(boldFont);
|
||||
|
||||
return boldStyle;
|
||||
}
|
||||
|
||||
public static void write(Workbook workbook, Path path) throws IOException {
|
||||
try (FileOutputStream fileOut = new FileOutputStream(path.toFile())) {
|
||||
workbook.write(fileOut);
|
||||
}
|
||||
}
|
||||
}
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
package com.baeldung.poi.rowstyle;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
||||
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PoiBoldStyleIntegrationTest {
|
||||
|
||||
private void writeSampleSheet(Path destination, Workbook workbook) throws IOException {
|
||||
Sheet sheet = workbook.createSheet();
|
||||
CellStyle boldStyle = PoiUtils.boldFontStyle(workbook);
|
||||
|
||||
Row header = PoiUtils.newRow(sheet, "Name", "Value", "Details");
|
||||
header.setRowStyle(boldStyle);
|
||||
|
||||
PoiUtils.newRow(sheet, "Albert", "A", "First");
|
||||
PoiUtils.newRow(sheet, "Jane", "B", "Second");
|
||||
PoiUtils.newRow(sheet, "Zack", "C", "Third");
|
||||
|
||||
PoiUtils.write(workbook, destination);
|
||||
}
|
||||
|
||||
private void assertRowStyleAppliedAndDefaultCellStylesDontMatch(Path sheetFile) throws IOException, InvalidFormatException {
|
||||
try (Workbook workbook = new XSSFWorkbook(sheetFile.toFile())) {
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
Row row0 = sheet.getRow(0);
|
||||
|
||||
XSSFCellStyle rowStyle = (XSSFCellStyle) row0.getRowStyle();
|
||||
assertTrue(rowStyle.getFont()
|
||||
.getBold());
|
||||
|
||||
row0.forEach(cell -> {
|
||||
XSSFCellStyle style = (XSSFCellStyle) cell.getCellStyle();
|
||||
assertNotEquals(rowStyle, style);
|
||||
});
|
||||
|
||||
Row row1 = sheet.getRow(1);
|
||||
XSSFCellStyle row1Style = (XSSFCellStyle) row1.getRowStyle();
|
||||
assertNull(row1Style);
|
||||
|
||||
Files.delete(sheetFile);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenXssfWorkbook_whenSetRowStyle1stRow_thenOnly1stRowStyled() throws IOException, InvalidFormatException {
|
||||
Path sheetFile = Files.createTempFile("xssf-row-style", ".xlsx");
|
||||
|
||||
try (Workbook workbook = new XSSFWorkbook()) {
|
||||
writeSampleSheet(sheetFile, workbook);
|
||||
}
|
||||
|
||||
assertRowStyleAppliedAndDefaultCellStylesDontMatch(sheetFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSxssfWorkbook_whenSetRowStyle_thenOnly1stRowStyled() throws IOException, InvalidFormatException {
|
||||
Path sheetFile = Files.createTempFile("sxssf-row-style", ".xlsx");
|
||||
|
||||
try (Workbook workbook = new SXSSFWorkbook()) {
|
||||
writeSampleSheet(sheetFile, workbook);
|
||||
}
|
||||
|
||||
assertRowStyleAppliedAndDefaultCellStylesDontMatch(sheetFile);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenHssfWorkbook_whenSetRowStyle_thenOnly1stRowStyled() throws IOException {
|
||||
Path sheetFile = Files.createTempFile("hssf-row-style", ".xls");
|
||||
|
||||
try (Workbook workbook = new HSSFWorkbook()) {
|
||||
writeSampleSheet(sheetFile, workbook);
|
||||
}
|
||||
|
||||
try (Workbook workbook = new HSSFWorkbook(Files.newInputStream(sheetFile))) {
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
Row row0 = sheet.getRow(0);
|
||||
|
||||
HSSFCellStyle rowStyle = (HSSFCellStyle) row0.getRowStyle();
|
||||
assertTrue(rowStyle.getFont(workbook)
|
||||
.getBold());
|
||||
|
||||
row0.forEach(cell -> {
|
||||
HSSFCellStyle style = (HSSFCellStyle) cell.getCellStyle();
|
||||
assertNotEquals(rowStyle, style);
|
||||
});
|
||||
|
||||
Row row1 = sheet.getRow(1);
|
||||
HSSFCellStyle row1Style = (HSSFCellStyle) row1.getRowStyle();
|
||||
assertNull(row1Style);
|
||||
|
||||
Files.delete(sheetFile);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenXssfWorkbook_whenSetCellStyleForEachRow_thenAllCellsContainStyle() throws IOException, InvalidFormatException {
|
||||
Path sheetFile = Files.createTempFile("xssf-cell-style", ".xlsx");
|
||||
|
||||
try (Workbook workbook = new XSSFWorkbook()) {
|
||||
Sheet sheet = workbook.createSheet();
|
||||
CellStyle boldStyle = PoiUtils.boldFontStyle(workbook);
|
||||
|
||||
Row header = PoiUtils.newRow(sheet, "Name", "Value", "Details");
|
||||
header.forEach(cell -> cell.setCellStyle(boldStyle));
|
||||
|
||||
PoiUtils.newRow(sheet, "Albert", "A", "First");
|
||||
PoiUtils.newRow(sheet, "Jane", "B", "Second");
|
||||
PoiUtils.newRow(sheet, "Zack", "C", "Third");
|
||||
|
||||
PoiUtils.write(workbook, sheetFile);
|
||||
}
|
||||
|
||||
try (Workbook workbook = new XSSFWorkbook(sheetFile.toFile())) {
|
||||
Sheet sheet = workbook.getSheetAt(0);
|
||||
Row row0 = sheet.getRow(0);
|
||||
|
||||
XSSFCellStyle rowStyle = (XSSFCellStyle) row0.getRowStyle();
|
||||
assertNull(rowStyle);
|
||||
|
||||
row0.forEach(cell -> {
|
||||
XSSFCellStyle style = (XSSFCellStyle) cell.getCellStyle();
|
||||
assertTrue(style.getFont()
|
||||
.getBold());
|
||||
});
|
||||
|
||||
Row row1 = sheet.getRow(1);
|
||||
rowStyle = (XSSFCellStyle) row1.getRowStyle();
|
||||
assertNull(rowStyle);
|
||||
|
||||
Files.delete(sheetFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,7 +97,7 @@
|
||||
<json-simple.version>1.1.1</json-simple.version>
|
||||
<aws-lambda-java-events.version>3.11.0</aws-lambda-java-events.version>
|
||||
<aws-lambda-java-core.version>1.2.1</aws-lambda-java-core.version>
|
||||
<gson.version>2.8.2</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -46,7 +46,7 @@
|
||||
<maven.compiler.source.version>11</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>11</maven.compiler.target.version>
|
||||
<jackson.version>2.16.0</jackson.version>
|
||||
<gson.version>2.10</gson.version>
|
||||
<gson.version>2.10.1</gson.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -3,3 +3,4 @@
|
||||
- [String Templates in Java 21](https://www.baeldung.com/java-21-string-templates)
|
||||
- [Unnamed Classes and Instance Main Methods in Java 21](https://www.baeldung.com/java-21-unnamed-class-instance-main)
|
||||
- [Unnamed Patterns and Variables in Java 21](https://www.baeldung.com/java-unnamed-patterns-variables)
|
||||
- [JFR View Command in Java 21](https://www.baeldung.com/java-flight-recorder-view)
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.jfrview;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class JFRExample {
|
||||
public static void main(String[] args) {
|
||||
JFRExample se = new JFRExample();
|
||||
se.insertToList(new ArrayList<>());
|
||||
}
|
||||
|
||||
private void insertToList(List<Object> list) {
|
||||
try {
|
||||
while (true) {
|
||||
list.add(new Object());
|
||||
}
|
||||
} catch (OutOfMemoryError e) {
|
||||
System.out.println("Out of Memory. Exiting");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,4 +9,8 @@
|
||||
- [Round the Date in Java](https://www.baeldung.com/java-round-the-date)
|
||||
- [Representing Furthest Possible Date in Java](https://www.baeldung.com/java-date-represent-max)
|
||||
- [Retrieving Unix Time in Java](https://www.baeldung.com/java-retrieve-unix-time)
|
||||
- [Calculate Months Between Two Dates in Java](https://www.baeldung.com/java-months-difference-two-dates)
|
||||
- [Format LocalDate to ISO 8601 With T and Z](https://www.baeldung.com/java-format-localdate-iso-8601-t-z)
|
||||
- [Check if Two Date Ranges Overlap](https://www.baeldung.com/java-check-two-date-ranges-overlap)
|
||||
- [Difference between ZoneOffset.UTC and ZoneId.of(“UTC”)](https://www.baeldung.com/java-zoneoffset-utc-zoneid-of)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-datetime-java8-1)
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.daterangeoverlap;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.Interval;
|
||||
|
||||
public class DateRangeOverlapChecker {
|
||||
|
||||
public static boolean isOverlapUsingCalendarAndDuration(Calendar start1, Calendar end1, Calendar start2, Calendar end2) {
|
||||
long overlap = Math.min(end1.getTimeInMillis(), end2.getTimeInMillis()) - Math.max(start1.getTimeInMillis(), start2.getTimeInMillis());
|
||||
return overlap >= 0;
|
||||
}
|
||||
|
||||
public static boolean isOverlapUsingLocalDateAndDuration(LocalDate start1, LocalDate end1, LocalDate start2, LocalDate end2) {
|
||||
long overlap = Math.min(end1.toEpochDay(), end2.toEpochDay()) - Math.max(start1.toEpochDay(), start2.toEpochDay());
|
||||
return overlap >= 0;
|
||||
}
|
||||
|
||||
public static boolean isOverlapUsingJodaTime(DateTime start1, DateTime end1, DateTime start2, DateTime end2) {
|
||||
Interval interval1 = new Interval(start1, end1);
|
||||
Interval interval2 = new Interval(start2, end2);
|
||||
return interval1.overlaps(interval2);
|
||||
}
|
||||
|
||||
public static boolean isOverlapUsingCalendarAndCondition(Calendar start1, Calendar end1, Calendar start2, Calendar end2) {
|
||||
return !(end1.before(start2) || start1.after(end2));
|
||||
}
|
||||
|
||||
public static boolean isOverlapUsingLocalDateAndCondition(LocalDate start1, LocalDate end1, LocalDate start2, LocalDate end2) {
|
||||
return !(end1.isBefore(start2) || start1.isAfter(end2));
|
||||
}
|
||||
|
||||
public static boolean isOverlapUsingCalendarAndFindMin(Calendar start1, Calendar end1, Calendar start2, Calendar end2) {
|
||||
long overlap1 = Math.min(end1.getTimeInMillis() - start1.getTimeInMillis(), end1.getTimeInMillis() - start2.getTimeInMillis());
|
||||
long overlap2 = Math.min(end2.getTimeInMillis() - start2.getTimeInMillis(), end2.getTimeInMillis() - start1.getTimeInMillis());
|
||||
return Math.min(overlap1, overlap2) / (24 * 60 * 60 * 1000) >= 0;
|
||||
}
|
||||
|
||||
public static boolean isOverlapUsingLocalDateAndFindMin(LocalDate start1, LocalDate end1, LocalDate start2, LocalDate end2) {
|
||||
long overlap1 = Math.min(end1.toEpochDay() - start1.toEpochDay(), end1.toEpochDay() - start2.toEpochDay());
|
||||
long overlap2 = Math.min(end2.toEpochDay() - start2.toEpochDay(), end2.toEpochDay() - start1.toEpochDay());
|
||||
return Math.min(overlap1, overlap2) >= 0;
|
||||
}
|
||||
}
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
package com.baeldung.daterangeoverlap;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DateRangeOverlapCheckerUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenPartialOverlappingRanges_thenReturnsTrue() {
|
||||
Calendar start1 = Calendar.getInstance();
|
||||
start1.set(2024, 11, 15);
|
||||
Calendar end1 = Calendar.getInstance();
|
||||
end1.set(2024, 11, 20);
|
||||
|
||||
Calendar start2 = Calendar.getInstance();
|
||||
start2.set(2024, 11, 18);
|
||||
Calendar end2 = Calendar.getInstance();
|
||||
end2.set(2024, 11, 22);
|
||||
|
||||
LocalDate startLD1 = LocalDate.of(2024, 12, 15);
|
||||
LocalDate endLD1 = LocalDate.of(2024, 12, 20);
|
||||
|
||||
LocalDate startLD2 = LocalDate.of(2024, 12, 18);
|
||||
LocalDate endLD2 = LocalDate.of(2024, 12, 22);
|
||||
|
||||
DateTime startJT1 = new DateTime(2024, 12, 15, 0, 0);
|
||||
DateTime endJT1 = new DateTime(2024, 12, 20, 0, 0);
|
||||
|
||||
DateTime startJT2 = new DateTime(2024, 12, 18, 0, 0);
|
||||
DateTime endJT2 = new DateTime(2024, 12, 22, 0, 0);
|
||||
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingCalendarAndDuration(start1, end1, start2, end2));
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingLocalDateAndDuration(startLD1, endLD1, startLD2, endLD2));
|
||||
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingCalendarAndCondition(start1, end1, start2, end2));
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingLocalDateAndCondition(startLD1, endLD1, startLD2, endLD2));
|
||||
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingCalendarAndFindMin(start1, end1, start2, end2));
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingLocalDateAndFindMin(startLD1, endLD1, startLD2, endLD2));
|
||||
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingJodaTime(startJT1, endJT1, startJT2, endJT2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFullOverlappingRanges_thenReturnsTrue() {
|
||||
Calendar start1 = Calendar.getInstance();
|
||||
start1.set(2024, 11, 15);
|
||||
Calendar end1 = Calendar.getInstance();
|
||||
end1.set(2024, 11, 20);
|
||||
|
||||
Calendar start2 = Calendar.getInstance();
|
||||
start2.set(2024, 11, 16);
|
||||
Calendar end2 = Calendar.getInstance();
|
||||
end2.set(2024, 11, 18);
|
||||
|
||||
LocalDate startLD1 = LocalDate.of(2024, 12, 15);
|
||||
LocalDate endLD1 = LocalDate.of(2024, 12, 20);
|
||||
|
||||
LocalDate startLD2 = LocalDate.of(2024, 12, 16);
|
||||
LocalDate endLD2 = LocalDate.of(2024, 12, 18);
|
||||
|
||||
DateTime startJT1 = new DateTime(2024, 12, 15, 0, 0);
|
||||
DateTime endJT1 = new DateTime(2024, 12, 20, 0, 0);
|
||||
|
||||
DateTime startJT2 = new DateTime(2024, 12, 16, 0, 0);
|
||||
DateTime endJT2 = new DateTime(2024, 12, 18, 0, 0);
|
||||
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingCalendarAndDuration(start1, end1, start2, end2));
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingLocalDateAndDuration(startLD1, endLD1, startLD2, endLD2));
|
||||
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingCalendarAndCondition(start1, end1, start2, end2));
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingLocalDateAndCondition(startLD1, endLD1, startLD2, endLD2));
|
||||
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingCalendarAndFindMin(start1, end1, start2, end2));
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingLocalDateAndFindMin(startLD1, endLD1, startLD2, endLD2));
|
||||
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingJodaTime(startJT1, endJT1, startJT2, endJT2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenConsecutiveRanges_thenReturnsFalse() {
|
||||
Calendar start1 = Calendar.getInstance();
|
||||
start1.set(2024, 11, 15);
|
||||
Calendar end1 = Calendar.getInstance();
|
||||
end1.set(2024, 11, 20);
|
||||
|
||||
Calendar start2 = Calendar.getInstance();
|
||||
start2.set(2024, 11, 21);
|
||||
Calendar end2 = Calendar.getInstance();
|
||||
end2.set(2024, 11, 24);
|
||||
|
||||
LocalDate startLD1 = LocalDate.of(2024, 12, 15);
|
||||
LocalDate endLD1 = LocalDate.of(2024, 12, 20);
|
||||
|
||||
LocalDate startLD2 = LocalDate.of(2024, 12, 21);
|
||||
LocalDate endLD2 = LocalDate.of(2024, 12, 24);
|
||||
|
||||
DateTime startJT1 = new DateTime(2024, 12, 15, 0, 0);
|
||||
DateTime endJT1 = new DateTime(2024, 12, 20, 0, 0);
|
||||
|
||||
DateTime startJT2 = new DateTime(2024, 12, 21, 0, 0);
|
||||
DateTime endJT2 = new DateTime(2024, 12, 24, 0, 0);
|
||||
|
||||
assertFalse(DateRangeOverlapChecker.isOverlapUsingCalendarAndDuration(start1, end1, start2, end2));
|
||||
assertFalse(DateRangeOverlapChecker.isOverlapUsingLocalDateAndDuration(startLD1, endLD1, startLD2, endLD2));
|
||||
|
||||
assertFalse(DateRangeOverlapChecker.isOverlapUsingCalendarAndCondition(start1, end1, start2, end2));
|
||||
assertFalse(DateRangeOverlapChecker.isOverlapUsingLocalDateAndCondition(startLD1, endLD1, startLD2, endLD2));
|
||||
|
||||
assertFalse(DateRangeOverlapChecker.isOverlapUsingCalendarAndFindMin(start1, end1, start2, end2));
|
||||
assertFalse(DateRangeOverlapChecker.isOverlapUsingLocalDateAndFindMin(startLD1, endLD1, startLD2, endLD2));
|
||||
|
||||
assertFalse(DateRangeOverlapChecker.isOverlapUsingJodaTime(startJT1, endJT1, startJT2, endJT2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenZeroRangeRanges_thenReturnsTrue() {
|
||||
Calendar start1 = Calendar.getInstance();
|
||||
start1.set(2024, 11, 15);
|
||||
Calendar end1 = Calendar.getInstance();
|
||||
end1.set(2024, 11, 20);
|
||||
|
||||
Calendar start2 = Calendar.getInstance();
|
||||
start2.set(2024, 11, 20);
|
||||
Calendar end2 = Calendar.getInstance();
|
||||
end2.set(2024, 11, 20);
|
||||
|
||||
LocalDate startLD1 = LocalDate.of(2024, 12, 15);
|
||||
LocalDate endLD1 = LocalDate.of(2024, 12, 20);
|
||||
|
||||
LocalDate startLD2 = LocalDate.of(2024, 12, 20);
|
||||
LocalDate endLD2 = LocalDate.of(2024, 12, 20);
|
||||
|
||||
DateTime startJT1 = new DateTime(2024, 12, 15, 0, 0);
|
||||
DateTime endJT1 = new DateTime(2024, 12, 20, 0, 0);
|
||||
|
||||
DateTime startJT2 = new DateTime(2024, 12, 20, 0, 0);
|
||||
DateTime endJT2 = new DateTime(2024, 12, 20, 0, 0);
|
||||
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingCalendarAndDuration(start1, end1, start2, end2));
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingLocalDateAndDuration(startLD1, endLD1, startLD2, endLD2));
|
||||
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingCalendarAndCondition(start1, end1, start2, end2));
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingLocalDateAndCondition(startLD1, endLD1, startLD2, endLD2));
|
||||
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingCalendarAndFindMin(start1, end1, start2, end2));
|
||||
assertTrue(DateRangeOverlapChecker.isOverlapUsingLocalDateAndFindMin(startLD1, endLD1, startLD2, endLD2));
|
||||
|
||||
//the overlaps method considers two intervals as overlapping only if they have a non-zero duration.
|
||||
assertFalse(DateRangeOverlapChecker.isOverlapUsingJodaTime(startJT1, endJT1, startJT2, endJT2));
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.zoneoffsetandzoneidof;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ZoneOffSetAndZoneIdOfUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenOffsetDateTimeWithUTCZoneOffset_thenOffsetShouldBeUTC() {
|
||||
OffsetDateTime dateTimeWithOffset = OffsetDateTime.now(ZoneOffset.UTC);
|
||||
assertEquals(dateTimeWithOffset.getOffset(), ZoneOffset.UTC);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenZonedDateTimeWithUTCZoneId_thenZoneShouldBeUTC() {
|
||||
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("UTC"));
|
||||
assertEquals(zonedDateTime.getZone(), ZoneId.of("UTC"));
|
||||
}
|
||||
}
|
||||
@@ -12,3 +12,4 @@ This module contains articles about arrays conversion in Java
|
||||
- [Convert an ArrayList of String to a String Array in Java](https://www.baeldung.com/java-convert-string-arraylist-array)
|
||||
- [Convert Char Array to Int Array in Java](https://www.baeldung.com/java-convert-char-int-array)
|
||||
- [How to Convert Byte Array to Char Array](https://www.baeldung.com/java-convert-byte-array-char)
|
||||
- [Convert byte[] to Byte[] and Vice Versa in Java](https://www.baeldung.com/java-byte-array-wrapper-primitive-type-convert)
|
||||
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.array.conversions;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ByteArrayToPrimitiveByteArrayUnitTest {
|
||||
private static final byte[] EXPECTED_ARRAY_VALUES = {65, 66, 67, 68};
|
||||
private static final Byte[] BYTE_ARRAY = {65, 66, 67, 68};
|
||||
|
||||
@Test
|
||||
public void givenByteArray_whenConvertingUsingByteValue_thenGiveExpectedResult() {
|
||||
byte[] newByteArray = new byte[BYTE_ARRAY.length];
|
||||
for (int i = 0; i < BYTE_ARRAY.length; i++) {
|
||||
newByteArray[i] = BYTE_ARRAY[i].byteValue();
|
||||
}
|
||||
assertThat(newByteArray).containsExactly(EXPECTED_ARRAY_VALUES);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenByteArray_whenConvertingUsingUnboxing_thenGiveExpectedResult() {
|
||||
byte[] newByteArray = new byte[BYTE_ARRAY.length];
|
||||
for (int i = 0; i < BYTE_ARRAY.length; i++) {
|
||||
newByteArray[i] = BYTE_ARRAY[i];
|
||||
}
|
||||
assertThat(newByteArray).containsExactly(EXPECTED_ARRAY_VALUES);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenByteArray_whenConvertingArrayUtils_thenGiveExpectedResult() {
|
||||
byte[] newByteArray = ArrayUtils.toPrimitive(BYTE_ARRAY);
|
||||
|
||||
assertThat(newByteArray).containsExactly(EXPECTED_ARRAY_VALUES);
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.array.conversions;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class PrimitiveByteArrayToByteArrayUnitTest {
|
||||
private static final byte[] PRIMITIVE_BYTE_ARRAY = {65, 66, 67, 68};
|
||||
private static final Byte[] EXPECTED_ARRAY_VALUES = {65, 66, 67, 68};
|
||||
|
||||
@Test
|
||||
public void givenPrimitiveByteArray_whenConvertingUsingByteValueOf_thenGiveExpectedResult() {
|
||||
Byte[] newByteArray = new Byte[PRIMITIVE_BYTE_ARRAY.length];
|
||||
for (int i = 0; i < PRIMITIVE_BYTE_ARRAY.length; i++) {
|
||||
newByteArray[i] = Byte.valueOf(PRIMITIVE_BYTE_ARRAY[i]);
|
||||
}
|
||||
assertThat(newByteArray).containsExactly(EXPECTED_ARRAY_VALUES);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPrimitiveByteArray_whenConvertingUsingAutoboxing_thenGiveExpectedResult() {
|
||||
Byte[] newByteArray = new Byte[PRIMITIVE_BYTE_ARRAY.length];
|
||||
for (int i = 0; i < PRIMITIVE_BYTE_ARRAY.length; i++) {
|
||||
newByteArray[i] = PRIMITIVE_BYTE_ARRAY[i];
|
||||
}
|
||||
assertThat(newByteArray).containsExactly(EXPECTED_ARRAY_VALUES);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPrimitiveByteArray_whenConvertingUsingAutoboxingAndArraysSetAll_thenGiveExpectedResult() {
|
||||
Byte[] newByteArray = new Byte[PRIMITIVE_BYTE_ARRAY.length];
|
||||
Arrays.setAll(newByteArray, n -> PRIMITIVE_BYTE_ARRAY[n]);
|
||||
|
||||
assertThat(newByteArray)
|
||||
.containsExactly(EXPECTED_ARRAY_VALUES);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPrimitiveByteArray_whenConvertingUsingArrayUtils_thenGiveExpectedResult() {
|
||||
Byte[] newByteArray = ArrayUtils.toObject(PRIMITIVE_BYTE_ARRAY);
|
||||
|
||||
assertThat(newByteArray).containsExactly(EXPECTED_ARRAY_VALUES);
|
||||
}
|
||||
}
|
||||
@@ -10,3 +10,4 @@ This module contains complete guides about arrays in Java
|
||||
- [Creating a Generic Array in Java](https://www.baeldung.com/java-generic-array)
|
||||
- [Maximum Size of Java Arrays](https://www.baeldung.com/java-arrays-max-size)
|
||||
- [Merge Two Arrays and Remove Duplicates in Java](https://www.baeldung.com/java-merge-two-arrays-delete-duplicates)
|
||||
- [Print a Java 2D Array](https://www.baeldung.com/java-2d-array-print)
|
||||
|
||||
@@ -7,10 +7,6 @@
|
||||
<name>core-java-arrays-guides</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<system-stubs.jupiter.version>2.1.5</system-stubs.jupiter.version>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
@@ -34,7 +30,10 @@
|
||||
<version>${system-stubs.jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<system-stubs.jupiter.version>2.1.5</system-stubs.jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -6,3 +6,4 @@ This module contains articles about Java Character Class
|
||||
- [Character#isAlphabetic vs. Character#isLetter](https://www.baeldung.com/java-character-isletter-isalphabetic)
|
||||
- [Difference Between Java’s “char” and “String”](https://www.baeldung.com/java-char-vs-string)
|
||||
- [Increment Character in Java](https://www.baeldung.com/java-char-sequence)
|
||||
- [Creating Unicode Character From Its Code Point Hex String](https://www.baeldung.com/java-unicode-character-from-code-point-hex-string)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user