merge
This commit is contained in:
+2
-1
@@ -27,4 +27,5 @@ target/
|
||||
|
||||
spring-openid/src/main/resources/application.properties
|
||||
.recommenders/
|
||||
/spring-hibernate4/nbproject/
|
||||
/spring-hibernate4/nbproject/
|
||||
spring-security-openid/src/main/resources/application.properties
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,68 @@
|
||||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-thrift</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<properties>
|
||||
<java.versin>1.8</java.versin>
|
||||
<junit.version>4.12</junit.version>
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<thrift.version>0.10.0</thrift.version>
|
||||
<maven-thrift.version>0.1.11</maven-thrift.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.thrift</groupId>
|
||||
<artifactId>libthrift</artifactId>
|
||||
<version>${thrift.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.7.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<defaultGoal>install</defaultGoal>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>build-helper-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>generate-sources</phase>
|
||||
<goals><goal>add-source</goal></goals>
|
||||
<configuration>
|
||||
<sources>
|
||||
<source>generated</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) throws TTransportException {
|
||||
CrossPlatformServiceServer server = new CrossPlatformServiceServer();
|
||||
server.start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
import com.baeldung.thrift.impl.CrossPlatformService;
|
||||
|
||||
import org.apache.thrift.TException;
|
||||
import org.apache.thrift.protocol.TBinaryProtocol;
|
||||
import org.apache.thrift.protocol.TProtocol;
|
||||
import org.apache.thrift.transport.TSocket;
|
||||
import org.apache.thrift.transport.TTransport;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
|
||||
public class CrossPlatformServiceClient {
|
||||
|
||||
public boolean ping() {
|
||||
try {
|
||||
TTransport transport;
|
||||
|
||||
transport = new TSocket("localhost", 9090);
|
||||
transport.open();
|
||||
|
||||
TProtocol protocol = new TBinaryProtocol(transport);
|
||||
CrossPlatformService.Client client = new CrossPlatformService.Client(protocol);
|
||||
|
||||
System.out.print("Calling remote method...");
|
||||
|
||||
boolean result = client.ping();
|
||||
|
||||
System.out.println("done.");
|
||||
|
||||
transport.close();
|
||||
|
||||
return result;
|
||||
} catch (TTransportException e) {
|
||||
e.printStackTrace();
|
||||
} catch (TException x) {
|
||||
x.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
import com.baeldung.thrift.impl.CrossPlatformResource;
|
||||
import com.baeldung.thrift.impl.CrossPlatformService;
|
||||
import com.baeldung.thrift.impl.InvalidOperationException;
|
||||
|
||||
import org.apache.thrift.TException;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class CrossPlatformServiceImpl implements CrossPlatformService.Iface {
|
||||
|
||||
@Override
|
||||
public CrossPlatformResource get(final int id) throws InvalidOperationException, TException {
|
||||
// add some action
|
||||
return new CrossPlatformResource();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(final CrossPlatformResource resource) throws InvalidOperationException, TException {
|
||||
// add some action
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CrossPlatformResource> getList() throws InvalidOperationException, TException {
|
||||
// add some action
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ping() throws InvalidOperationException, TException {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
import com.baeldung.thrift.impl.CrossPlatformService;
|
||||
|
||||
import org.apache.thrift.server.TServer;
|
||||
import org.apache.thrift.server.TSimpleServer;
|
||||
import org.apache.thrift.transport.TServerSocket;
|
||||
import org.apache.thrift.transport.TServerTransport;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
|
||||
public class CrossPlatformServiceServer {
|
||||
|
||||
private TServer server;
|
||||
|
||||
public void start() throws TTransportException {
|
||||
TServerTransport serverTransport = new TServerSocket(9090);
|
||||
server = new TSimpleServer(new TServer.Args(serverTransport)
|
||||
.processor(new CrossPlatformService.Processor<>(new CrossPlatformServiceImpl())));
|
||||
|
||||
System.out.print("Starting the server... ");
|
||||
|
||||
server.serve();
|
||||
|
||||
System.out.println("done.");
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
if (server != null && server.isServing()) {
|
||||
System.out.print("Stopping the server... ");
|
||||
|
||||
server.stop();
|
||||
|
||||
System.out.println("done.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
namespace cpp com.baeldung.thrift.impl
|
||||
namespace java com.baeldung.thrift.impl
|
||||
|
||||
exception InvalidOperationException {
|
||||
1: i32 code,
|
||||
2: string description
|
||||
}
|
||||
|
||||
struct CrossPlatformResource {
|
||||
1: i32 id,
|
||||
2: string name,
|
||||
3: optional string salutation
|
||||
}
|
||||
|
||||
service CrossPlatformService {
|
||||
|
||||
CrossPlatformResource get(1:i32 id) throws (1:InvalidOperationException e),
|
||||
|
||||
void save(1:CrossPlatformResource resource) throws (1:InvalidOperationException e),
|
||||
|
||||
list <CrossPlatformResource> getList() throws (1:InvalidOperationException e),
|
||||
|
||||
bool ping() throws (1:InvalidOperationException e)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.thrift;
|
||||
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CrossPlatformServiceTest {
|
||||
|
||||
private CrossPlatformServiceServer server = new CrossPlatformServiceServer();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
server.start();
|
||||
} catch (TTransportException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}).start();
|
||||
try {
|
||||
// wait for the server start up
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ping() {
|
||||
CrossPlatformServiceClient client = new CrossPlatformServiceClient();
|
||||
Assert.assertTrue(client.ping());
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -57,7 +57,6 @@
|
||||
<configuration>
|
||||
<source>1.9</source>
|
||||
<target>1.9</target>
|
||||
<verbose>true</verbose>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
@@ -72,6 +71,9 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- project build encoding -->
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
<!-- logging -->
|
||||
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||
|
||||
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.java9.language.collections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class ListFactoryMethodsTest {
|
||||
|
||||
@Test
|
||||
public void whenListCreated_thenSuccess() {
|
||||
List<String> traditionlList = new ArrayList<String>();
|
||||
traditionlList.add("foo");
|
||||
traditionlList.add("bar");
|
||||
traditionlList.add("baz");
|
||||
List<String> factoryCreatedList = List.of("foo", "bar", "baz");
|
||||
assertEquals(traditionlList, factoryCreatedList);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void onElemAdd_ifUnSupportedOpExpnThrown_thenSuccess() {
|
||||
List<String> list = List.of("foo", "bar");
|
||||
list.add("baz");
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void onElemModify_ifUnSupportedOpExpnThrown_thenSuccess() {
|
||||
List<String> list = List.of("foo", "bar");
|
||||
list.set(0, "baz");
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void onElemRemove_ifUnSupportedOpExpnThrown_thenSuccess() {
|
||||
List<String> list = List.of("foo", "bar");
|
||||
list.remove("foo");
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void onNullElem_ifNullPtrExpnThrown_thenSuccess() {
|
||||
List.of("foo", "bar", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifNotArrayList_thenSuccess() {
|
||||
List<String> list = List.of("foo", "bar");
|
||||
assertFalse(list instanceof ArrayList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifListSizeIsOne_thenSuccess() {
|
||||
int[] arr = { 1, 2, 3, 4 };
|
||||
List<int[]> list = List.of(arr);
|
||||
assertEquals(1, list.size());
|
||||
assertArrayEquals(arr, list.get(0));
|
||||
}
|
||||
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.java9.language.collections;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class MapFactoryMethodsTest {
|
||||
|
||||
@Test
|
||||
public void whenMapCreated_thenSuccess() {
|
||||
Map<String, String> traditionlMap = new HashMap<String, String>();
|
||||
traditionlMap.put("foo", "a");
|
||||
traditionlMap.put("bar", "b");
|
||||
traditionlMap.put("baz", "c");
|
||||
Map<String, String> factoryCreatedMap = Map.of("foo", "a", "bar", "b", "baz", "c");
|
||||
assertEquals(traditionlMap, factoryCreatedMap);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void onElemAdd_ifUnSupportedOpExpnThrown_thenSuccess() {
|
||||
Map<String, String> map = Map.of("foo", "a", "bar", "b");
|
||||
map.put("baz", "c");
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void onElemModify_ifUnSupportedOpExpnThrown_thenSuccess() {
|
||||
Map<String, String> map = Map.of("foo", "a", "bar", "b");
|
||||
map.put("foo", "c");
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void onElemRemove_ifUnSupportedOpExpnThrown_thenSuccess() {
|
||||
Map<String, String> map = Map.of("foo", "a", "bar", "b");
|
||||
map.remove("foo");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenDuplicateKeys_ifIllegalArgExp_thenSuccess() {
|
||||
Map.of("foo", "a", "foo", "b");
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void onNullKey_ifNullPtrExp_thenSuccess() {
|
||||
Map.of("foo", "a", null, "b");
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void onNullValue_ifNullPtrExp_thenSuccess() {
|
||||
Map.of("foo", "a", "bar", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifNotHashMap_thenSuccess() {
|
||||
Map<String, String> map = Map.of("foo", "a", "bar", "b");
|
||||
assertFalse(map instanceof HashMap);
|
||||
}
|
||||
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.java9.language.collections;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class SetFactoryMethodsTest {
|
||||
|
||||
@Test
|
||||
public void whenSetCreated_thenSuccess() {
|
||||
Set<String> traditionlSet = new HashSet<String>();
|
||||
traditionlSet.add("foo");
|
||||
traditionlSet.add("bar");
|
||||
traditionlSet.add("baz");
|
||||
Set<String> factoryCreatedSet = Set.of("foo", "bar", "baz");
|
||||
assertEquals(traditionlSet, factoryCreatedSet);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void onDuplicateElem_IfIllegalArgExp_thenSuccess() {
|
||||
Set.of("foo", "bar", "baz", "foo");
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void onElemAdd_ifUnSupportedOpExpnThrown_thenSuccess() {
|
||||
Set<String> set = Set.of("foo", "bar");
|
||||
set.add("baz");
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void onElemRemove_ifUnSupportedOpExpnThrown_thenSuccess() {
|
||||
Set<String> set = Set.of("foo", "bar", "baz");
|
||||
set.remove("foo");
|
||||
}
|
||||
|
||||
@Test(expected = NullPointerException.class)
|
||||
public void onNullElem_ifNullPtrExpnThrown_thenSuccess() {
|
||||
Set.of("foo", "bar", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifNotHashSet_thenSuccess() {
|
||||
Set<String> list = Set.of("foo", "bar");
|
||||
assertFalse(list instanceof HashSet);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ifSetSizeIsOne_thenSuccess() {
|
||||
int[] arr = { 1, 2, 3, 4 };
|
||||
Set<int[]> set = Set.of(arr);
|
||||
assertEquals(1, set.size());
|
||||
assertArrayEquals(arr, set.iterator().next());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -54,3 +54,6 @@
|
||||
- [The Basics of Java Generics](http://www.baeldung.com/java-generics)
|
||||
- [The Traveling Salesman Problem in Java](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman)
|
||||
- [How to Create an Executable JAR with Maven](http://www.baeldung.com/executable-jar-with-maven)
|
||||
- [Guide to java.util.concurrent.Future](http://www.baeldung.com/java-future)
|
||||
- [Guide to java.util.concurrent.BlockingQueue](http://www.baeldung.com/java-blocking-queue)
|
||||
- [Guide to CountDownLatch in Java](http://www.baeldung.com/java-countdown-latch)
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.baeldung;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class CharArrayToStringUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenCharArray_whenCallingStringConstructor_shouldConvertToString() {
|
||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
||||
String result = new String(charArray);
|
||||
String expectedValue = "character";
|
||||
|
||||
assertEquals(expectedValue, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCharArray_whenCallingStringConstructorWithOffsetAndLength_shouldConvertToString(){
|
||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
||||
String result = new String(charArray, 4, 3);
|
||||
String expectedValue = "act";
|
||||
|
||||
assertEquals(expectedValue, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCharArray_whenCallingStringCopyValueOf_shouldConvertToString(){
|
||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
||||
String result = String.copyValueOf(charArray);
|
||||
String expectedValue = "character";
|
||||
|
||||
assertEquals(expectedValue, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCharArray_whenCallingStringCopyValueOfWithOffsetAndLength_shouldConvertToString(){
|
||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
||||
String result = String.copyValueOf(charArray, 0, 4);
|
||||
String expectedValue = "char";
|
||||
|
||||
assertEquals(expectedValue, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCharArray_whenCallingStringValueOf_shouldConvertToString(){
|
||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
||||
String result = String.valueOf(charArray);
|
||||
String expectedValue = "character";
|
||||
|
||||
assertEquals(expectedValue, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCharArray_whenCallingStringValueOfWithOffsetAndLength_shouldConvertToString(){
|
||||
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
|
||||
String result = String.valueOf(charArray, 3, 4);
|
||||
String expectedValue = "ract";
|
||||
|
||||
assertEquals(expectedValue, result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringToCharArrayUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenString_whenCallingStringToCharArray_shouldConvertToCharArray() {
|
||||
String givenString = "characters";
|
||||
|
||||
char[] result = givenString.toCharArray();
|
||||
|
||||
char[] expectedCharArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's' };
|
||||
|
||||
assertArrayEquals(expectedCharArray, result);
|
||||
}
|
||||
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.concurrent.priorityblockingqueue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.PriorityBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.util.Lists.newArrayList;
|
||||
|
||||
public class PriorityBlockingQueueUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUnorderedValues_whenPolling_thenShouldOrderQueue() throws InterruptedException {
|
||||
PriorityBlockingQueue<Integer> queue = new PriorityBlockingQueue<>();
|
||||
ArrayList<Integer> polledElements = new ArrayList<>();
|
||||
|
||||
queue.add(1);
|
||||
queue.add(5);
|
||||
queue.add(2);
|
||||
queue.add(3);
|
||||
queue.add(4);
|
||||
|
||||
queue.drainTo(polledElements);
|
||||
|
||||
assertThat(polledElements).containsExactly(1, 2, 3, 4, 5);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPollingEmptyQueue_thenShouldBlockThread() throws InterruptedException {
|
||||
PriorityBlockingQueue<Integer> queue = new PriorityBlockingQueue<>();
|
||||
|
||||
final Thread thread = new Thread(() -> {
|
||||
System.out.println("Polling...");
|
||||
while (true) {
|
||||
try {
|
||||
Integer poll = queue.take();
|
||||
System.out.println("Polled: " + poll);
|
||||
} catch (InterruptedException e) {}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
|
||||
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
|
||||
System.out.println("Adding to queue");
|
||||
|
||||
queue.addAll(newArrayList(1, 5, 6, 1, 2, 6, 7));
|
||||
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.baeldung.java.streams;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.LongStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ThreadPoolInParallelStreamTest {
|
||||
|
||||
@Test
|
||||
public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal()
|
||||
throws InterruptedException, ExecutionException {
|
||||
long firstNum = 1;
|
||||
long lastNum = 1_000_000;
|
||||
|
||||
List<Long> aList = LongStream.rangeClosed(firstNum, lastNum).boxed()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
ForkJoinPool customThreadPool = new ForkJoinPool(4);
|
||||
long actualTotal = customThreadPool.submit(() -> aList.parallelStream()
|
||||
.reduce(0L, Long::sum)).get();
|
||||
|
||||
assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_whenCallingParallelStream_shouldBeParallelStream(){
|
||||
List<Long> aList = new ArrayList<>();
|
||||
Stream<Long> parallelStream = aList.parallelStream();
|
||||
|
||||
assertTrue(parallelStream.isParallel());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
|
||||
public class CustomEvent {
|
||||
private String action;
|
||||
|
||||
public CustomEvent(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
public String getAction() {
|
||||
return action;
|
||||
}
|
||||
|
||||
public void setAction(String action) {
|
||||
this.action = action;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
import com.google.common.eventbus.EventBus;
|
||||
|
||||
class EventBusWrapper {
|
||||
|
||||
private static EventBus eventBus = new EventBus();
|
||||
|
||||
static void register(Object object){
|
||||
eventBus.register(object);
|
||||
}
|
||||
|
||||
static void unregister(Object object){
|
||||
eventBus.unregister(object);
|
||||
}
|
||||
|
||||
static void post(Object object){
|
||||
eventBus.post(object);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package org.baeldung.guava;
|
||||
import com.google.common.eventbus.Subscribe;
|
||||
|
||||
public class EventListener {
|
||||
|
||||
private static int eventsHandled;
|
||||
|
||||
/**
|
||||
* Handles events of type String *
|
||||
*/
|
||||
@Subscribe
|
||||
public void stringEvent(String event){
|
||||
System.out.println("do event ["+event+"]");
|
||||
eventsHandled++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles events of type CustomEvent
|
||||
*/
|
||||
@Subscribe
|
||||
public void someEvent(CustomEvent customEvent){
|
||||
System.out.println("do event ["+ customEvent.getAction()+"]");
|
||||
eventsHandled++;
|
||||
}
|
||||
|
||||
public int getEventsHandled() {
|
||||
return eventsHandled;
|
||||
}
|
||||
|
||||
public void resetEventsHandled(){
|
||||
eventsHandled = 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class GuavaEventBusTest {
|
||||
|
||||
private EventListener listener;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
listener = new EventListener();
|
||||
EventBusWrapper.register(listener);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
EventBusWrapper.unregister(listener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringEvent_whenEventHandled_thenSuccess() throws Exception {
|
||||
listener.resetEventsHandled();
|
||||
|
||||
EventBusWrapper.post("String Event");
|
||||
assertEquals(1, listener.getEventsHandled());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCustomEvent_whenEventHandled_thenSuccess() throws Exception {
|
||||
listener.resetEventsHandled();
|
||||
|
||||
CustomEvent customEvent = new CustomEvent("Custom Event");
|
||||
EventBusWrapper.post(customEvent);
|
||||
|
||||
assertEquals(1, listener.getEventsHandled());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package org.baeldung.guava;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.Ordering;
|
||||
import com.google.common.primitives.Ints;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class GuavaOrderingTest {
|
||||
@Test
|
||||
public void givenListOfIntegers_whenCreateNaturalOrderOrdering_shouldSortProperly() {
|
||||
//given
|
||||
List<Integer> integers = Arrays.asList(3, 2, 1);
|
||||
|
||||
//when
|
||||
integers.sort(Ordering.natural());
|
||||
|
||||
//then
|
||||
assertEquals(Arrays.asList(1, 2, 3), integers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfPersonObject_whenSortedUsingCustomOrdering_shouldSortProperly() {
|
||||
//given
|
||||
List<Person> persons = Arrays.asList(new Person("Michael", 10), new Person("Alice", 3));
|
||||
Ordering<Person> orderingByAge = new Ordering<Person>() {
|
||||
@Override
|
||||
public int compare(Person p1, Person p2) {
|
||||
return Ints.compare(p1.age, p2.age);
|
||||
}
|
||||
};
|
||||
|
||||
//when
|
||||
persons.sort(orderingByAge);
|
||||
|
||||
//then
|
||||
assertEquals(Arrays.asList(new Person("Alice", 3), new Person("Michael", 10)), persons);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfPersonObject_whenSortedUsingChainedOrdering_shouldSortPropely() {
|
||||
//given
|
||||
List<Person> persons = Arrays.asList(new Person("Michael", 10), new Person("Alice", 3), new Person("Thomas", null));
|
||||
Ordering<Person> ordering = Ordering.natural().nullsFirst().onResultOf(new Function<Person, Comparable>() {
|
||||
@Override
|
||||
public Comparable apply(Person person) {
|
||||
return person.age;
|
||||
}
|
||||
});
|
||||
|
||||
//when
|
||||
persons.sort(ordering);
|
||||
|
||||
//then
|
||||
assertEquals(Arrays.asList(new Person("Thomas", null), new Person("Alice", 3), new Person("Michael", 10)), persons);
|
||||
}
|
||||
|
||||
|
||||
class Person {
|
||||
private final String name;
|
||||
private final Integer age;
|
||||
|
||||
private Person(String name, Integer age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Person person = (Person) o;
|
||||
|
||||
if (name != null ? !name.equals(person.name) : person.name != null) return false;
|
||||
return age != null ? age.equals(person.age) : person.age == null;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = name != null ? name.hashCode() : 0;
|
||||
result = 31 * result + (age != null ? age.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.javaslang.exception.handling;
|
||||
|
||||
import com.baeldung.javaslang.exception.handling.client.ClientException;
|
||||
import com.baeldung.javaslang.exception.handling.client.HttpClient;
|
||||
import com.baeldung.javaslang.exception.handling.client.Response;
|
||||
|
||||
public class JavaTryCatch {
|
||||
private HttpClient httpClient;
|
||||
|
||||
public JavaTryCatch(HttpClient httpClient) {
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
public Response getResponse() {
|
||||
try {
|
||||
return httpClient.call();
|
||||
} catch (ClientException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.javaslang.exception.handling;
|
||||
|
||||
|
||||
import com.baeldung.javaslang.exception.handling.client.HttpClient;
|
||||
import com.baeldung.javaslang.exception.handling.client.Response;
|
||||
import javaslang.control.Try;
|
||||
|
||||
public class JavaslangTry {
|
||||
private final HttpClient httpClient;
|
||||
|
||||
public JavaslangTry(HttpClient httpClient) {
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
public Try<Response> getResponse() {
|
||||
return Try.of(httpClient::call);
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.javaslang.exception.handling.client;
|
||||
|
||||
|
||||
public class ClientException extends Exception {
|
||||
public ClientException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.javaslang.exception.handling.client;
|
||||
|
||||
|
||||
public interface HttpClient {
|
||||
Response call() throws ClientException;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.javaslang.exception.handling.client;
|
||||
|
||||
public class Response {
|
||||
public final String id;
|
||||
|
||||
public Response(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user