Skip to content

Commit e9ce7e4

Browse files
authored
Merge branch 'eugenp:master' into JAVA-20167_1
2 parents 5fac3cf + 167d296 commit e9ce7e4

File tree

17 files changed

+474
-24
lines changed

17 files changed

+474
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.baeldung.listandset.benchmark;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
import org.openjdk.jmh.infra.Blackhole;
5+
import org.openjdk.jmh.runner.Runner;
6+
import org.openjdk.jmh.runner.RunnerException;
7+
import org.openjdk.jmh.runner.options.Options;
8+
import org.openjdk.jmh.runner.options.OptionsBuilder;
9+
10+
import java.io.IOException;
11+
import java.util.ArrayList;
12+
import java.util.HashSet;
13+
import java.util.List;
14+
import java.util.Set;
15+
import java.util.concurrent.TimeUnit;
16+
17+
@BenchmarkMode(Mode.SingleShotTime)
18+
@Warmup(iterations = 3, time = 10, timeUnit = TimeUnit.MILLISECONDS)
19+
@Measurement(iterations = 3, time = 10, timeUnit = TimeUnit.MILLISECONDS)
20+
public class ListAndSetAddBenchmark {
21+
22+
public static void main(String[] args) throws IOException, RunnerException {
23+
Options opt = new OptionsBuilder()
24+
.include(ListAndSetAddBenchmark.class.getSimpleName())
25+
.forks(1)
26+
.addProfiler("gc")
27+
.build();
28+
new Runner(opt).run();
29+
30+
}
31+
32+
@Benchmark
33+
public void addElementToArrayList(Params param, Blackhole blackhole) {
34+
param.arrayList.clear();
35+
for (int i = 0; i < param.addNumber; i++) {
36+
blackhole.consume(param.arrayList.add(i));
37+
}
38+
}
39+
40+
@Benchmark
41+
public void addElementToHashSet(Params param, Blackhole blackhole) {
42+
param.hashSet.clear();
43+
for (int i = 0; i < param.addNumber; i++) {
44+
blackhole.consume(param.hashSet.add(i));
45+
}
46+
}
47+
48+
@State(Scope.Benchmark)
49+
public static class Params {
50+
public int addNumber = 10000000;
51+
52+
public List<Integer> arrayList = new ArrayList<>();
53+
public Set<Integer> hashSet = new HashSet<>();
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.listandset.benchmark;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
import org.openjdk.jmh.infra.Blackhole;
5+
import org.openjdk.jmh.runner.Runner;
6+
import org.openjdk.jmh.runner.RunnerException;
7+
import org.openjdk.jmh.runner.options.Options;
8+
import org.openjdk.jmh.runner.options.OptionsBuilder;
9+
10+
import java.io.IOException;
11+
import java.util.ArrayList;
12+
import java.util.HashSet;
13+
import java.util.List;
14+
import java.util.Set;
15+
import java.util.concurrent.TimeUnit;
16+
17+
@BenchmarkMode(Mode.SingleShotTime)
18+
@Warmup(iterations = 3, time = 10, timeUnit = TimeUnit.MILLISECONDS)
19+
@Measurement(iterations = 3, time = 10, timeUnit = TimeUnit.MILLISECONDS)
20+
public class ListAndSetContainsBenchmark {
21+
22+
public static void main(String[] args) throws IOException, RunnerException {
23+
Options opt = new OptionsBuilder()
24+
.include(ListAndSetContainsBenchmark.class.getSimpleName())
25+
.forks(1)
26+
.addProfiler("gc")
27+
.build();
28+
new Runner(opt).run();
29+
30+
}
31+
32+
33+
34+
35+
@Benchmark
36+
public void searchElementInArrayList(Params param, Blackhole blackhole) {
37+
38+
blackhole.consume(param.arrayList.contains(param.searchElement));
39+
}
40+
41+
@Benchmark
42+
public void searchElementInHashSet(Params param, Blackhole blackhole) {
43+
44+
blackhole.consume(param.hashSet.contains(param.searchElement));
45+
46+
}
47+
48+
@State(Scope.Benchmark)
49+
public static class Params {
50+
@Param({"5000000"})
51+
public int searchElement;
52+
53+
@Param({"10000000"})
54+
public int collectionSize;
55+
56+
public List<Integer> arrayList;
57+
public Set<Integer> hashSet;
58+
59+
@Setup(Level.Iteration)
60+
public void setup() {
61+
arrayList = new ArrayList<>();
62+
hashSet = new HashSet<>();
63+
for (int i = 0; i < collectionSize; i++) {
64+
arrayList.add(i);
65+
hashSet.add(i);
66+
}
67+
}
68+
}
69+
70+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.listtojson;
2+
import com.fasterxml.jackson.core.JsonProcessingException;
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.google.gson.Gson;
5+
import org.json.JSONArray;
6+
import org.junit.Assert;
7+
import java.util.Arrays;
8+
import org.junit.Test;
9+
import java.util.List;
10+
11+
public class ListToJsonArrayUnitTest {
12+
public List<String> list = Arrays.asList("Article 1", "Article 2", "Article 3");
13+
public String expectedJsonArray = "[\"Article 1\",\"Article 2\",\"Article 3\"]";
14+
15+
@Test
16+
public void given_JavaList_whenUsingJacksonLibrary_thenOutJsonArray() throws JsonProcessingException {
17+
ObjectMapper objectMapper = new ObjectMapper();
18+
String jsonArray = objectMapper.writeValueAsString(list);
19+
Assert.assertEquals(expectedJsonArray, jsonArray);
20+
}
21+
22+
@Test
23+
public void given_JavaList_whenUsingGsonLibrary_thenOutJsonArray() {
24+
Gson gson = new Gson();
25+
String jsonArray = gson.toJson(list);
26+
Assert.assertEquals(expectedJsonArray, jsonArray);
27+
}
28+
29+
@Test
30+
public void given_JavaList_whenOrgJsonLibrary_thenOutJsonAray() {
31+
JSONArray jsonArray = new JSONArray(list);
32+
Assert.assertEquals(expectedJsonArray, jsonArray.toString());
33+
}
34+
35+
}

core-java-modules/core-java-string-operations-6/pom.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
66
<artifactId>core-java-string-operations-6</artifactId>
77
<name>core-java-string-operations-6</name>
@@ -12,6 +12,14 @@
1212
<artifactId>core-java-modules</artifactId>
1313
<version>0.0.1-SNAPSHOT</version>
1414
</parent>
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.apache.commons</groupId>
18+
<artifactId>commons-lang3</artifactId>
19+
<version>${apache.commons-lang.version}</version>
20+
</dependency>
21+
22+
</dependencies>
1523

1624
<build>
1725
<plugins>
@@ -29,6 +37,7 @@
2937
<properties>
3038
<maven.compiler.source>11</maven.compiler.source>
3139
<maven.compiler.target>11</maven.compiler.target>
40+
<apache.commons-lang.version>3.12.0</apache.commons-lang.version>
3241
</properties>
3342

3443
</project>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.baeldung.checkcase;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.apache.commons.lang3.StringUtils;
7+
import org.junit.jupiter.api.Test;
8+
9+
class CaseChecker {
10+
11+
static boolean allUpper1(String input) {
12+
return input.equals(input.toUpperCase());
13+
}
14+
15+
static boolean allLower1(String input) {
16+
return input.equals(input.toLowerCase());
17+
}
18+
19+
static boolean allUpper2(String input) {
20+
for (char c : input.toCharArray()) {
21+
// don't write in this way: if (!Character.isUpperCase(c))
22+
if (Character.isLowerCase(c)) {
23+
return false;
24+
}
25+
}
26+
return true;
27+
}
28+
29+
static boolean allLower2(String input) {
30+
for (char c : input.toCharArray()) {
31+
// don't write in this way: if (!Character.isLowerCase(c))
32+
if (Character.isUpperCase(c)) {
33+
return false;
34+
}
35+
}
36+
return true;
37+
}
38+
39+
static boolean allUpper3(String input) {
40+
return input.chars()
41+
.noneMatch(Character::isLowerCase);
42+
}
43+
44+
static boolean allLower3(String input) {
45+
return input.chars()
46+
.noneMatch(Character::isUpperCase);
47+
}
48+
}
49+
50+
public class StringAllUpperOrLowercaseUnitTest {
51+
private static final String UPPER_INPUT = "1: COOL!";
52+
private static final String LOWER_INPUT = "2: cool!";
53+
private static final String MIXED_INPUT = "3: Cool!";
54+
55+
@Test
56+
void whenComparingToConvertedString_thenGetExpectedResult() {
57+
assertTrue(CaseChecker.allLower1(LOWER_INPUT));
58+
assertFalse(CaseChecker.allLower1(UPPER_INPUT));
59+
assertFalse(CaseChecker.allLower1(MIXED_INPUT));
60+
61+
assertFalse(CaseChecker.allUpper1(LOWER_INPUT));
62+
assertTrue(CaseChecker.allUpper1(UPPER_INPUT));
63+
assertFalse(CaseChecker.allUpper1(MIXED_INPUT));
64+
}
65+
66+
@Test
67+
void whenCheckCharInArray_thenGetExpectedResult() {
68+
assertTrue(CaseChecker.allLower2(LOWER_INPUT));
69+
assertFalse(CaseChecker.allLower2(UPPER_INPUT));
70+
assertFalse(CaseChecker.allLower2(MIXED_INPUT));
71+
72+
assertFalse(CaseChecker.allUpper2(LOWER_INPUT));
73+
assertTrue(CaseChecker.allUpper2(UPPER_INPUT));
74+
assertFalse(CaseChecker.allUpper2(MIXED_INPUT));
75+
}
76+
77+
@Test
78+
void whenUsingStream_thenGetExpectedResult() {
79+
assertTrue(CaseChecker.allLower3(LOWER_INPUT));
80+
assertFalse(CaseChecker.allLower3(UPPER_INPUT));
81+
assertFalse(CaseChecker.allLower3(MIXED_INPUT));
82+
83+
assertFalse(CaseChecker.allUpper3(LOWER_INPUT));
84+
assertTrue(CaseChecker.allUpper3(UPPER_INPUT));
85+
assertFalse(CaseChecker.allUpper3(MIXED_INPUT));
86+
}
87+
88+
@Test
89+
void whenUsingApacheCommons_thenGetExpectedResult() {
90+
assertFalse(StringUtils.isAllLowerCase(LOWER_INPUT));
91+
assertFalse(StringUtils.isAllLowerCase(UPPER_INPUT));
92+
assertFalse(StringUtils.isAllLowerCase(MIXED_INPUT));
93+
94+
assertFalse(StringUtils.isAllLowerCase("a b"));
95+
assertTrue(StringUtils.isAllLowerCase("ab"));
96+
97+
assertFalse(StringUtils.isAllUpperCase(LOWER_INPUT));
98+
assertFalse(StringUtils.isAllUpperCase(UPPER_INPUT));
99+
assertFalse(StringUtils.isAllUpperCase(MIXED_INPUT));
100+
101+
assertFalse(StringUtils.isAllUpperCase("A B"));
102+
assertTrue(StringUtils.isAllUpperCase("AB"));
103+
}
104+
}

persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/data/Citizen.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ public class Citizen {
1212
public Citizen() {
1313
}
1414

15-
public Citizen(EncryptedCitizen encryptedCitizen) {
16-
if (encryptedCitizen != null) {
17-
this.name = encryptedCitizen.getName();
18-
}
15+
public Citizen(String name) {
16+
this.name = name;
1917
}
2018

2119
public String getName() {

persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/data/EncryptedCitizen.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class EncryptedCitizen {
1313
public EncryptedCitizen() {
1414
}
1515

16-
public EncryptedCitizen(Citizen citizen) {
17-
this.name = citizen.getName();
16+
public EncryptedCitizen(String name) {
17+
this.name = name;
1818
}
1919

2020
public String getName() {

persistence-modules/spring-boot-persistence-mongodb-3/src/main/java/com/baeldung/boot/csfle/service/CitizenService.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Object save(Citizen citizen) {
3939
if (encryptionConfig.isAutoEncryption()) {
4040
return mongo.save(citizen);
4141
} else {
42-
EncryptedCitizen encryptedCitizen = new EncryptedCitizen(citizen);
42+
EncryptedCitizen encryptedCitizen = new EncryptedCitizen(citizen.getName());
4343
encryptedCitizen.setEmail(encrypt(citizen.getEmail(), DETERMINISTIC_ALGORITHM));
4444
encryptedCitizen.setBirthYear(encrypt(citizen.getBirthYear(), RANDOM_ALGORITHM));
4545

@@ -77,26 +77,31 @@ public Citizen findByEmail(String email) {
7777
}
7878
}
7979

80-
public Binary encrypt(Object value, String algorithm) {
81-
if (value == null)
80+
public Binary encrypt(BsonValue bsonValue, String algorithm) {
81+
if (bsonValue == null)
8282
return null;
8383

84-
BsonValue bsonValue;
85-
if (value instanceof Integer) {
86-
bsonValue = new BsonInt32((Integer) value);
87-
} else if (value instanceof String) {
88-
bsonValue = new BsonString((String) value);
89-
} else {
90-
throw new IllegalArgumentException("unsupported type: " + value.getClass());
91-
}
92-
9384
EncryptOptions options = new EncryptOptions(algorithm);
9485
options.keyId(encryptionConfig.getDataKeyId());
9586

9687
BsonBinary encryptedValue = clientEncryption.encrypt(bsonValue, options);
9788
return new Binary(encryptedValue.getType(), encryptedValue.getData());
9889
}
9990

91+
public Binary encrypt(String value, String algorithm) {
92+
if (value == null)
93+
return null;
94+
95+
return encrypt(new BsonString(value), algorithm);
96+
}
97+
98+
public Binary encrypt(Integer value, String algorithm) {
99+
if (value == null)
100+
return null;
101+
102+
return encrypt(new BsonInt32(value), algorithm);
103+
}
104+
100105
public BsonValue decryptProperty(Binary value) {
101106
if (value == null)
102107
return null;
@@ -108,7 +113,7 @@ private Citizen decrypt(EncryptedCitizen encrypted) {
108113
if (encrypted == null)
109114
return null;
110115

111-
Citizen citizen = new Citizen(encrypted);
116+
Citizen citizen = new Citizen(encrypted.getName());
112117

113118
BsonValue decryptedBirthYear = decryptProperty(encrypted.getBirthYear());
114119
if (decryptedBirthYear != null) {

spring-web-modules/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<module>spring-mvc-forms-thymeleaf</module>
2929
<module>spring-mvc-java</module>
3030
<module>spring-mvc-java-2</module>
31+
<module>spring-mvc-java-3</module>
3132
<module>spring-mvc-velocity</module>
3233
<module>spring-mvc-views</module>
3334
<module>spring-mvc-webflow</module>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
### Relevant Articles:

0 commit comments

Comments
 (0)