Skip to content

Commit db35d6c

Browse files
authored
Merge branch 'eugenp:master' into master
2 parents 42a78e4 + 94e0317 commit db35d6c

File tree

82 files changed

+2276
-22
lines changed

Some content is hidden

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

82 files changed

+2276
-22
lines changed

core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arraycompare/ArraysCompareUnitTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ void givenSameContents_whenCompare_thenCorrect() {
1818

1919
@Test
2020
void givenDifferentContents_whenCompare_thenDifferent() {
21-
String[] array1 = new String[] { "A", "B", "C" };
22-
String[] array2 = new String[] { "A", "C", "B", "D" };
21+
String[] array1 = new String[] { "A", "B", "C", "D"};
22+
String[] array2 = new String[] { "A", "C", "B" };
2323

2424
assertThat(Arrays.compare(array1, array2)).isLessThan(0);
2525
assertThat(Arrays.compare(array2, array1)).isGreaterThan(0);

core-java-modules/core-java-arrays-operations-basic/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656
</execution>
5757
</executions>
5858
</plugin>
59+
<plugin>
60+
<groupId>org.apache.maven.plugins</groupId>
61+
<artifactId>maven-compiler-plugin</artifactId>
62+
<configuration>
63+
<source>10</source>
64+
<target>10</target>
65+
</configuration>
66+
</plugin>
5967
</plugins>
6068
</build>
6169

core-java-modules/core-java-arrays-operations-basic/src/main/java/com/baeldung/array/ArrayInitializer.java

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package com.baeldung.array;
22

33
import java.util.Arrays;
4+
import java.util.stream.DoubleStream;
5+
import java.util.stream.IntStream;
46

57
import org.apache.commons.lang3.ArrayUtils;
68

79
public class ArrayInitializer {
810

911
static int[] initializeArrayInLoop() {
10-
int array[] = new int[5];
12+
int[] array = new int[5];
1113
for (int i = 0; i < array.length; i++) {
1214
array[i] = i + 2;
1315
}
1416
return array;
1517
}
1618

1719
static int[][] initializeMultiDimensionalArrayInLoop() {
18-
int array[][] = new int[2][5];
20+
int[][] array = new int[2][5];
1921
for (int i = 0; i < 2; i++) {
2022
for (int j = 0; j < 5; j++) {
2123
array[i][j] = j + 1;
@@ -26,7 +28,7 @@ static int[][] initializeMultiDimensionalArrayInLoop() {
2628
}
2729

2830
static String[] initializeArrayAtTimeOfDeclarationMethod1() {
29-
String array[] = new String[] { "Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda" };
31+
String[] array = new String[] { "Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda" };
3032
return array;
3133
}
3234

@@ -36,30 +38,30 @@ static int[] initializeArrayAtTimeOfDeclarationMethod2() {
3638
}
3739

3840
static int[] initializeArrayAtTimeOfDeclarationMethod3() {
39-
int array[] = { 1, 2, 3, 4, 5 };
41+
int[] array = { 1, 2, 3, 4, 5 };
4042
return array;
4143
}
4244

4345
static long[] initializeArrayUsingArraysFill() {
44-
long array[] = new long[5];
46+
long[] array = new long[5];
4547
Arrays.fill(array, 30);
4648
return array;
4749
}
4850

4951
static int[] initializeArrayRangeUsingArraysFill() {
50-
int array[] = new int[5];
52+
int[] array = new int[5];
5153
Arrays.fill(array, 0, 3, -50);
5254
return array;
5355
}
5456

5557
static int[] initializeArrayUsingArraysCopy() {
56-
int array[] = { 1, 2, 3, 4, 5 };
58+
int[] array = { 1, 2, 3, 4, 5 };
5759
int[] copy = Arrays.copyOf(array, 5);
5860
return copy;
5961
}
6062

6163
static int[] initializeLargerArrayUsingArraysCopy() {
62-
int array[] = { 1, 2, 3, 4, 5 };
64+
int[] array = { 1, 2, 3, 4, 5 };
6365
int[] copy = Arrays.copyOf(array, 6);
6466
return copy;
6567
}
@@ -75,4 +77,56 @@ static char[] initializeArrayUsingArraysUtilClone() {
7577
char[] array = new char[] { 'a', 'b', 'c' };
7678
return ArrayUtils.clone(array);
7779
}
80+
81+
static int[] initializeArrayUsingArraysStream() {
82+
int[] array = new int[5];
83+
Arrays.setAll(array, i -> i + 1);
84+
return array;
85+
}
86+
87+
static int[] initializeArrayWithDefaultValues() {
88+
return new int[5];
89+
}
90+
91+
static double[] initializeArrayOfDoubleTypeWithDefaultValues() {
92+
return new double[5];
93+
}
94+
95+
static String[] initializeArrayOfStringTypeWithDefaultValues() {
96+
return new String[5];
97+
}
98+
99+
static boolean[] initializeArrayOfBooleanTypeWithDefaultValues() {
100+
return new boolean[5];
101+
}
102+
103+
static int[][] initializeTwoDimensionalArrayWithDefaultValues() {
104+
return new int[2][5];
105+
}
106+
107+
static int[] initializeArrayUsingIntStream() {
108+
int[] values = IntStream.of(1, 2, 3, 4, 5)
109+
.toArray();
110+
return values;
111+
}
112+
113+
static double[] initializeArrayOfDoubleTypeUsingStreamApi() {
114+
return Arrays.stream(new double[] { 1.1, 2.2, 3.3, 4.4, 5.5 })
115+
.toArray();
116+
}
117+
118+
static int[][][] initializeThreeDimensionalArrayWithDefaultValue() {
119+
return new int[2][3][4];
120+
}
121+
122+
static int[][] initializeTwoDimensionalArrayUsingStream() {
123+
int[][] matrix = IntStream.range(0, 3)
124+
.mapToObj(i -> IntStream.range(0, 4)
125+
.map(j -> i * 4 + j)
126+
.toArray())
127+
.toArray(int[][]::new);
128+
129+
return matrix;
130+
}
131+
78132
}

core-java-modules/core-java-arrays-operations-basic/src/test/java/com/baeldung/array/ArrayInitializerUnitTest.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,19 @@
1111
import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysUtilClone;
1212
import static com.baeldung.array.ArrayInitializer.initializeLargerArrayUsingArraysCopy;
1313
import static com.baeldung.array.ArrayInitializer.initializeMultiDimensionalArrayInLoop;
14+
import static com.baeldung.array.ArrayInitializer.initializeTwoDimensionalArrayWithDefaultValues;
15+
import static com.baeldung.array.ArrayInitializer.initializeArrayWithDefaultValues;
16+
import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysStream;
17+
import static com.baeldung.array.ArrayInitializer.initializeArrayUsingIntStream;
18+
import static com.baeldung.array.ArrayInitializer.initializeArrayOfDoubleTypeUsingStreamApi;
19+
import static com.baeldung.array.ArrayInitializer.initializeTwoDimensionalArrayUsingStream;
1420
import static org.junit.Assert.assertArrayEquals;
21+
import static org.junit.Assert.assertEquals;
1522

1623
import org.junit.Test;
1724

25+
import java.util.Arrays;
26+
1827
public class ArrayInitializerUnitTest {
1928

2029
@Test
@@ -71,4 +80,94 @@ public void whenInitializeLargerArrayRangeUsingArraysSetAll_thenCorrect() {
7180
public void whenInitializeArrayUsingArraysUtilClone_thenCorrect() {
7281
assertArrayEquals(new char[] { 'a', 'b', 'c' }, initializeArrayUsingArraysUtilClone());
7382
}
83+
84+
@Test
85+
public void whenInitializeArrayWithDefault_thenCorrect() {
86+
assertArrayEquals(new int[5], initializeArrayWithDefaultValues());
87+
int[] array = new int[5];
88+
assertArrayEquals(new int[] { 0, 0, 0, 0, 0 }, array);
89+
}
90+
91+
@Test
92+
public void whenInitializeArrayOfBooleanTypeWithDefault_thenCorrect() {
93+
boolean[] array = new boolean[5];
94+
assertArrayEquals(new boolean[] { false, false, false, false, false }, array);
95+
}
96+
97+
@Test
98+
public void whenInitializeArrayOfStringTypeWithDefault_thenCorrect() {
99+
String[] array = new String[5];
100+
assertArrayEquals(new String[] { null, null, null, null, null }, array);
101+
}
102+
103+
@Test
104+
public void whenAddingItemsToAnArray_thenCorrect() {
105+
int[] numbers = initializeArrayWithDefaultValues();
106+
numbers[0] = 10;
107+
numbers[1] = 20;
108+
numbers[2] = 30;
109+
numbers[3] = 40;
110+
numbers[4] = 50;
111+
assertEquals(5, numbers.length);
112+
assertEquals(20, numbers[1]);
113+
}
114+
115+
@Test
116+
public void whenInitializeEmptyTwoDimensionalArray_thenCorrect() {
117+
assertArrayEquals(new int[2][5], initializeTwoDimensionalArrayWithDefaultValues());
118+
}
119+
120+
@Test
121+
public void whenAddingItemsToAnTwoDimensionalArray_thenCorrect() {
122+
int[][] matrix = initializeTwoDimensionalArrayWithDefaultValues();
123+
matrix[0][0] = 10;
124+
matrix[0][1] = 20;
125+
matrix[0][2] = 30;
126+
matrix[0][3] = 40;
127+
matrix[0][4] = 50;
128+
matrix[1][0] = 60;
129+
matrix[1][1] = 70;
130+
matrix[1][2] = 80;
131+
matrix[1][3] = 90;
132+
matrix[1][4] = 100;
133+
assertEquals(2, matrix.length);
134+
assertEquals(5, matrix[0].length);
135+
assertEquals(5, matrix[1].length);
136+
assertEquals(20, matrix[0][1]);
137+
}
138+
139+
@Test
140+
public void whenInitializeArrayWithStream_thenCorrect() {
141+
assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, initializeArrayUsingArraysStream());
142+
}
143+
144+
@Test
145+
public void whenInitializeArrayOfIntegerUsingIntStream_thenCorrect() {
146+
assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, initializeArrayUsingIntStream());
147+
}
148+
149+
@Test
150+
public void whenInitializeArrayOfDoubleUsingStreamApi_thenCorrect() {
151+
assertArrayEquals(new double[] { 1.1, 2.2, 3.3, 4.4, 5.5 }, initializeArrayOfDoubleTypeUsingStreamApi(), 0.0);
152+
}
153+
154+
@Test
155+
public void whenInitializeEmptyThreeDimensionalArray_thenCorrect() {
156+
int[][][] threeDimensionalArray = new int[2][3][4];
157+
assertEquals(2, threeDimensionalArray.length);
158+
assertEquals(3, threeDimensionalArray[0].length);
159+
assertEquals(4, threeDimensionalArray[0][0].length);
160+
}
161+
162+
@Test
163+
public void whenInitializeTwoDimensionalArrayWithStreamApi_thenCorrect() {
164+
assertArrayEquals(new int[][] { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 } }, initializeTwoDimensionalArrayUsingStream());
165+
}
166+
167+
@Test
168+
public void whenPopulatingAnArrayWithSetAllMethod_thenCorrect() {
169+
int[] numbers = new int[5];
170+
Arrays.setAll(numbers, i -> i * 2);
171+
assertArrayEquals(new int[] { 0, 2, 4, 6, 8 }, numbers);
172+
}
74173
}

libraries-data-db/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@
163163
<artifactId>jakarta.xml.bind-api</artifactId>
164164
<version>${jakarta.xml.bind.version}</version>
165165
</dependency>
166+
167+
<!-- TigerBeetle -->
168+
<dependency>
169+
<groupId>com.tigerbeetle</groupId>
170+
<artifactId>tigerbeetle-java</artifactId>
171+
<version>0.15.3</version>
172+
</dependency>
166173
</dependencies>
167174

168175
<build>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.libraries.tigerbeetle.config;
2+
3+
import com.tigerbeetle.Client;
4+
import com.tigerbeetle.UInt128;
5+
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
import java.math.BigInteger;
10+
11+
@Configuration
12+
public class TigerBeetleConfig {
13+
14+
@Value("${tigerbeetle.clusterID:0}")
15+
private BigInteger clusterID;
16+
17+
@Value("${tb_address:3000}")
18+
private String[] replicaAddress;
19+
20+
@Bean
21+
Client tigerBeetleClient() {
22+
return new Client(UInt128.asBytes(clusterID), replicaAddress);
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.libraries.tigerbeetle.domain;
2+
3+
import lombok.Builder;
4+
5+
import java.math.BigInteger;
6+
import java.util.UUID;
7+
8+
@Builder
9+
public record Account(
10+
UUID id,
11+
BigInteger accountHolderId,
12+
int code,
13+
int ledger,
14+
int userData32,
15+
long userData64,
16+
BigInteger creditsPosted,
17+
BigInteger creditsPending,
18+
BigInteger debtsPosted,
19+
BigInteger debtsPending,
20+
int flags,
21+
long timestamp) {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.libraries.tigerbeetle.domain;
2+
3+
import com.tigerbeetle.CreateAccountResult;
4+
5+
public class AccountException extends RuntimeException{
6+
7+
private final CreateAccountResult result;
8+
9+
public AccountException(CreateAccountResult result) {
10+
this.result = result;
11+
}
12+
13+
public CreateAccountResult getResult() {
14+
return this.result;
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.libraries.tigerbeetle.domain;
2+
3+
import lombok.Builder;
4+
5+
import java.math.BigInteger;
6+
import java.time.Instant;
7+
import java.util.UUID;
8+
9+
@Builder
10+
public record Balance(
11+
UUID accountId,
12+
Instant timestamp,
13+
BigInteger creditsPosted,
14+
BigInteger creditsPending,
15+
BigInteger debitsPosted,
16+
BigInteger debitsPending
17+
) {
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.libraries.tigerbeetle.domain;
2+
3+
import lombok.Builder;
4+
5+
import java.math.BigInteger;
6+
import java.time.Instant;
7+
import java.util.UUID;
8+
9+
@Builder
10+
public record Transfer(
11+
UUID id,
12+
BigInteger amount,
13+
int code,
14+
int ledger,
15+
int flags,
16+
UUID debitAccountId,
17+
UUID creditAccountId,
18+
Instant timestamp,
19+
int userData32,
20+
long userData64,
21+
UUID userData128,
22+
UUID pendingId
23+
) {
24+
}

0 commit comments

Comments
 (0)