Generated
+60
-6
@@ -6,15 +6,69 @@
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="jmh" />
|
||||
<module name="assertion-libraries" />
|
||||
<module name="cucumber" />
|
||||
<module name="core-java-annotations" />
|
||||
<module name="jackson-simple" />
|
||||
<module name="spring-testing-2" />
|
||||
<module name="test-containers" />
|
||||
<module name="mockserver" />
|
||||
<module name="spring-testing" />
|
||||
<module name="xml" />
|
||||
<module name="core-java-datetime-conversion" />
|
||||
<module name="testing-libraries" />
|
||||
<module name="mocks" />
|
||||
<module name="load-testing-comparison" />
|
||||
<module name="junit-5-basics" />
|
||||
<module name="mockito-2" />
|
||||
<module name="testing-assertions" />
|
||||
<module name="easymock" />
|
||||
<module name="core-java-collections-list" />
|
||||
<module name="junit5-migration" />
|
||||
<module name="core-java-io" />
|
||||
<module name="easy-random" />
|
||||
<module name="junit-5" />
|
||||
<module name="junit-4" />
|
||||
<module name="junit-5-advanced" />
|
||||
<module name="rest-testing" />
|
||||
<module name="jackson-exceptions" />
|
||||
<module name="core-java-8" />
|
||||
<module name="jackson-conversions" />
|
||||
<module name="math-test-functions" />
|
||||
<module name="java-numbers-2" />
|
||||
<module name="jackson-annotations" />
|
||||
<module name="java-numbers" />
|
||||
<module name="mockito" />
|
||||
<module name="jackson" />
|
||||
<module name="groovy-spock" />
|
||||
<module name="jackson-conversions-2" />
|
||||
<module name="codebank" />
|
||||
<module name="selenium-junit-testng" />
|
||||
<module name="junit5-annotations" />
|
||||
<module name="mockito-3" />
|
||||
<module name="powermock" />
|
||||
<module name="zerocode" />
|
||||
<module name="gatling" />
|
||||
<module name="discourse" />
|
||||
<module name="rest-assured" />
|
||||
<module name="jackson-custom-conversions" />
|
||||
<module name="testing-libraries-2" />
|
||||
<module name="string-test-functions" />
|
||||
<module name="xmlunit-2" />
|
||||
<module name="testng" />
|
||||
<module name="hamcrest" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
<bytecodeTargetLevel>
|
||||
<module name="codebank" target="11" />
|
||||
<module name="discourse" target="11" />
|
||||
<module name="parent-modules" target="11" />
|
||||
<module name="toolkits" target="1.5" />
|
||||
</bytecodeTargetLevel>
|
||||
</component>
|
||||
<component name="JavacSettings">
|
||||
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
|
||||
<module name="codebank" options="" />
|
||||
<module name="discourse" options="" />
|
||||
<module name="java-numbers" options="" />
|
||||
<module name="java-numbers-2" options="" />
|
||||
<module name="jmh" options="--add-exports java.base/jdk.internal.vm.annotation=ALL-UNNAMED" />
|
||||
<module name="xml" options="" />
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+55
@@ -1,9 +1,64 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-8/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-8/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-annotations/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-annotations/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-collections-list/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-collections-list/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-datetime-conversion/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-io/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/core-java-io/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/core-java-modules/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-modules/jackson-annotations/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-modules/jackson-annotations/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-modules/jackson-conversions-2/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-modules/jackson-conversions-2/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-modules/jackson-conversions/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-modules/jackson-conversions/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-modules/jackson-custom-conversions/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-modules/jackson-custom-conversions/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-modules/jackson-exceptions/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-modules/jackson-exceptions/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-modules/jackson/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-modules/jackson/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-modules/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-modules/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-simple/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/jackson-simple/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/parent-java/java-numbers-2/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/parent-java/java-numbers-2/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/parent-java/java-numbers/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/parent-java/java-numbers/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/parent-java/jmh/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/parent-java/jmh/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/parent-java/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/parent-java/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/easy-random/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/easy-random/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/junit-5-basics/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/junit-5-basics/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/junit-5-basics/src/test/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/mockito-2/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/mockito-2/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/mockserver/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/mockserver/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/rest-testing/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/rest-testing/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/spring-testing/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/spring-testing/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/testing-modules/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/toolkits/codebank/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/toolkits/codebank/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/toolkits/discourse/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/toolkits/discourse/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/xml/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/xml/src/main/resources" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+5
@@ -6,6 +6,11 @@
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="m2-ossez-com-m2" />
|
||||
<option name="name" value="m2-ossez-com-m2" />
|
||||
<option name="url" value="https://m2-ossez-com-206755003006.d.codeartifact.us-east-2.amazonaws.com/maven/m2/" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
|
||||
Generated
+6
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="JpaBuddyIdeaProjectConfig">
|
||||
<option name="defaultUnitInitialized" value="true" />
|
||||
</component>
|
||||
</project>
|
||||
Generated
+1
@@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
|
||||
Generated
-11
@@ -1,11 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/toolkits/codebank/codebank.iml" filepath="$PROJECT_DIR$/toolkits/codebank/codebank.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/toolkits/discourse/discourse.iml" filepath="$PROJECT_DIR$/toolkits/discourse/discourse.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/parent-modules.iml" filepath="$PROJECT_DIR$/parent-modules.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/toolkits/toolkits.iml" filepath="$PROJECT_DIR$/toolkits/toolkits.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
Generated
+373
-23
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,26 @@
|
||||
*.class
|
||||
|
||||
0.*
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
.resourceCache
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
|
||||
# Files generated by integration tests
|
||||
*.txt
|
||||
backup-pom.xml
|
||||
/bin/
|
||||
/temp
|
||||
|
||||
#IntelliJ specific
|
||||
.idea/
|
||||
*.iml
|
||||
@@ -0,0 +1,15 @@
|
||||
## Core Java 8
|
||||
|
||||
This module contains articles about Java 8 core features
|
||||
|
||||
### Relevant Articles:
|
||||
- [New Features in Java 8](https://www.baeldung.com/java-8-new-features)
|
||||
- [Guide to Java 8 groupingBy Collector](https://www.baeldung.com/java-groupingby-collector)
|
||||
- [Strategy Design Pattern in Java 8](https://www.baeldung.com/java-strategy-pattern)
|
||||
- [Guide to Java 8 Comparator.comparing()](https://www.baeldung.com/java-8-comparator-comparing)
|
||||
- [Guide to the Java 8 forEach](https://www.baeldung.com/foreach-java)
|
||||
- [Introduction to Spliterator in Java](https://www.baeldung.com/java-spliterator)
|
||||
- [Finding Min/Max in an Array with Java](https://www.baeldung.com/java-array-min-max)
|
||||
- [Internationalization and Localization in Java 8](https://www.baeldung.com/java-8-localization)
|
||||
- [Generalized Target-Type Inference in Java](https://www.baeldung.com/java-generalized-target-type-inference)
|
||||
- [[More -->]](/core-java-modules/core-java-8-2)
|
||||
@@ -0,0 +1,55 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-8</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-8</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.ossez.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-8</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+9
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.ossez.java_8_features;
|
||||
|
||||
public class Address {
|
||||
|
||||
private String street;
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package com.ossez.java_8_features;
|
||||
|
||||
public class CustomException extends RuntimeException {
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.ossez.java_8_features;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class OptionalAddress {
|
||||
|
||||
private String street;
|
||||
|
||||
public Optional<String> getStreet() {
|
||||
return Optional.ofNullable(street);
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.ossez.java_8_features;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class OptionalUser {
|
||||
|
||||
private OptionalAddress address;
|
||||
|
||||
public Optional<OptionalAddress> getAddress() {
|
||||
return Optional.of(address);
|
||||
}
|
||||
|
||||
public void setAddress(OptionalAddress address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.ossez.java_8_features;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class User {
|
||||
|
||||
private String name;
|
||||
|
||||
private Address address;
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static boolean isRealUser(User user) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getOrThrow() {
|
||||
String value = null;
|
||||
Optional<String> valueOpt = Optional.ofNullable(value);
|
||||
String result = valueOpt.orElseThrow(CustomException::new).toUpperCase();
|
||||
return result;
|
||||
}
|
||||
|
||||
public boolean isLegalName(String name) {
|
||||
return name.length() > 3 && name.length() < 16;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.ossez.java_8_features;
|
||||
|
||||
public interface Vehicle {
|
||||
|
||||
void moveTo(long altitude, long longitude);
|
||||
|
||||
static String producer() {
|
||||
return "N&F Vehicles";
|
||||
}
|
||||
|
||||
default long[] startPosition() {
|
||||
return new long[] { 23, 15 };
|
||||
}
|
||||
|
||||
default String getOverview() {
|
||||
return "ATV made by " + producer();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.ossez.java_8_features;
|
||||
|
||||
public class VehicleImpl implements Vehicle {
|
||||
|
||||
@Override
|
||||
public void moveTo(long altitude, long longitude) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.ossez.java_8_features.groupingby;
|
||||
|
||||
public class BlogPost {
|
||||
private String title;
|
||||
private String author;
|
||||
private BlogPostType type;
|
||||
private int likes;
|
||||
|
||||
public BlogPost(String title, String author, BlogPostType type, int likes) {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.type = type;
|
||||
this.likes = likes;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public BlogPostType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public int getLikes() {
|
||||
return likes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BlogPost{" + "title='" + title + '\'' + ", type=" + type + ", likes=" + likes + '}';
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.ossez.java_8_features.groupingby;
|
||||
|
||||
public enum BlogPostType {
|
||||
NEWS, REVIEW, GUIDE
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.ossez.java_8_features.groupingby;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class Tuple {
|
||||
private final BlogPostType type;
|
||||
private final String author;
|
||||
|
||||
public Tuple(BlogPostType type, String author) {
|
||||
this.type = type;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public BlogPostType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
Tuple tuple = (Tuple) o;
|
||||
return type == tuple.type && author.equals(tuple.author);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(type, author);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Tuple{" + "type=" + type + ", author='" + author + '\'' + '}';
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.ossez.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Stream;
|
||||
import static org.apache.commons.collections4.CollectionUtils.emptyIfNull;
|
||||
|
||||
public class NullSafeCollectionStreamsUsingCommonsEmptyIfNull {
|
||||
|
||||
/**
|
||||
* This method shows how to make a null safe stream from a collection through the use of
|
||||
* emptyIfNull() method from Apache Commons CollectionUtils library
|
||||
*
|
||||
* @param collection The collection that is to be converted into a stream
|
||||
* @return The stream that has been created from the collection or an empty stream if the collection is null
|
||||
*/
|
||||
public Stream<String> collectionAsStream(Collection<String> collection) {
|
||||
return emptyIfNull(collection).stream();
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.ossez.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class NullSafeCollectionStreamsUsingJava8OptionalContainer {
|
||||
|
||||
/**
|
||||
* This method shows how to make a null safe stream from a collection through the use of
|
||||
* Java SE 8’s Optional Container
|
||||
*
|
||||
* @param collection The collection that is to be converted into a stream
|
||||
* @return The stream that has been created from the collection or an empty stream if the collection is null
|
||||
*/
|
||||
public Stream<String> collectionAsStream(Collection<String> collection) {
|
||||
return Optional.ofNullable(collection)
|
||||
.map(Collection::stream)
|
||||
.orElseGet(Stream::empty);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.ossez.nullsafecollectionstreams;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class NullSafeCollectionStreamsUsingNullDereferenceCheck {
|
||||
|
||||
/**
|
||||
* This method shows how to make a null safe stream from a collection through the use of a check
|
||||
* to prevent null dereferences
|
||||
*
|
||||
* @param collection The collection that is to be converted into a stream
|
||||
* @return The stream that has been created from the collection or an empty stream if the collection is null
|
||||
*/
|
||||
public Stream<String> collectionAsStream(Collection<String> collection) {
|
||||
return collection == null ? Stream.empty() : collection.stream();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.ossez.spliteratorAPI;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Article {
|
||||
private List<Author> listOfAuthors;
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Article(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Article(List<Author> listOfAuthors, int id) {
|
||||
super();
|
||||
this.listOfAuthors = listOfAuthors;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<Author> getListOfAuthors() {
|
||||
return listOfAuthors;
|
||||
}
|
||||
|
||||
public void setListOfAuthors(List<Author> listOfAuthors) {
|
||||
this.listOfAuthors = listOfAuthors;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.ossez.spliteratorAPI;
|
||||
|
||||
public class Author {
|
||||
private String name;
|
||||
private int relatedArticleId;
|
||||
|
||||
public Author(String name, int relatedArticleId) {
|
||||
this.name = name;
|
||||
this.relatedArticleId = relatedArticleId;
|
||||
}
|
||||
|
||||
public int getRelatedArticleId() {
|
||||
return relatedArticleId;
|
||||
}
|
||||
|
||||
public void setRelatedArticleId(int relatedArticleId) {
|
||||
this.relatedArticleId = relatedArticleId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[name: " + name + ", relatedId: " + relatedArticleId + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.ossez.spliteratorAPI;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Executor {
|
||||
|
||||
public static int countAutors(Stream<Author> stream) {
|
||||
RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true),
|
||||
RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
|
||||
return wordCounter.getCounter();
|
||||
}
|
||||
|
||||
public static List<Article> generateElements() {
|
||||
return Stream.generate(() -> new Article("Java")).limit(35000).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.ossez.spliteratorAPI;
|
||||
|
||||
public class RelatedAuthorCounter {
|
||||
private final int counter;
|
||||
private final boolean isRelated;
|
||||
|
||||
public RelatedAuthorCounter(int counter, boolean isRelated) {
|
||||
this.counter = counter;
|
||||
this.isRelated = isRelated;
|
||||
}
|
||||
|
||||
public RelatedAuthorCounter accumulate(Author author) {
|
||||
if (author.getRelatedArticleId() == 0) {
|
||||
return isRelated ? this : new RelatedAuthorCounter(counter, true);
|
||||
} else {
|
||||
return isRelated ? new RelatedAuthorCounter(counter + 1, false) : this;
|
||||
}
|
||||
}
|
||||
|
||||
public RelatedAuthorCounter combine(RelatedAuthorCounter RelatedAuthorCounter) {
|
||||
return new RelatedAuthorCounter(counter + RelatedAuthorCounter.counter, RelatedAuthorCounter.isRelated);
|
||||
}
|
||||
|
||||
public int getCounter() {
|
||||
return counter;
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.ossez.spliteratorAPI;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Spliterator;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class RelatedAuthorSpliterator implements Spliterator<Author> {
|
||||
private final List<Author> list;
|
||||
AtomicInteger current = new AtomicInteger();
|
||||
|
||||
public RelatedAuthorSpliterator(List<Author> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tryAdvance(Consumer<? super Author> action) {
|
||||
|
||||
action.accept(list.get(current.getAndIncrement()));
|
||||
return current.get() < list.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Spliterator<Author> trySplit() {
|
||||
int currentSize = list.size() - current.get();
|
||||
if (currentSize < 10) {
|
||||
return null;
|
||||
}
|
||||
for (int splitPos = currentSize / 2 + current.intValue(); splitPos < list.size(); splitPos++) {
|
||||
if (list.get(splitPos).getRelatedArticleId() == 0) {
|
||||
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(list.subList(current.get(), splitPos));
|
||||
current.set(splitPos);
|
||||
return spliterator;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long estimateSize() {
|
||||
return list.size() - current.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int characteristics() {
|
||||
return CONCURRENT;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.ossez.spliteratorAPI;
|
||||
|
||||
import java.util.Spliterator;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class Task implements Callable<String> {
|
||||
private Spliterator<Article> spliterator;
|
||||
private final static String SUFFIX = "- published by Baeldung";
|
||||
|
||||
public Task(Spliterator<Article> spliterator) {
|
||||
this.spliterator = spliterator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String call() {
|
||||
int current = 0;
|
||||
while (spliterator.tryAdvance(article -> {
|
||||
article.setName(article.getName()
|
||||
.concat(SUFFIX));
|
||||
})) {
|
||||
current++;
|
||||
}
|
||||
;
|
||||
return Thread.currentThread()
|
||||
.getName() + ":" + current;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user