Skip to content

Commit d12f30c

Browse files
authored
Merge branch 'master' into PR-7319
2 parents 73ceac2 + c72cb42 commit d12f30c

File tree

504 files changed

+78661
-860
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

504 files changed

+78661
-860
lines changed

algorithms-modules/algorithms-miscellaneous-7/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
- [Getting Pixel Array From Image in Java](https://www.baeldung.com/java-getting-pixel-array-from-image)
66
- [Calculate Distance Between Two Coordinates in Java](https://www.baeldung.com/java-find-distance-between-points)
77
- [Rotate Arrays in Java](https://www.baeldung.com/java-rotate-arrays)
8+
- [Find Missing Number From a Given Array in Java](https://www.baeldung.com/java-array-find-missing-number)
89
- More articles: [[<-- prev]](/algorithms-miscellaneous-6)
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package com.baeldung.algorithms.weightedaverage;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.*;
6+
import java.util.function.BiConsumer;
7+
import java.util.function.BinaryOperator;
8+
import java.util.function.Function;
9+
import java.util.function.Supplier;
10+
import java.util.stream.Collector;
11+
import java.util.stream.IntStream;
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
15+
public class WeightedAverageUnitTest {
16+
17+
private List<Values> values = Arrays.asList(
18+
new Values(1, 10),
19+
new Values(3, 20),
20+
new Values(5, 30),
21+
new Values(7, 50),
22+
new Values(9, 40)
23+
);
24+
25+
private Double expected = 6.2;
26+
27+
@Test
28+
void twoPass() {
29+
double top = values.stream()
30+
.mapToDouble(v -> v.value * v.weight)
31+
.sum();
32+
double bottom = values.stream()
33+
.mapToDouble(v -> v.weight)
34+
.sum();
35+
36+
double result = top / bottom;
37+
assertEquals(expected, result);
38+
}
39+
40+
@Test
41+
void onePass() {
42+
double top = 0;
43+
double bottom = 0;
44+
45+
for (Values v : values) {
46+
top += (v.value * v.weight);
47+
bottom += v.weight;
48+
}
49+
50+
double result = top / bottom;
51+
assertEquals(expected, result);
52+
}
53+
54+
@Test
55+
void expanding() {
56+
double result = values.stream()
57+
.flatMap(v -> Collections.nCopies(v.weight, v.value).stream())
58+
.mapToInt(v -> v)
59+
.average()
60+
.getAsDouble();
61+
assertEquals(expected, result);
62+
}
63+
64+
@Test
65+
void reduce() {
66+
class WeightedAverage {
67+
final double top;
68+
final double bottom;
69+
70+
public WeightedAverage(double top, double bottom) {
71+
this.top = top;
72+
this.bottom = bottom;
73+
}
74+
75+
double average() {
76+
return top / bottom;
77+
}
78+
}
79+
80+
double result = values.stream()
81+
.reduce(new WeightedAverage(0, 0),
82+
(acc, next) -> new WeightedAverage(
83+
acc.top + (next.value * next.weight),
84+
acc.bottom + next.weight),
85+
(left, right) -> new WeightedAverage(
86+
left.top + right.top,
87+
left.bottom + right.bottom))
88+
.average();
89+
assertEquals(expected, result);
90+
}
91+
92+
@Test
93+
void customCollector() {
94+
class WeightedAverage implements Collector<Values, WeightedAverage.RunningTotals, Double> {
95+
class RunningTotals {
96+
double top;
97+
double bottom;
98+
99+
public RunningTotals() {
100+
this.top = 0;
101+
this.bottom = 0;
102+
}
103+
}
104+
105+
@Override
106+
public Supplier<RunningTotals> supplier() {
107+
return RunningTotals::new;
108+
}
109+
110+
@Override
111+
public BiConsumer<RunningTotals, Values> accumulator() {
112+
return (current, next) -> {
113+
current.top += (next.value * next.weight);
114+
current.bottom += next.weight;
115+
};
116+
}
117+
118+
@Override
119+
public BinaryOperator<RunningTotals> combiner() {
120+
return (left, right) -> {
121+
left.top += right.top;
122+
left.bottom += right.bottom;
123+
124+
return left;
125+
};
126+
}
127+
128+
@Override
129+
public Function<RunningTotals, Double> finisher() {
130+
return rt -> rt.top / rt.bottom;
131+
}
132+
133+
@Override
134+
public Set<Characteristics> characteristics() {
135+
return Collections.singleton(Characteristics.UNORDERED);
136+
}
137+
}
138+
139+
double result = values.stream()
140+
.collect(new WeightedAverage());
141+
assertEquals(expected, result);
142+
}
143+
144+
private static class Values {
145+
int value;
146+
int weight;
147+
148+
public Values(int value, int weight) {
149+
this.value = value;
150+
this.weight = weight;
151+
}
152+
}
153+
}

apache-httpclient-2/src/test/resources/logback.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
</appender>
77
<logger name="com.baeldung.httpclient.cookies" level="info"/>
88

9-
<logger name="com.baeldung.httpclient.readresponsebodystring" level="debug"/>
10-
<logger name="org.apache.http" level="debug"/>
11-
<logger name="org.apache.hc.client5.http" level="debug"/>
9+
<logger name="com.baeldung.httpclient.readresponsebodystring" level="INFO"/>
10+
<logger name="org.apache.http" level="WARN"/>
11+
<logger name="org.apache.hc.client5.http" level="WARN"/>
1212

1313
<root level="WARN">
1414
<appender-ref ref="stdout"/>

apache-kafka-2/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ You can build the project from the command line using: *mvn clean install*, or i
1515
- [bootstrap-server in Kafka Configuration](https://www.baeldung.com/java-kafka-bootstrap-server)
1616
- [Introduction to Apache Kafka](https://www.baeldung.com/apache-kafka)
1717
- [Ensuring Message Ordering in Kafka: Strategies and Configurations](https://www.baeldung.com/kafka-message-ordering)
18+
- [Read Multiple Messages with Apache Kafka](https://www.baeldung.com/kafka-read-multiple-messages)
Binary file not shown.
Binary file not shown.
Binary file not shown.

apache-poi-3/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
</dependencies>
7777

7878
<properties>
79-
<poi.version>5.2.3</poi.version>
79+
<poi.version>5.2.5</poi.version>
8080
<poiji.version>4.1.1</poiji.version>
8181
<fastexcel.version>0.15.7</fastexcel.version>
8282
<jxl.version>2.6.12</jxl.version>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.poi.rowstyle;
2+
3+
import java.io.FileOutputStream;
4+
import java.io.IOException;
5+
import java.nio.file.Path;
6+
7+
import org.apache.poi.ss.usermodel.Cell;
8+
import org.apache.poi.ss.usermodel.CellStyle;
9+
import org.apache.poi.ss.usermodel.Font;
10+
import org.apache.poi.ss.usermodel.Row;
11+
import org.apache.poi.ss.usermodel.Sheet;
12+
import org.apache.poi.ss.usermodel.Workbook;
13+
14+
public class PoiUtils {
15+
16+
private PoiUtils() {
17+
}
18+
19+
private static void newCell(Row row, String value) {
20+
short cellNum = row.getLastCellNum();
21+
if (cellNum == -1)
22+
cellNum = 0;
23+
24+
Cell cell = row.createCell(cellNum);
25+
cell.setCellValue(value);
26+
}
27+
28+
public static Row newRow(Sheet sheet, String... rowValues) {
29+
Row row = sheet.createRow(sheet.getLastRowNum() + 1);
30+
31+
for (String value : rowValues) {
32+
newCell(row, value);
33+
}
34+
35+
return row;
36+
}
37+
38+
public static CellStyle boldFontStyle(Workbook workbook) {
39+
Font boldFont = workbook.createFont();
40+
boldFont.setBold(true);
41+
42+
CellStyle boldStyle = workbook.createCellStyle();
43+
boldStyle.setFont(boldFont);
44+
45+
return boldStyle;
46+
}
47+
48+
public static void write(Workbook workbook, Path path) throws IOException {
49+
try (FileOutputStream fileOut = new FileOutputStream(path.toFile())) {
50+
workbook.write(fileOut);
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package com.baeldung.poi.rowstyle;
2+
3+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
11+
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
12+
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
13+
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
14+
import org.apache.poi.ss.usermodel.CellStyle;
15+
import org.apache.poi.ss.usermodel.Row;
16+
import org.apache.poi.ss.usermodel.Sheet;
17+
import org.apache.poi.ss.usermodel.Workbook;
18+
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
19+
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
20+
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
21+
import org.junit.jupiter.api.Test;
22+
23+
class PoiBoldStyleIntegrationTest {
24+
25+
private void writeSampleSheet(Path destination, Workbook workbook) throws IOException {
26+
Sheet sheet = workbook.createSheet();
27+
CellStyle boldStyle = PoiUtils.boldFontStyle(workbook);
28+
29+
Row header = PoiUtils.newRow(sheet, "Name", "Value", "Details");
30+
header.setRowStyle(boldStyle);
31+
32+
PoiUtils.newRow(sheet, "Albert", "A", "First");
33+
PoiUtils.newRow(sheet, "Jane", "B", "Second");
34+
PoiUtils.newRow(sheet, "Zack", "C", "Third");
35+
36+
PoiUtils.write(workbook, destination);
37+
}
38+
39+
private void assertRowStyleAppliedAndDefaultCellStylesDontMatch(Path sheetFile) throws IOException, InvalidFormatException {
40+
try (Workbook workbook = new XSSFWorkbook(sheetFile.toFile())) {
41+
Sheet sheet = workbook.getSheetAt(0);
42+
Row row0 = sheet.getRow(0);
43+
44+
XSSFCellStyle rowStyle = (XSSFCellStyle) row0.getRowStyle();
45+
assertTrue(rowStyle.getFont()
46+
.getBold());
47+
48+
row0.forEach(cell -> {
49+
XSSFCellStyle style = (XSSFCellStyle) cell.getCellStyle();
50+
assertNotEquals(rowStyle, style);
51+
});
52+
53+
Row row1 = sheet.getRow(1);
54+
XSSFCellStyle row1Style = (XSSFCellStyle) row1.getRowStyle();
55+
assertNull(row1Style);
56+
57+
Files.delete(sheetFile);
58+
}
59+
}
60+
61+
@Test
62+
void givenXssfWorkbook_whenSetRowStyle1stRow_thenOnly1stRowStyled() throws IOException, InvalidFormatException {
63+
Path sheetFile = Files.createTempFile("xssf-row-style", ".xlsx");
64+
65+
try (Workbook workbook = new XSSFWorkbook()) {
66+
writeSampleSheet(sheetFile, workbook);
67+
}
68+
69+
assertRowStyleAppliedAndDefaultCellStylesDontMatch(sheetFile);
70+
}
71+
72+
@Test
73+
void givenSxssfWorkbook_whenSetRowStyle_thenOnly1stRowStyled() throws IOException, InvalidFormatException {
74+
Path sheetFile = Files.createTempFile("sxssf-row-style", ".xlsx");
75+
76+
try (Workbook workbook = new SXSSFWorkbook()) {
77+
writeSampleSheet(sheetFile, workbook);
78+
}
79+
80+
assertRowStyleAppliedAndDefaultCellStylesDontMatch(sheetFile);
81+
}
82+
83+
@Test
84+
void givenHssfWorkbook_whenSetRowStyle_thenOnly1stRowStyled() throws IOException {
85+
Path sheetFile = Files.createTempFile("hssf-row-style", ".xls");
86+
87+
try (Workbook workbook = new HSSFWorkbook()) {
88+
writeSampleSheet(sheetFile, workbook);
89+
}
90+
91+
try (Workbook workbook = new HSSFWorkbook(Files.newInputStream(sheetFile))) {
92+
Sheet sheet = workbook.getSheetAt(0);
93+
Row row0 = sheet.getRow(0);
94+
95+
HSSFCellStyle rowStyle = (HSSFCellStyle) row0.getRowStyle();
96+
assertTrue(rowStyle.getFont(workbook)
97+
.getBold());
98+
99+
row0.forEach(cell -> {
100+
HSSFCellStyle style = (HSSFCellStyle) cell.getCellStyle();
101+
assertNotEquals(rowStyle, style);
102+
});
103+
104+
Row row1 = sheet.getRow(1);
105+
HSSFCellStyle row1Style = (HSSFCellStyle) row1.getRowStyle();
106+
assertNull(row1Style);
107+
108+
Files.delete(sheetFile);
109+
}
110+
}
111+
112+
@Test
113+
void givenXssfWorkbook_whenSetCellStyleForEachRow_thenAllCellsContainStyle() throws IOException, InvalidFormatException {
114+
Path sheetFile = Files.createTempFile("xssf-cell-style", ".xlsx");
115+
116+
try (Workbook workbook = new XSSFWorkbook()) {
117+
Sheet sheet = workbook.createSheet();
118+
CellStyle boldStyle = PoiUtils.boldFontStyle(workbook);
119+
120+
Row header = PoiUtils.newRow(sheet, "Name", "Value", "Details");
121+
header.forEach(cell -> cell.setCellStyle(boldStyle));
122+
123+
PoiUtils.newRow(sheet, "Albert", "A", "First");
124+
PoiUtils.newRow(sheet, "Jane", "B", "Second");
125+
PoiUtils.newRow(sheet, "Zack", "C", "Third");
126+
127+
PoiUtils.write(workbook, sheetFile);
128+
}
129+
130+
try (Workbook workbook = new XSSFWorkbook(sheetFile.toFile())) {
131+
Sheet sheet = workbook.getSheetAt(0);
132+
Row row0 = sheet.getRow(0);
133+
134+
XSSFCellStyle rowStyle = (XSSFCellStyle) row0.getRowStyle();
135+
assertNull(rowStyle);
136+
137+
row0.forEach(cell -> {
138+
XSSFCellStyle style = (XSSFCellStyle) cell.getCellStyle();
139+
assertTrue(style.getFont()
140+
.getBold());
141+
});
142+
143+
Row row1 = sheet.getRow(1);
144+
rowStyle = (XSSFCellStyle) row1.getRowStyle();
145+
assertNull(rowStyle);
146+
147+
Files.delete(sheetFile);
148+
}
149+
}
150+
}

0 commit comments

Comments
 (0)