Skip to content

Commit ebcd7ec

Browse files
authored
Merge branch 'eugenp:master' into master
2 parents c8e938b + 9ee6019 commit ebcd7ec

File tree

32 files changed

+789
-129
lines changed

32 files changed

+789
-129
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ Building a single module
7272
To build a specific module, run the command: `mvn clean install` in the module directory.
7373

7474

75+
Building modules from the root of the repository
76+
====================
77+
To build specific modules from the root of the repository, run the command: `mvn clean install --pl asm,atomikos -Pdefault-first` in the root directory.
78+
79+
Here `asm` and `atomikos` are the modules that we want to build and `default-first` is the maven profile in which these modules are present.
80+
81+
7582
Running a Spring Boot module
7683
====================
7784
To run a Spring Boot module, run the command: `mvn spring-boot:run` in the module directory.

core-java-modules/core-java-collections-array-list/pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,33 @@
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>core-java-collections-array-list</artifactId>
77
<version>0.1.0-SNAPSHOT</version>
8+
<build>
9+
<plugins>
10+
<plugin>
11+
<groupId>org.apache.maven.plugins</groupId>
12+
<artifactId>maven-compiler-plugin</artifactId>
13+
<configuration>
14+
<source>${maven-compiler-plugin.source}</source>
15+
<target>${maven-compiler-plugin.target}</target>
16+
</configuration>
17+
</plugin>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-surefire-plugin</artifactId>
21+
<version>${surefire.plugin.version}</version>
22+
<configuration>
23+
<argLine>
24+
--add-opens java.base/java.util=ALL-UNNAMED
25+
</argLine>
26+
</configuration>
27+
</plugin>
28+
</plugins>
29+
</build>
30+
<properties>
31+
<maven-compiler-plugin.source>16</maven-compiler-plugin.source>
32+
<maven-compiler-plugin.target>16</maven-compiler-plugin.target>
33+
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
34+
</properties>
835
<name>core-java-collections-array-list</name>
936
<packaging>jar</packaging>
1037

@@ -20,6 +47,12 @@
2047
<artifactId>commons-collections4</artifactId>
2148
<version>${commons-collections4.version}</version>
2249
</dependency>
50+
<dependency>
51+
<groupId>com.google.guava</groupId>
52+
<artifactId>guava</artifactId>
53+
<version>31.1-jre</version>
54+
<scope>test</scope>
55+
</dependency>
2356
</dependencies>
2457

