Skip to content

Commit 6ebf7d0

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents ef541ac + 2b317d1 commit 6ebf7d0

33 files changed

Lines changed: 1305 additions & 219 deletions

File tree

‎core-java-arrays/src/main/java/com/baeldung/array/operations/ArrayOperations.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import java.util.Arrays;
55
import java.util.HashSet;
66
import java.util.LinkedHashSet;
7+
import java.util.LinkedList;
78
import java.util.Random;
89
import java.util.Set;
910
import java.util.function.Function;
1011
import java.util.function.IntPredicate;
1112
import java.util.function.Predicate;
13+
import java.util.stream.Stream;
1214

1315
import org.apache.commons.lang3.ArrayUtils;
1416

@@ -194,4 +196,16 @@ public static int getRandomFromIntArray(int[] array) {
194196
public static <T> T getRandomFromObjectArray(T[] array) {
195197
return array[new Random().nextInt(array.length)];
196198
}
199+
200+
public static Integer[] intersectionSimple(final Integer[] a, final Integer[] b){
201+
return Stream.of(a).filter(Arrays.asList(b)::contains).toArray(Integer[]::new);
202+
}
203+
204+
public static Integer[] intersectionSet(final Integer[] a, final Integer[] b){
205+
return Stream.of(a).filter(Arrays.asList(b)::contains).distinct().toArray(Integer[]::new);
206+
}
207+
208+
public static Integer[] intersectionMultiSet(final Integer[] a, final Integer[] b){
209+
return Stream.of(a).filter(new LinkedList<>(Arrays.asList(b))::remove).toArray(Integer[]::new);
210+
}
197211
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.array.operations;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static com.baeldung.array.operations.ArrayOperations.intersectionMultiSet;
6+
import static com.baeldung.array.operations.ArrayOperations.intersectionSet;
7+
import static com.baeldung.array.operations.ArrayOperations.intersectionSimple;
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
class IntersectionUnitTest {
11+
private static final Integer[] a = { 1, 3, 2 };
12+
private static final Integer[] b = { 4, 3, 2, 4, 2, 3, 4, 4, 3 };
13+
private static final Integer[] c = { 1, 3, 2, 3, 3, 2 };
14+
15+
@Test
16+
void whenIntersectionSimpleIsUsed_thenCommonEntriesAreInTheResult() {
17+
assertThat(intersectionSimple(a, b)).isEqualTo(new Integer[] { 3, 2 });
18+
assertThat(intersectionSimple(b, a)).isEqualTo(new Integer[] { 3, 2, 2, 3, 3 });
19+
}
20+
21+
@Test
22+
void whenIntersectionSimpleIsUsedWithAnArrayAndItself_thenTheResultIsTheIdentity() {
23+
assertThat(intersectionSimple(b, b)).isEqualTo(b);
24+
assertThat(intersectionSimple(a, a)).isEqualTo(a);
25+
}
26+
27+
@Test
28+
void whenIntersectionSetIsUsed_thenCommonEntriesAreInTheResult() {
29+
assertThat(intersectionSet(b, a)).isEqualTo(new Integer[] { 3, 2 });
30+
}
31+
32+
@Test
33+
void whenIntersectionSetIsUsed_thenTheNumberOfEntriesDoesNotChangeWithTheParameterOrder() {
34+
assertThat(intersectionSet(a, b)).isEqualTo(new Integer[] { 3, 2 });
35+
assertThat(intersectionSet(b, a)).isEqualTo(new Integer[] { 3, 2 });
36+
}
37+
38+
@Test
39+
void whenIntersectionSetIsUsedWithAnArrayAndWithItself_andTheInputHasNoDuplicateEntries_ThenTheResultIsTheIdentity() {
40+
assertThat(intersectionSet(a, a)).isEqualTo(a);
41+
}
42+
43+
@Test
44+
void whenIntersectionSetIsUsedWithAnArrayAndWithItself_andTheInputHasDuplicateEntries_ThenTheResultIsNotTheIdentity() {
45+
assertThat(intersectionSet(b, b)).isNotEqualTo(b);
46+
}
47+
48+
@Test
49+
void whenMultiSetIsUsed_thenCommonEntriesAreInTheResult() {
50+
assertThat(intersectionMultiSet(b, a)).isEqualTo(new Integer[] { 3, 2 });
51+
}
52+
53+
@Test
54+
void whenIntersectionMultiSetIsUsed_thenTheNumberOfEntriesDoesNotChangeWithTheParameterOrder() {
55+
assertThat(intersectionMultiSet(a, b)).isEqualTo(new Integer[] { 3, 2 });
56+
assertThat(intersectionMultiSet(b, a)).isEqualTo(new Integer[] { 3, 2 });
57+
assertThat(intersectionMultiSet(b, c)).isEqualTo(new Integer[] { 3, 2, 2, 3, 3 });
58+
assertThat(intersectionMultiSet(c, b)).isEqualTo(new Integer[] { 3, 2, 3, 3, 2 });
59+
}
60+
61+
@Test
62+
void whenIntersectionMultiSetIsUsedWithAnArrayAndWithItself_ThenTheResultIsTheIdentity() {
63+
assertThat(intersectionMultiSet(b, b)).isEqualTo(b);
64+
assertThat(intersectionMultiSet(a, a)).isEqualTo(a);
65+
}
66+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.constructors;
2+
3+
import java.time.LocalDateTime;
4+
5+
class BankAccount {
6+
String name;
7+
LocalDateTime opened;
8+
double balance;
9+
10+
@Override
11+
public String toString() {
12+
return String.format("%s, %s, %f", this.name, this.opened.toString(), this.balance);
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public LocalDateTime getOpened() {
20+
return opened;
21+
}
22+
23+
public double getBalance() {
24+
return this.balance;
25+
}
26+
}
27+
28+
class BankAccountEmptyConstructor extends BankAccount {
29+
public BankAccountEmptyConstructor() {
30+
this.name = "";
31+
this.opened = LocalDateTime.now();
32+
this.balance = 0.0d;
33+
}
34+
}
35+
36+
class BankAccountParameterizedConstructor extends BankAccount {
37+
public BankAccountParameterizedConstructor(String name, LocalDateTime opened, double balance) {
38+
this.name = name;
39+
this.opened = opened;
40+
this.balance = balance;
41+
}
42+
}
43+
44+
class BankAccountCopyConstructor extends BankAccount {
45+
public BankAccountCopyConstructor(String name, LocalDateTime opened, double balance) {
46+
this.name = name;
47+
this.opened = opened;
48+
this.balance = balance;
49+
}
50+
51+
public BankAccountCopyConstructor(BankAccount other) {
52+
this.name = other.name;
53+
this.opened = LocalDateTime.now();
54+
this.balance = 0.0f;
55+
}
56+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.constructors;
2+
3+
import java.time.LocalDateTime;
4+
5+
class Transaction {
6+
final BankAccountEmptyConstructor bankAccount;
7+
final LocalDateTime date;
8+
final double amount;
9+
10+
public Transaction(BankAccountEmptyConstructor account, LocalDateTime date, double amount) {
11+
this.bankAccount = account;
12+
this.date = date;
13+
this.amount = amount;
14+
}
15+
16+
/*
17+
* Compilation Error :'(, all final variables must be explicitly initialised.
18+
* public Transaction() {
19+
* }
20+
*/
21+
22+
public void invalidMethod() {
23+
// this.amount = 102.03; // Results in a compiler error. You cannot change the value of a final variable.
24+
}
25+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.constructors;
2+
3+
import com.baeldung.constructors.*;
4+
5+
import java.util.logging.Logger;
6+
import java.time.LocalDateTime;
7+
import java.time.Month;
8+
9+
import org.junit.Test;
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.assertj.core.api.Assertions.assertThatCode;
12+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
13+
14+
public class ConstructorUnitTest {
15+
final static Logger LOGGER = Logger.getLogger(ConstructorUnitTest.class.getName());
16+
17+
@Test
18+
public void givenNoExplicitContructor_whenUsed_thenFails() {
19+
BankAccount account = new BankAccount();
20+
assertThatThrownBy(() -> { account.toString(); }).isInstanceOf(Exception.class);
21+
}
22+
23+
@Test
24+
public void givenNoArgumentConstructor_whenUsed_thenSucceeds() {
25+
BankAccountEmptyConstructor account = new BankAccountEmptyConstructor();
26+
assertThatCode(() -> {
27+
account.toString();
28+
}).doesNotThrowAnyException();
29+
}
30+
31+
@Test
32+
public void givenParameterisedConstructor_whenUsed_thenSucceeds() {
33+
LocalDateTime opened = LocalDateTime.of(2018, Month.JUNE, 29, 06, 30, 00);
34+
BankAccountParameterizedConstructor account =
35+
new BankAccountParameterizedConstructor("Tom", opened, 1000.0f);
36+
37+
assertThatCode(() -> {
38+
account.toString();
39+
}).doesNotThrowAnyException();
40+
}
41+
42+
@Test
43+
public void givenCopyContructor_whenUser_thenMaintainsLogic() {
44+
LocalDateTime opened = LocalDateTime.of(2018, Month.JUNE, 29, 06, 30, 00);
45+
BankAccountCopyConstructor account = new BankAccountCopyConstructor("Tim", opened, 1000.0f);
46+
BankAccountCopyConstructor newAccount = new BankAccountCopyConstructor(account);
47+
48+
assertThat(account.getName()).isEqualTo(newAccount.getName());
49+
assertThat(account.getOpened()).isNotEqualTo(newAccount.getOpened());
50+
51+
assertThat(newAccount.getBalance()).isEqualTo(0.0f);
52+
}
53+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.baeldung.string;
2+
3+
4+
import org.apache.commons.lang3.StringUtils;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertTrue;
9+
10+
public class StringReplaceAndRemoveUnitTest {
11+
12+
13+
@Test
14+
public void givenTestStrings_whenReplace_thenProcessedString() {
15+
16+
String master = "Hello World Baeldung!";
17+
String target = "Baeldung";
18+
String replacement = "Java";
19+
String processed = master.replace(target, replacement);
20+
assertTrue(processed.contains(replacement));
21+
assertFalse(processed.contains(target));
22+
23+
}
24+
25+
@Test
26+
public void givenTestStrings_whenReplaceAll_thenProcessedString() {
27+
28+
String master2 = "Welcome to Baeldung, Hello World Baeldung";
29+
String regexTarget= "(Baeldung)$";
30+
String replacement = "Java";
31+
String processed2 = master2.replaceAll(regexTarget, replacement);
32+
assertTrue(processed2.endsWith("Java"));
33+
34+
}
35+
36+
@Test
37+
public void givenTestStrings_whenStringBuilderMethods_thenProcessedString() {
38+
39+
String master = "Hello World Baeldung!";
40+
String target = "Baeldung";
41+
String replacement = "Java";
42+
43+
int startIndex = master.indexOf(target);
44+
int stopIndex = startIndex + target.length();
45+
46+
StringBuilder builder = new StringBuilder(master);
47+
48+
49+
builder.delete(startIndex, stopIndex);
50+
assertFalse(builder.toString().contains(target));
51+
52+
53+
builder.replace(startIndex, stopIndex, replacement);
54+
assertTrue(builder.toString().contains(replacement));
55+
56+
57+
}
58+
59+
60+
@Test
61+
public void givenTestStrings_whenStringUtilsMethods_thenProcessedStrings() {
62+
63+
String master = "Hello World Baeldung!";
64+
String target = "Baeldung";
65+
String replacement = "Java";
66+
67+
String processed = StringUtils.replace(master, target, replacement);
68+
assertTrue(processed.contains(replacement));
69+
70+
String master2 = "Hello World Baeldung!";
71+
String target2 = "baeldung";
72+
String processed2 = StringUtils.replaceIgnoreCase(master2, target2, replacement);
73+
assertFalse(processed2.contains(target));
74+
75+
}
76+
77+
78+
79+
80+
81+
82+
83+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.timestamp;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import java.sql.Timestamp;
7+
import java.time.LocalDateTime;
8+
import java.time.format.DateTimeFormatter;
9+
10+
public class StringToTimestampConverterUnitTest {
11+
12+
@Test
13+
public void givenDatePattern_whenParsing_thenTimestampIsCorrect() {
14+
String pattern = "MMM dd, yyyy HH:mm:ss.SSSSSSSS";
15+
String timestampAsString = "Nov 12, 2018 13:02:56.12345678";
16+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
17+
LocalDateTime localDateTime = LocalDateTime.from(formatter.parse(timestampAsString));
18+
19+
Timestamp timestamp = Timestamp.valueOf(localDateTime);
20+
Assert.assertEquals("2018-11-12 13:02:56.12345678", timestamp.toString());
21+
}
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.timestamp;
2+
3+
import org.junit.Assert;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.sql.Timestamp;
7+
import java.time.format.DateTimeFormatter;
8+
9+
public class TimestampToStringConverterTest {
10+
11+
@Test
12+
public void givenDatePattern_whenFormatting_thenResultingStringIsCorrect() {
13+
Timestamp timestamp = Timestamp.valueOf("2018-12-12 01:02:03.123456789");
14+
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
15+
16+
String timestampAsString = formatter.format(timestamp.toLocalDateTime());
17+
Assert.assertEquals("2018-12-12T01:02:03.123456789", timestampAsString);
18+
}
19+
}

‎kotlin-libraries/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@
8787
<artifactId>h2</artifactId>
8888
<version>${h2database.version}</version>
8989
</dependency>
90+
<!-- https://mvnrepository.com/artifact/io.arrow-kt/arrow-core -->
91+
<dependency>
92+
<groupId>io.arrow-kt</groupId>
93+
<artifactId>arrow-core</artifactId>
94+
<version>0.7.3</version>
95+
</dependency>
96+
9097
</dependencies>
9198

9299
<properties>

0 commit comments

Comments
 (0)