[JAVA-22519] Created new module libraries-stream and added 5 articles (#15403)
This commit is contained in:
@@ -9,10 +9,8 @@ Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-m
|
||||
|
||||
### Relevant articles
|
||||
- [Introduction to Caffeine](https://www.baeldung.com/java-caching-caffeine)
|
||||
- [Introduction to StreamEx](https://www.baeldung.com/streamex)
|
||||
- [A Docker Guide for Java](https://www.baeldung.com/docker-java-api)
|
||||
- [Introduction to Akka Actors in Java](https://www.baeldung.com/akka-actors-java)
|
||||
- [A Guide to Byte Buddy](https://www.baeldung.com/byte-buddy)
|
||||
- [Introduction to jOOL](https://www.baeldung.com/jool)
|
||||
- [Consumer Driven Contracts with Pact](https://www.baeldung.com/pact-junit-consumer-driven-contracts)
|
||||
- [Introduction to Atlassian Fugue](https://www.baeldung.com/java-fugue)
|
||||
|
||||
@@ -49,21 +49,6 @@
|
||||
<version>${typesafe-akka.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>one.util</groupId>
|
||||
<artifactId>streamex</artifactId>
|
||||
<version>${streamex.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.bytebuddy</groupId>
|
||||
<artifactId>byte-buddy</artifactId>
|
||||
<version>${byte-buddy.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.bytebuddy</groupId>
|
||||
<artifactId>byte-buddy-agent</artifactId>
|
||||
<version>${byte-buddy.version}</version>
|
||||
</dependency>
|
||||
<!--Java Docker API Client -->
|
||||
<dependency>
|
||||
<groupId>com.github.docker-java</groupId>
|
||||
@@ -139,7 +124,6 @@
|
||||
<spring.version>4.3.8.RELEASE</spring.version>
|
||||
<scala.version>2.12</scala.version>
|
||||
<typesafe-akka.version>2.5.11</typesafe-akka.version>
|
||||
<streamex.version>0.8.1</streamex.version>
|
||||
<docker.version>3.0.14</docker.version>
|
||||
<caffeine.version>3.1.8</caffeine.version>
|
||||
<findbugs.version>3.0.2</findbugs.version>
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.bytebuddy;
|
||||
|
||||
import net.bytebuddy.implementation.bind.annotation.BindingPriority;
|
||||
|
||||
public class Bar {
|
||||
|
||||
@BindingPriority(3)
|
||||
public static String sayHelloBar() {
|
||||
return "Holla in Bar!";
|
||||
}
|
||||
|
||||
@BindingPriority(2)
|
||||
public static String sayBar() {
|
||||
return "bar";
|
||||
}
|
||||
|
||||
public String bar() {
|
||||
return Bar.class.getSimpleName() + " - Bar";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.baeldung.bytebuddy;
|
||||
|
||||
public class Foo {
|
||||
|
||||
public String sayHelloFoo() {
|
||||
return "Hello in Foo!";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.baeldung.streamex;
|
||||
|
||||
public class Role {
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package com.baeldung.streamex;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import one.util.streamex.DoubleStreamEx;
|
||||
import one.util.streamex.EntryStream;
|
||||
import one.util.streamex.IntStreamEx;
|
||||
import one.util.streamex.StreamEx;
|
||||
|
||||
public class StreamEX {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Collector shortcut methods (toList, toSet, groupingBy, joining, etc.)
|
||||
List<User> users = Arrays.asList(new User("name"), new User(), new User());
|
||||
users.stream().map(User::getName).collect(Collectors.toList());
|
||||
List<String> userNames = StreamEx.of(users).map(User::getName).toList();
|
||||
Map<Role, List<User>> role2users = StreamEx.of(users).groupingBy(User::getRole);
|
||||
StreamEx.of(1, 2, 3).joining("; "); // "1; 2; 3"
|
||||
// Selecting stream elements of specific type
|
||||
List usersAndRoles = Arrays.asList(new User(), new Role());
|
||||
List<Role> roles = IntStreamEx.range(usersAndRoles.size()).mapToObj(usersAndRoles::get).select(Role.class).toList();
|
||||
System.out.println(roles);
|
||||
// adding elements to Stream
|
||||
List<String> appendedUsers = StreamEx.of(users).map(User::getName).prepend("(none)").append("LAST").toList();
|
||||
System.out.println(appendedUsers);
|
||||
// Removing unwanted elements and using the stream as Iterable:
|
||||
for (String line : StreamEx.of(users).map(User::getName).nonNull()) {
|
||||
System.out.println(line);
|
||||
}
|
||||
// Selecting map keys by value predicate:
|
||||
Map<String, Role> nameToRole = new HashMap<>();
|
||||
nameToRole.put("first", new Role());
|
||||
nameToRole.put("second", null);
|
||||
Set<String> nonNullRoles = StreamEx.ofKeys(nameToRole, Objects::nonNull).toSet();
|
||||
System.out.println(nonNullRoles);
|
||||
// Operating on key-value pairs:
|
||||
Map<User, List<Role>> users2roles = transformMap(role2users);
|
||||
Map<String, String> mapToString = EntryStream.of(users2roles).mapKeys(String::valueOf).mapValues(String::valueOf).toMap();
|
||||
// Support of byte/char/short/float types:
|
||||
short[] src = { 1, 2, 3 };
|
||||
char[] output = IntStreamEx.of(src).map(x -> x * 5).toCharArray();
|
||||
}
|
||||
|
||||
public double[] getDiffBetweenPairs(double... numbers) {
|
||||
return DoubleStreamEx.of(numbers).pairMap((a, b) -> b - a).toArray();
|
||||
}
|
||||
|
||||
public static Map<User, List<Role>> transformMap(Map<Role, List<User>> role2users) {
|
||||
Map<User, List<Role>> users2roles = EntryStream.of(role2users).flatMapValues(List::stream).invert().grouping();
|
||||
return users2roles;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.baeldung.streamex;
|
||||
|
||||
public class User {
|
||||
|
||||
int id;
|
||||
String name;
|
||||
Role role = new Role();
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Role getRole() {
|
||||
return role;
|
||||
}
|
||||
|
||||
public void setRole(Role role) {
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
package com.baeldung.bytebuddy;
|
||||
|
||||
import net.bytebuddy.ByteBuddy;
|
||||
import net.bytebuddy.agent.ByteBuddyAgent;
|
||||
import net.bytebuddy.dynamic.DynamicType;
|
||||
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
|
||||
import net.bytebuddy.dynamic.loading.ClassReloadingStrategy;
|
||||
import net.bytebuddy.implementation.FixedValue;
|
||||
import net.bytebuddy.implementation.MethodDelegation;
|
||||
import net.bytebuddy.matcher.ElementMatchers;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
|
||||
import static net.bytebuddy.matcher.ElementMatchers.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class ByteBuddyUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenObject_whenToString_thenReturnHelloWorldString() throws InstantiationException, IllegalAccessException {
|
||||
DynamicType.Unloaded unloadedType = new ByteBuddy().subclass(Object.class).method(ElementMatchers.isToString()).intercept(FixedValue.value("Hello World ByteBuddy!")).make();
|
||||
|
||||
Class<?> dynamicType = unloadedType.load(getClass().getClassLoader()).getLoaded();
|
||||
|
||||
assertEquals(dynamicType.newInstance().toString(), "Hello World ByteBuddy!");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFoo_whenRedefined_thenReturnFooRedefined() throws Exception {
|
||||
ByteBuddyAgent.install();
|
||||
new ByteBuddy().redefine(Foo.class).method(named("sayHelloFoo")).intercept(FixedValue.value("Hello Foo Redefined")).make().load(Foo.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent());
|
||||
Foo f = new Foo();
|
||||
assertEquals(f.sayHelloFoo(), "Hello Foo Redefined");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSayHelloFoo_whenMethodDelegation_thenSayHelloBar() throws IllegalAccessException, InstantiationException {
|
||||
|
||||
String r = new ByteBuddy().subclass(Foo.class).method(named("sayHelloFoo").and(isDeclaredBy(Foo.class).and(returns(String.class)))).intercept(MethodDelegation.to(Bar.class)).make().load(getClass().getClassLoader()).getLoaded().newInstance()
|
||||
.sayHelloFoo();
|
||||
|
||||
assertEquals(r, Bar.sayHelloBar());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMethodName_whenDefineMethod_thenCreateMethod() throws Exception {
|
||||
Class<?> type = new ByteBuddy().subclass(Object.class).name("MyClassName").defineMethod("custom", String.class, Modifier.PUBLIC).intercept(MethodDelegation.to(Bar.class)).defineField("x", String.class, Modifier.PUBLIC).make()
|
||||
.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
|
||||
|
||||
Method m = type.getDeclaredMethod("custom", null);
|
||||
|
||||
assertEquals(m.invoke(type.newInstance()), Bar.sayHelloBar());
|
||||
assertNotNull(type.getDeclaredField("x"));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package com.baeldung.streamex;
|
||||
|
||||
import one.util.streamex.StreamEx;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class StreamExMergeStreamsUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoStreams_whenMergingStreams_thenResultingStreamContainsElementsFromBothStreams() {
|
||||
Stream<Integer> stream1 = Stream.of(1, 3, 5);
|
||||
Stream<Integer> stream2 = Stream.of(2, 4, 6);
|
||||
|
||||
Stream<Integer> resultingStream = StreamEx.of(stream1).append(stream2);
|
||||
|
||||
assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6), resultingStream.collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFourStreams_whenMergingStreams_thenResultingStreamContainsElementsFromAllStreams() {
|
||||
Stream<Integer> stream1 = Stream.of(1, 3, 5);
|
||||
Stream<Integer> stream2 = Stream.of(2, 4, 6);
|
||||
Stream<Integer> stream3 = Stream.of(18, 15, 36);
|
||||
Stream<Integer> stream4 = Stream.of(99);
|
||||
|
||||
Stream<Integer> resultingStream = StreamEx.of(stream1).append(stream2).append(stream3).append(stream4);
|
||||
|
||||
assertEquals(Arrays.asList(1, 3, 5, 2, 4, 6, 18, 15, 36, 99), resultingStream.collect(Collectors.toList()));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenThreeStreams_whenAppendingAndPrependingStreams_thenResultingStreamContainsElementsFromAllStreams() {
|
||||
Stream<String> stream1 = Stream.of("foo", "bar");
|
||||
Stream<String> openingBracketStream = Stream.of("[");
|
||||
Stream<String> closingBracketStream = Stream.of("]");
|
||||
|
||||
Stream<String> resultingStream = StreamEx.of(stream1).append(closingBracketStream).prepend(openingBracketStream);
|
||||
|
||||
assertEquals(Arrays.asList("[", "foo", "bar", "]"), resultingStream.collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user