Skip to content

Commit

Permalink
Merge pull request #3 from eugenp/master
Browse files Browse the repository at this point in the history
sync with origin
  • Loading branch information
danielguedesb committed Jul 27, 2018
2 parents 2a773d6 + 7070f25 commit dab27a0
Show file tree
Hide file tree
Showing 157 changed files with 1,952 additions and 915 deletions.
3 changes: 3 additions & 0 deletions apache-meecrowave/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Relevant Articles:
================================
- [Building a Microservice with Apache Meecrowave](http://www.baeldung.com/apache-meecrowave)
3 changes: 2 additions & 1 deletion core-java-8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
- [Overview of Java Built-in Annotations](http://www.baeldung.com/java-default-annotations)
- [Finding Min/Max in an Array with Java](http://www.baeldung.com/java-array-min-max)
- [Internationalization and Localization in Java 8](http://www.baeldung.com/java-8-localization)
- [Filtering Kotlin Collections](http://www.baeldung.com/kotlin-filter-collection)
- [How to Find an Element in a List with Java](http://www.baeldung.com/find-list-element-java)
- [Measure Elapsed Time in Java](http://www.baeldung.com/java-measure-elapsed-time)
- [Java Optional – orElse() vs orElseGet()](http://www.baeldung.com/java-optional-or-else-vs-or-else-get)
Expand All @@ -55,3 +54,5 @@
- [Java 8 Unsigned Arithmetic Support](http://www.baeldung.com/java-unsigned-arithmetic)
- [How to Get the Start and the End of a Day using Java](http://www.baeldung.com/java-day-start-end)
- [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference)
- [Image to Base64 String Conversion](http://www.baeldung.com/java-base64-image-string)
- [Calculate Age in Java](http://www.baeldung.com/java-get-age)
11 changes: 11 additions & 0 deletions core-java-8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@
<artifactId>joda-time</artifactId>
<version>${joda.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${asspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${asspectj.version}</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -170,6 +180,7 @@
<joda.version>2.10</joda.version>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
<asspectj.version>1.8.9</asspectj.version>
<avaitility.version>1.7.0</avaitility.version>
<jmh-core.version>1.19</jmh-core.version>
<jmh-generator.version>1.19</jmh-generator.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.baeldung.aspect;

public aspect ChangeCallsToCurrentTimeInMillisMethod {
long around():
call(public static native long java.lang.System.currentTimeMillis())
&& within(user.code.base.pckg.*) {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.baeldung.list;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.*;

import static org.junit.Assert.*;

public class AddElementsUnitTest {

List<Flower> flowers;

@Before
public void init() {
this.flowers = new ArrayList<>(Arrays.asList(
new Flower("Poppy", 12),
new Flower("Anemone", 8),
new Flower("Catmint", 12)));
}

@Test
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithNewItems() {
List<Flower> anotherList = new ArrayList<>();
anotherList.addAll(flowers);

assertEquals(anotherList.size(), flowers.size());
Assert.assertTrue(anotherList.containsAll(flowers));
}

@Test
public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithOneModifiedElementByConstructor() {
List<Flower> anotherList = new ArrayList<>();
anotherList.addAll(flowers);

Flower flower = anotherList.get(0);
flower.setPetals(flowers.get(0).getPetals() * 3);

assertEquals(anotherList.size(), flowers.size());
Assert.assertTrue(anotherList.containsAll(flowers));
}

@Test
public void givenAListAndElements_whenUseCollectionsAddAll_thenAddElementsToTargetList() {
List<Flower> target = new ArrayList<>();

Collections.addAll(target, flowers.get(0), flowers.get(1), flowers.get(2), flowers.get(0));

assertEquals(target.size(), 4);
}

@Test
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListSkipFirstElementByStreamProcess() {
List<Flower> flowerVase = new ArrayList<>();

flowers.stream()
.skip(1)
.forEachOrdered(flowerVase::add);

assertEquals(flowerVase.size() + 1, flowers.size());
assertFalse(flowerVase.containsAll(flowers));
}

@Test
public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListFilteringElementsByStreamProcess() {
List<Flower> flowerVase = new ArrayList<>();

flowers.stream()
.filter(f -> f.getPetals() > 10)
.forEachOrdered(flowerVase::add);

assertEquals(flowerVase.size() + 1, flowers.size());
assertFalse(flowerVase.containsAll(flowers));
}

@Test
public void givenAList_whenListIsNotNull_thenAddElementsToListByStreamProcessWihtOptional() {
List<Flower> target = new ArrayList<>();

Optional.ofNullable(flowers)
.ifPresent(target::addAll);

assertNotNull(target);
assertEquals(target.size(), 3);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.baeldung.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.time.Clock;
import java.time.Instant;
Expand All @@ -9,6 +10,8 @@
import java.time.ZoneId;
import java.time.temporal.ChronoField;

import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.junit.Test;

public class CurrentDateTimeUnitTest {
Expand Down Expand Up @@ -39,5 +42,4 @@ public void shouldReturnCurrentTimestamp() {

assertEquals(clock.instant().getEpochSecond(), now.getEpochSecond());
}

}
14 changes: 14 additions & 0 deletions core-java-9/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,23 @@
<version>${awaitility.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -50,6 +62,8 @@

<properties>
<!-- testing -->
<assertj.version>3.10.0</assertj.version>
<junit.platform.version>1.2.0</junit.platform.version>
<awaitility.version>1.7.0</awaitility.version>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.java9.language.stream;

import static java.util.stream.Collectors.filtering;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toList;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class StreamsGroupingCollectionFilter {

static public Map<Integer, List<Integer>> findEvenNumbersAfterGroupingByQuantityOfDigits(Collection<Integer> baseCollection) {
Function<Integer, Integer> getQuantityOfDigits = item -> (int) Math.log10(item) + 1;

return baseCollection.stream()
.collect(groupingBy(getQuantityOfDigits, filtering(item -> item % 2 == 0, toList())));
}

static public Map<Integer, List<Integer>> findEvenNumbersBeforeGroupingByQuantityOfDigits(Collection<Integer> baseCollection) {

return baseCollection.stream()
.filter(item -> item % 2 == 0)
.collect(groupingBy(item -> (int) Math.log10(item) + 1, toList()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.baeldung.java9.language.stream;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
public class CollectionFilterUnitTest {

private static final Collection<Integer> BASE_INTEGER_COLLECTION = Arrays.asList(9, 12, 55, 56, 101, 115, 8002, 223, 2668, 19, 8);
private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP = createExpectedFilterAfterGroupingMap();
private static Map<Integer, List<Integer>> createExpectedFilterAfterGroupingMap() {
Map<Integer, List<Integer>> map = new HashMap<>();
map.put(1, Arrays.asList(8));
map.put(2, Arrays.asList(12, 56));
map.put(3, Collections.emptyList());
map.put(4, Arrays.asList(8002, 2668));
return map;

}

private static final Map<Integer, List<Integer>> EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP = createExpectedFilterBeforeGroupingMap();
private static Map<Integer, List<Integer>> createExpectedFilterBeforeGroupingMap() {
Map<Integer, List<Integer>> map = new HashMap<>();
map.put(1, Arrays.asList(8));
map.put(2, Arrays.asList(12, 56));
map.put(4, Arrays.asList(8002, 2668));
return map;

}

@Test
public void givenAStringCollection_whenFilteringFourLetterWords_thenObtainTheFilteredCollection() {
Map<Integer, List<Integer>> filteredAfterGroupingMap = StreamsGroupingCollectionFilter.findEvenNumbersAfterGroupingByQuantityOfDigits(BASE_INTEGER_COLLECTION);
Map<Integer, List<Integer>> filteredBeforeGroupingMap = StreamsGroupingCollectionFilter.findEvenNumbersBeforeGroupingByQuantityOfDigits(BASE_INTEGER_COLLECTION);

assertThat(filteredAfterGroupingMap).containsAllEntriesOf(EXPECTED_EVEN_FILTERED_AFTER_GROUPING_MAP);
assertThat(filteredBeforeGroupingMap).doesNotContainKey(3)
.containsAllEntriesOf(EXPECTED_EVEN_FILTERED_BEFORE_GROUPING_MAP);
}

}
1 change: 1 addition & 0 deletions core-java-collections/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
- [How to TDD a List Implementation in Java](http://www.baeldung.com/java-test-driven-list)
- [How to Store Duplicate Keys in a Map in Java?](http://www.baeldung.com/java-map-duplicate-keys)
- [Getting the Size of an Iterable in Java](http://www.baeldung.com/java-iterable-size)
- [Iterating Backward Through a List](http://www.baeldung.com/java-list-iterate-backwards)
18 changes: 18 additions & 0 deletions core-java-collections/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,37 @@
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections-api</artifactId>
<version>${eclipse.collections.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections</artifactId>
<version>${eclipse.collections.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<junit.platform.version>1.2.0</junit.platform.version>
<commons-lang3.version>3.5</commons-lang3.version>
<commons-collections4.version>4.1</commons-collections4.version>
<collections-generic.version>4.01</collections-generic.version>
<avaitility.version>1.7.0</avaitility.version>
<assertj.version>3.6.1</assertj.version>
<eclipse.collections.version>9.2.0</eclipse.collections.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.baeldung.convertlisttomap;

import com.google.common.collect.Maps;
import org.apache.commons.collections4.MapUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections4.MapUtils;
import com.google.common.collect.Maps;

public class ConvertListToMapService {

public Map<Integer, Animal> convertListBeforeJava8(List<Animal> list) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.java.filtering;

import java.util.Collection;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;

public class CollectionUtilsCollectionFilter {

static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
Predicate<Integer> apacheEventNumberPredicate = item -> item % 2 == 0;

CollectionUtils.filter(baseCollection, apacheEventNumberPredicate);
return baseCollection;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.baeldung.java.filtering;

import java.util.Collection;

import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.utility.Iterate;

public class EclipseCollectionsCollectionFilter {

static public Collection<Integer> findEvenNumbers(Collection<Integer> baseCollection) {
Predicate<Integer> eclipsePredicate = item -> item % 2 == 0;
Collection<Integer> filteredList = Lists.mutable.ofAll(baseCollection)
.select(eclipsePredicate);

return filteredList;
}

static public Collection<Integer> findEvenNumbersUsingIterate(Collection<Integer> baseCollection) {
Predicate<Integer> eclipsePredicate = new Predicate<Integer>() {
private static final long serialVersionUID = 1L;

@Override
public boolean accept(Integer arg0) {
return arg0 % 2 == 0;
}
};
Collection<Integer> filteredList = Iterate.select(baseCollection, eclipsePredicate);

return filteredList;
}
}
Loading

0 comments on commit dab27a0

Please sign in to comment.