Skip to content

Commit dab27a0

Browse files
Merge pull request eugenp#3 from eugenp/master
sync with origin
2 parents 2a773d6 + 7070f25 commit dab27a0

157 files changed

Lines changed: 1952 additions & 915 deletions

File tree

Some content is hidden

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

‎apache-meecrowave/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Relevant Articles:
2+
================================
3+
- [Building a Microservice with Apache Meecrowave](http://www.baeldung.com/apache-meecrowave)

‎core-java-8/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
- [Overview of Java Built-in Annotations](http://www.baeldung.com/java-default-annotations)
4747
- [Finding Min/Max in an Array with Java](http://www.baeldung.com/java-array-min-max)
4848
- [Internationalization and Localization in Java 8](http://www.baeldung.com/java-8-localization)
49-
- [Filtering Kotlin Collections](http://www.baeldung.com/kotlin-filter-collection)
5049
- [How to Find an Element in a List with Java](http://www.baeldung.com/find-list-element-java)
5150
- [Measure Elapsed Time in Java](http://www.baeldung.com/java-measure-elapsed-time)
5251
- [Java Optional – orElse() vs orElseGet()](http://www.baeldung.com/java-optional-or-else-vs-or-else-get)
@@ -55,3 +54,5 @@
5554
- [Java 8 Unsigned Arithmetic Support](http://www.baeldung.com/java-unsigned-arithmetic)
5655
- [How to Get the Start and the End of a Day using Java](http://www.baeldung.com/java-day-start-end)
5756
- [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference)
57+
- [Image to Base64 String Conversion](http://www.baeldung.com/java-base64-image-string)
58+
- [Calculate Age in Java](http://www.baeldung.com/java-get-age)

‎core-java-8/pom.xml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@
9999
<artifactId>joda-time</artifactId>
100100
<version>${joda.version}</version>
101101
</dependency>
102+
<dependency>
103+
<groupId>org.aspectj</groupId>
104+
<artifactId>aspectjrt</artifactId>
105+
<version>${asspectj.version}</version>
106+
</dependency>
107+
<dependency>
108+
<groupId>org.aspectj</groupId>
109+
<artifactId>aspectjweaver</artifactId>
110+
<version>${asspectj.version}</version>
111+
</dependency>
102112
</dependencies>
103113

104114
<build>
@@ -170,6 +180,7 @@
170180
<joda.version>2.10</joda.version>
171181
<!-- testing -->
172182
<assertj.version>3.6.1</assertj.version>
183+
<asspectj.version>1.8.9</asspectj.version>
173184
<avaitility.version>1.7.0</avaitility.version>
174185
<jmh-core.version>1.19</jmh-core.version>
175186
<jmh-generator.version>1.19</jmh-generator.version>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.aspect;
2+
3+
public aspect ChangeCallsToCurrentTimeInMillisMethod {
4+
long around():
5+
call(public static native long java.lang.System.currentTimeMillis())
6+
&& within(user.code.base.pckg.*) {
7+
return 0;
8+
}
9+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.baeldung.list;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.util.*;
8+
9+
import static org.junit.Assert.*;
10+
11+
public class AddElementsUnitTest {
12+
13+
List<Flower> flowers;
14+
15+
@Before
16+
public void init() {
17+
this.flowers = new ArrayList<>(Arrays.asList(
18+
new Flower("Poppy", 12),
19+
new Flower("Anemone", 8),
20+
new Flower("Catmint", 12)));
21+
}
22+
23+
@Test
24+
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithNewItems() {
25+
List<Flower> anotherList = new ArrayList<>();
26+
anotherList.addAll(flowers);
27+
28+
assertEquals(anotherList.size(), flowers.size());
29+
Assert.assertTrue(anotherList.containsAll(flowers));
30+
}
31+
32+
@Test
33+
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithOneModifiedElementByConstructor() {
34+
List<Flower> anotherList = new ArrayList<>();
35+
anotherList.addAll(flowers);
36+
37+
Flower flower = anotherList.get(0);
38+
flower.setPetals(flowers.get(0).getPetals() * 3);
39+
40+
assertEquals(anotherList.size(), flowers.size());
41+
Assert.assertTrue(anotherList.containsAll(flowers));
42+
}
43+
44+
@Test
45+
public void givenAListAndElements_whenUseCollectionsAddAll_thenAddElementsToTargetList() {
46+
List<Flower> target = new ArrayList<>();
47+
48+
Collections.addAll(target, flowers.get(0), flowers.get(1), flowers.get(2), flowers.get(0));
49+
50+
assertEquals(target.size(), 4);
51+
}
52+
53+
@Test
54+
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListSkipFirstElementByStreamProcess() {
55+
List<Flower> flowerVase = new ArrayList<>();
56+
57+
flowers.stream()
58+
.skip(1)
59+
.forEachOrdered(flowerVase::add);
60+
61+
assertEquals(flowerVase.size() + 1, flowers.size());
62+
assertFalse(flowerVase.containsAll(flowers));
63+
}
64+
65+
@Test
66+
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListFilteringElementsByStreamProcess() {
67+
List<Flower> flowerVase = new ArrayList<>();
68+
69+
flowers.stream()
70+
.filter(f -> f.getPetals() > 10)
71+
.forEachOrdered(flowerVase::add);
72+
73+
assertEquals(flowerVase.size() + 1, flowers.size());
74+
assertFalse(flowerVase.containsAll(flowers));
75+
}
76+
77+
@Test
78+
public void givenAList_whenListIsNotNull_thenAddElementsToListByStreamProcessWihtOptional() {
79+
List<Flower> target = new ArrayList<>();
80+
81+
Optional.ofNullable(flowers)
82+
.ifPresent(target::addAll);
83+
84+
assertNotNull(target);
85+
assertEquals(target.size(), 3);
86+
}
87+
}

‎core-java-8/src/test/java/com/baeldung/util/CurrentDateTimeUnitTest.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.baeldung.util;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
45

56
import java.time.Clock;
67
import java.time.Instant;
@@ -9,6 +10,8 @@
910
import java.time.ZoneId;
1011
import java.time.temporal.ChronoField;
1112

13+
import org.joda.time.DateTime;
14+
import org.joda.time.DateTimeUtils;
1215
import org.junit.Test;
1316

1417
public class CurrentDateTimeUnitTest {
@@ -39,5 +42,4 @@ public void shouldReturnCurrentTimestamp() {
3942

4043
assertEquals(clock.instant().getEpochSecond(), now.getEpochSecond());
4144
}
42-
4345
}

‎core-java-9/pom.xml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,23 @@
1919
<version>${awaitility.version}</version>
2020
<scope>test</scope>
2121
</dependency>
22+
<dependency>
23+
<groupId>org.assertj</groupId>
24+
<artifactId>assertj-core</artifactId>
25+
<version>${assertj.version}</version>
26+
<scope>test</scope>
27+
</dependency>
2228
<dependency>
2329
<groupId>com.google.guava</groupId>
2430
<artifactId>guava</artifactId>
2531
<version>${guava.version}</version>
2632
</dependency>
33+
<dependency>
34+
<groupId>org.junit.platform</groupId>
35+
<artifactId>junit-platform-runner</artifactId>
36+
<version>${junit.platform.version}</version>
37+
<scope>test</scope>
38+
</dependency>
2739
</dependencies>
2840

2941
<build>
@@ -50,6 +62,8 @@
5062

5163
<properties>
5264
<!-- testing -->
65+
<assertj.version>3.10.0</assertj.version>
66+
<junit.platform.version>1.2.0</junit.platform.version>
5367
<awaitility.version>1.7.0</awaitility.version>
5468
<maven.compiler.source>1.9</maven.compiler.source>
5569
<maven.compiler.target>1.9</maven.compiler.target>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.java9.language.stream;
2+
3+
import static java.util.stream.Collectors.filtering;
4+
import static java.util.stream.Collectors.groupingBy;
5+
import static java.util.stream.Collectors.toList;
6+
7+
import java.util.Collection;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.function.Function;
11+
12+
public class StreamsGroupingCollectionFilter {
13+
14+
static public Map<Integer, List<Integer>> findEvenNumbersAfterGroupingByQuantityOfDigits(Collection<Integer> baseCollection) {
15+
Function<Integer, Integer> getQuantityOfDigits = item -> (int) Math.log10(item) + 1;
16+
17+
return baseCollection.stream()
18+
.collect(groupingBy(getQuantityOfDigits, filtering(item -> item % 2 == 0, toList())));
19+
}
20+
21+
static public Map<Integer, List<Integer>> findEvenNumbersBeforeGroupingByQuantityOfDigits(Collection<Integer> baseCollection) {
22+
23+
return baseCollection.stream()
24+
.filter(item -> item % 2 == 0)
25+
.collect(groupingBy(item -> (int) Math.log10(item) + 1, toList()));
26+
}
27+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.java9.language.stream;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.Arrays;
6+
import java.util.Collection;
7+
import java.util.Collections;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.platform.runner.JUnitPlatform;
14+
import org.junit.runner.RunWith;
15+
16+
@RunWith(JUnitPlatform.class)
17+
public class CollectionFilterUnitTest {
18+
19+
private static final Collection<Integer> BASE_INTEGER_COLLECTION = Arrays.asList(9, 12, 55, 56, 101, 115, 8002, 223, 2668, 19, 8);
20+
private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP = createExpectedFilterAfterGroupingMap();
21+
private static Map<Integer, List<Integer>> createExpectedFilterAfterGroupingMap() {
22+
Map<Integer, List<Integer>> map = new HashMap<>();
23+
map.put(1, Arrays.asList(8));
24+
map.put(2, Arrays.asList(12, 56));
25+
map.put(3, Collections.emptyList());
26+
map.put(4, Arrays.asList(8002, 2668));
27+
return map;
28+
29+
}
30+
31+
private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP = createExpectedFilterBeforeGroupingMap();
32+
private static Map<Integer, List<Integer>> createExpectedFilterBeforeGroupingMap() {
33+
Map<Integer, List<Integer>> map = new HashMap<>();
34+
map.put(1, Arrays.asList(8));
35+
map.put(2, Arrays.asList(12, 56));
36+
map.put(4, Arrays.asList(8002, 2668));
37+
return map;
38+
39+
}
40+
41+
@Test
42+
public void givenAStringCollection_whenFilteringFourLetterWords_thenObtainTheFilteredCollection() {
43+
Map<Integer, List<Integer>> filteredAfterGroupingMap = StreamsGroupingCollectionFilter.findEvenNumbersAfterGroupingByQuantityOfDigits(BASE_INTEGER_COLLECTION);
44+
Map<Integer, List<Integer>> filteredBeforeGroupingMap = StreamsGroupingCollectionFilter.findEvenNumbersBeforeGroupingByQuantityOfDigits(BASE_INTEGER_COLLECTION);
45+
46+
assertThat(filteredAfterGroupingMap).containsAllEntriesOf(EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP);
47+
assertThat(filteredBeforeGroupingMap).doesNotContainKey(3)
48+
.containsAllEntriesOf(EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP);
49+
}
50+
51+
}

‎core-java-collections/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@
3030
- [How to TDD a List Implementation in Java](http://www.baeldung.com/java-test-driven-list)
3131
- [How to Store Duplicate Keys in a Map in Java?](http://www.baeldung.com/java-map-duplicate-keys)
3232
- [Getting the Size of an Iterable in Java](http://www.baeldung.com/java-iterable-size)
33+
- [Iterating Backward Through a List](http://www.baeldung.com/java-list-iterate-backwards)

0 commit comments

Comments
 (0)