2558
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.listofobjectstolistofstring;
2+
3+
public class Node {
4+
5+
private final int x;
6+
private final int y;
7+
8+
public Node(int x, int y) {
9+
this.x = x;
10+
this.y = y;
11+
}
12+
13+
@Override
14+
public String toString() {
15+
return "Node (" + "x=" + x + ", y=" + y + ')';
16+
}
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.listofobjectstolistofstring;
2+
3+
public class User {
4+
private final String fullName;
5+
6+
public User(String fullName) {
7+
this.fullName = fullName;
8+
}
9+
10+
@Override
11+
public String toString() {
12+
return "User (" + "full name='" + fullName + ')';
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.baeldung.listofobjectstolistofstring;
2+
3+
import com.google.common.collect.Lists;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.Objects;
11+
import java.util.stream.Collectors;
12+
13+
public class ConvertObjectListToStringListUnitTest {
14+
15+
@Test
16+
public void givenObjectList_whenForEachUsedToConvert_thenReturnSuccess() {
17+
List<String> outputList = new ArrayList<>(objectListWithNull().size());
18+
for (Object obj : objectListWithNull()) {
19+
outputList.add(Objects.toString(obj, null));
20+
}
21+
Assert.assertEquals(expectedStringListWithNull(), outputList);
22+
}
23+
24+
@Test
25+
public void givenObjectList_whenUsingStreamsToConvert_thenReturnSuccess() {
26+
List<String> outputList;
27+
outputList = objectListWithNull().stream()
28+
.map((obj) -> Objects.toString(obj, null))
29+
.collect(Collectors.toList());
30+
Assert.assertEquals(expectedStringListWithNull(), outputList);
31+
32+
}
33+
34+
@Test
35+
public void givenObjectList_whenUsingStreamsUnmodifiableListToConvert_thenReturnSuccess() {
36+
List<String> outputList;
37+
outputList = objectListWithNull().stream()
38+
.filter(Objects::nonNull)
39+
.map((obj) -> Objects.toString(obj, null))
40+
.collect(Collectors.toUnmodifiableList());
41+
Assert.assertEquals(expectedStringListWithoutNull(), outputList);
42+
43+
}
44+
45+
@Test
46+
public void givenObjectList_whenUsingGuavaTransform_thenReturnSuccess() {
47+
List<String> outputList;
48+
outputList = Lists.transform(objectListWithNull(), obj -> Objects.toString(obj, null));
49+
Assert.assertEquals(expectedStringListWithNull(), outputList);
50+
}
51+
52+
@Test
53+
public void givenObjectListWithNoNull_whenUsingToList_thenReturnSuccess() {
54+
List<String> outputList;
55+
outputList = objectListWithoutNull().stream()
56+
.map((obj) -> Objects.toString(obj, null))
57+
.toList();
58+
Assert.assertEquals(expectedStringListWithoutNull(), outputList);
59+
}
60+
61+
private List<String> expectedStringListWithNull() {
62+
List<String> listOfStrings = new ArrayList<>();
63+
listOfStrings.add("1");
64+
listOfStrings.add("true");
65+
listOfStrings.add("hello");
66+
listOfStrings.add(Double.toString(273773.98));
67+
listOfStrings.add(null);
68+
listOfStrings.add(new Node(2, 4).toString());
69+
listOfStrings.add(new User("John Doe").toString());
70+
return listOfStrings;
71+
}
72+
73+
private List<Object> objectListWithNull() {
74+
List<Object> listOfStrings = new ArrayList<>();
75+
listOfStrings.add(1);
76+
listOfStrings.add(true);
77+
listOfStrings.add("hello");
78+
listOfStrings.add(Double.valueOf(273773.98));
79+
listOfStrings.add(null);
80+
listOfStrings.add(new Node(2, 4));
81+
listOfStrings.add(new User("John Doe"));
82+
return listOfStrings;
83+
}
84+
85+
private List<String> expectedStringListWithoutNull() {
86+
return List.of("1", "true", "hello", Double.toString(273773.98), new Node(2, 4).toString(), new User("John Doe").toString());
87+
}
88+
89+
private List<Object> objectListWithoutNull() {
90+
return List.of(1, true, "hello", Double.valueOf(273773.98), new Node(2, 4), new User("John Doe"));
91+
}
92+
}

core-java-modules/core-java-lang-5/src/main/java/com/baeldung/convertnumberbases/ConvertNumberBases.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,76 @@
22

33
public class ConvertNumberBases {
44

5-
public static String convertNumberToNewBase(String number, int base, int newBase){
5+
public static String convertNumberToNewBase(String number, int base, int newBase) {
66
return Integer.toString(Integer.parseInt(number, base), newBase);
77
}
8+
89
public static String convertNumberToNewBaseCustom(String num, int base, int newBase) {
910
int decimalNumber = convertFromAnyBaseToDecimal(num, base);
10-
return convertFromDecimalToBaseX(decimalNumber, newBase);
11+
String targetBase = "";
12+
try {
13+
targetBase = convertFromDecimalToBaseX(decimalNumber, newBase);
14+
} catch (IllegalArgumentException e) {
15+
e.printStackTrace();
16+
}
17+
return targetBase;
1118
}
1219

13-
public static String convertFromDecimalToBaseX(int num, int newBase) {
20+
public static String convertFromDecimalToBaseX(int num, int newBase) throws IllegalArgumentException {
21+
if ((newBase < 2 || newBase > 10) && newBase != 16) {
22+
throw new IllegalArgumentException("New base must be from 2 - 10 or 16");
23+
}
1424

1525
String result = "";
1626
int remainder;
1727
while (num > 0) {
1828
remainder = num % newBase;
1929
if (newBase == 16) {
20-
if (remainder == 10)
30+
if (remainder == 10) {
2131
result += 'A';
22-
else if (remainder == 11)
32+
} else if (remainder == 11) {
2333
result += 'B';
24-
else if (remainder == 12)
34+
} else if (remainder == 12) {
2535
result += 'C';
26-
else if (remainder == 13)
36+
} else if (remainder == 13) {
2737
result += 'D';
28-
else if (remainder == 14)
38+
} else if (remainder == 14) {
2939
result += 'E';
30-
else if (remainder == 15)
40+
} else if (remainder == 15) {
3141
result += 'F';
32-
else
42+
} else {
3343
result += remainder;
34-
} else
44+
}
45+
} else {
3546
result += remainder;
36-
47+
}
3748
num /= newBase;
3849
}
3950
return new StringBuffer(result).reverse().toString();
4051
}
4152

4253
public static int convertFromAnyBaseToDecimal(String num, int base) {
43-
44-
if (base < 2 || (base > 10 && base != 16))
54+
if (base < 2 || (base > 10 && base != 16)) {
4555
return -1;
46-
56+
}
4757
int val = 0;
4858
int power = 1;
49-
5059
for (int i = num.length() - 1; i >= 0; i--) {
5160
int digit = charToDecimal(num.charAt(i));
52-
53-
if (digit < 0 || digit >= base)
61+
if (digit < 0 || digit >= base) {
5462
return -1;
55-
63+
}
5664
val += digit * power;
5765
power = power * base;
5866
}
59-
6067
return val;
6168
}
6269

6370
public static int charToDecimal(char c) {
64-
if (c >= '0' && c <= '9')
71+
if (c >= '0' && c <= '9') {
6572
return (int) c - '0';
66-
else
73+
} else {
6774
return (int) c - 'A' + 10;
75+
}
6876
}
6977
}

core-java-modules/core-java-lang-5/src/test/java/com/baeldung/convertnumberbases/ConvertNumberBasesUnitTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.convertnumberbases;
22

3+
import static com.baeldung.convertnumberbases.ConvertNumberBases.convertFromDecimalToBaseX;
34
import static com.baeldung.convertnumberbases.ConvertNumberBases.convertNumberToNewBase;
45
import static com.baeldung.convertnumberbases.ConvertNumberBases.convertNumberToNewBaseCustom;
56
import static org.junit.jupiter.api.Assertions.*;
@@ -17,4 +18,15 @@ void whenConvertingBase2NumberToBase8_ThenResultShouldBeDigitsInBase8() {
1718
assertEquals(convertNumberToNewBaseCustom("11001000", 2, 8), "310");
1819
}
1920

21+
@Test
22+
void whenInputIsOutOfRange_thenIllegalArgumentExceptionIsThrown() {
23+
Exception exception = assertThrows(IllegalArgumentException.class, ()-> {
24+
convertFromDecimalToBaseX(100, 12);
25+
});
26+
String expectedMessage = "New base must be from 2 - 10 or 16";
27+
String actualMessage = exception.getMessage();
28+
29+
assertTrue(actualMessage.contains(expectedMessage));
30+
}
31+
2032
}

core-java-modules/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
<module>core-java-collections-2</module>
3232
<module>core-java-collections-3</module>
3333
<module>core-java-collections-4</module>
34-
<module>core-java-collections-array-list</module>
3534
<module>core-java-collections-conversions</module>
3635
<module>core-java-collections-conversions-2</module>
3736
<module>core-java-collections-set-2</module>

httpclient-simple/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@
112112
</exclusion>
113113
</exclusions>
114114
</dependency>
115+
116+
<dependency>
117+
<groupId>org.apache.httpcomponents.client5</groupId>
118+
<artifactId>httpclient5-fluent</artifactId>
119+
<version>${httpclient5-fluent.version}</version>
120+
<exclusions>
121+
<exclusion>
122+
<artifactId>commons-logging</artifactId>
123+
<groupId>commons-logging</groupId>
124+
</exclusion>
125+
</exclusions>
126+
</dependency>
115127
<!-- utils -->
116128
<dependency>
117129
<groupId>org.apache.commons</groupId>
@@ -308,6 +320,7 @@
308320
<!-- http client & core 5 -->
309321
<httpcore5.version>5.2</httpcore5.version>
310322
<httpclient5.version>5.2</httpclient5.version>
323+
<httpclient5-fluent.version>5.2</httpclient5-fluent.version>
311324
<!-- maven plugins -->
312325
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
313326
</properties>

0 commit comments

Comments
 (0)