This commit is contained in:
Matt Zhang
2019-06-01 15:03:41 +10:00
91 changed files with 1615 additions and 358 deletions
+8 -3
View File
@@ -20,17 +20,22 @@ In additional to Spring, the following technologies are in focus: `core Java`, `
Building the project
====================
To do the full build, do: `mvn install -Pdefault -Dgib.enabled=false`
To do the full build, do: `mvn clean install`
Building a single module
====================
To build a specific module run the command: `mvn clean install -Dgib.enabled=false` in the module directory
To build a specific module run the command: `mvn clean install` in the module directory
Running a Spring Boot module
====================
To run a Spring Boot module run the command: `mvn spring-boot:run -Dgib.enabled=false` in the module directory
To run a Spring Boot module run the command: `mvn spring-boot:run` in the module directory
#Running Tests
The command `mvn clean install` will run the unit tests in a module.
To run the integration tests, use the command `mvn clean install -Pintegration-lite-first`
@@ -21,7 +21,7 @@ import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
public class GeodeSamplesIntegrationTest {
public class GeodeSamplesLiveTest {
ClientCache cache = null;
Region<String, String> region = null;
+3
View File
@@ -0,0 +1,3 @@
## Relevant articles:
- [OData Protocol Guide](https://www.baeldung.com/odata)
+1 -1
View File
@@ -28,6 +28,6 @@
- [Java 9 Convenience Factory Methods for Collections](https://www.baeldung.com/java-9-collections-factory-methods)
- [Java 9 Stream API Improvements](https://www.baeldung.com/java-9-stream-api)
- [A Guide to Java 9 Modularity](https://www.baeldung.com/java-9-modularity)
- [Java 9 Platform Module API](https://www.baeldung.com/java-9-module-api)
- [Java 9 java.lang.Module API](https://www.baeldung.com/java-9-module-api)
- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api)
- [Filtering a Stream of Optionals in Java](https://www.baeldung.com/java-filter-stream-of-optional)
@@ -0,0 +1,3 @@
## Relevant articles:
- [Why Do Local Variables Used in Lambdas Have to Be Final or Effectively Final?](https://www.baeldung.com/java-lambda-effectively-final-local-variables)
@@ -1 +0,0 @@
### Relevant Articles:
@@ -19,7 +19,7 @@ public class Graph {
void removeVertex(String label) {
Vertex v = new Vertex(label);
adjVertices.values().stream().map(e -> e.remove(v)).collect(Collectors.toList());
adjVertices.values().stream().forEach(e -> e.remove(v));
adjVertices.remove(new Vertex(label));
}
@@ -1,20 +1,31 @@
package com.baeldung.graph;
import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class GraphTraversalUnitTest {
public class GraphUnitTest {
@Test
public void givenAGraph_whenTraversingDepthFirst_thenExpectedResult() {
Graph graph = createGraph();
Assert.assertEquals("[Bob, Rob, Maria, Alice, Mark]",
assertEquals("[Bob, Rob, Maria, Alice, Mark]",
GraphTraversal.depthFirstTraversal(graph, "Bob").toString());
}
@Test
public void givenAGraph_whenTraversingBreadthFirst_thenExpectedResult() {
Graph graph = createGraph();
Assert.assertEquals("[Bob, Alice, Rob, Mark, Maria]",
assertEquals("[Bob, Alice, Rob, Mark, Maria]",
GraphTraversal.breadthFirstTraversal(graph, "Bob").toString());
}
@Test
public void givenAGraph_whenRemoveVertex_thenVertedNotFound() {
Graph graph = createGraph();
assertEquals("[Bob, Alice, Rob, Mark, Maria]",
GraphTraversal.breadthFirstTraversal(graph, "Bob").toString());
graph.removeVertex("Maria");
assertEquals("[Bob, Alice, Rob, Mark]",
GraphTraversal.breadthFirstTraversal(graph, "Bob").toString());
}
@@ -9,19 +9,3 @@ interface Document {
fun getType() = "document"
}
class TextDocument : Document {
override fun getType() = "text"
fun transformList(list : List<Number>) : List<Number> {
return list.filter { n -> n.toInt() > 1 }
}
fun transformListInverseWildcards(list : List<@JvmSuppressWildcards Number>) : List<@JvmWildcard Number> {
return list.filter { n -> n.toInt() > 1 }
}
var list : List<@JvmWildcard Any> = ArrayList()
}
class XmlDocument(d : Document) : Document by d
@@ -0,0 +1,16 @@
package com.baeldung.jvmannotations
import java.util.*
class TextDocument : Document {
override fun getType() = "text"
fun transformList(list : List<Number>) : List<Number> {
return list.filter { n -> n.toInt() > 1 }
}
fun transformListInverseWildcards(list : List<@JvmSuppressWildcards Number>) : List<@JvmWildcard Number> {
return list.filter { n -> n.toInt() > 1 }
}
var list : List<@JvmWildcard Any> = ArrayList()
}
@@ -0,0 +1,5 @@
package com.baeldung.jvmannotations
import java.util.*
class XmlDocument(d : Document) : Document by d
@@ -3,6 +3,8 @@ package com.baeldung.range
import org.junit.Test
import kotlin.test.assertEquals
import com.baeldung.jvmannotations.*;
class DocumentTest {
@Test
@@ -0,0 +1,48 @@
package com.baeldung.convertToMap;
public class Book {
private String name;
private int releaseYear;
private String isbn;
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", releaseYear=" + releaseYear +
", isbn='" + isbn + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getReleaseYear() {
return releaseYear;
}
public void setReleaseYear(int releaseYear) {
this.releaseYear = releaseYear;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public Book(String name, int releaseYear, String isbn) {
this.name = name;
this.releaseYear = releaseYear;
this.isbn = isbn;
}
}
@@ -0,0 +1,34 @@
package com.baeldung.convertToMap;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
public class ConvertToMap {
public Map<String, String> listToMap(List<Book> books) {
return books.stream().collect(Collectors.toMap(Book::getIsbn, Book::getName));
}
public Map<Integer, Book> listToMapWithDupKeyError(List<Book> books) {
return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity()));
}
public Map<Integer, Book> listToMapWithDupKey(List<Book> books) {
return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(),
(o1, o2) -> o1));
}
public Map<Integer, Book> listToConcurrentMap(List<Book> books) {
return books.stream().collect(Collectors.toMap(Book::getReleaseYear, Function.identity(), (o1, o2) -> o1, ConcurrentHashMap::new));
}
public TreeMap<String, Book> listToSortedMap(List<Book> books) {
return books.stream()
.sorted(Comparator.comparing(Book::getName))
.collect(Collectors.toMap(Book::getName, Function.identity(), (o1, o2) -> o1, TreeMap::new));
}
}
@@ -0,0 +1,50 @@
package com.baeldung.convertToMap;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class ConvertToMapUnitTest {
private List<Book> bookList;
private ConvertToMap convertToMap = new ConvertToMap();
@Before
public void init() {
bookList = new ArrayList<>();
bookList.add(new Book("The Fellowship of the Ring", 1954, "0395489318"));
bookList.add(new Book("The Two Towers", 1954, "0345339711"));
bookList.add(new Book("The Return of the King", 1955, "0618129111"));
}
@Test
public void whenConvertFromListToMap() {
assertTrue(convertToMap.listToMap(bookList).size() == 3);
}
@Test(expected = IllegalStateException.class)
public void whenMapHasDuplicateKey_without_merge_function_then_runtime_exception() {
convertToMap.listToMapWithDupKeyError(bookList);
}
@Test
public void whenMapHasDuplicateKey_with_merge_function() {
assertTrue(convertToMap.listToMapWithDupKey(bookList).size() == 2);
}
@Test
public void whenCreateConcurrentHashMap() {
assertTrue(convertToMap.listToConcurrentMap(bookList) instanceof ConcurrentHashMap);
}
@Test
public void whenMapisSorted() {
assertTrue(convertToMap.listToSortedMap(bookList).firstKey().equals("The Fellowship of the Ring"));
}
}
+120
View File
@@ -0,0 +1,120 @@
<?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>
<groupId>com.baeldung</groupId>
<artifactId>kotlin-quasar</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>kotlin-quasar</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>co.paralleluniverse</groupId>
<artifactId>quasar-core</artifactId>
<version>${quasar.version}</version>
</dependency>
<dependency>
<groupId>co.paralleluniverse</groupId>
<artifactId>quasar-actors</artifactId>
<version>${quasar.version}</version>
</dependency>
<dependency>
<groupId>co.paralleluniverse</groupId>
<artifactId>quasar-reactive-streams</artifactId>
<version>${quasar.version}</version>
</dependency>
<dependency>
<groupId>co.paralleluniverse</groupId>
<artifactId>quasar-kotlin</artifactId>
<version>${quasar.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>getClasspathFilenames</id>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
<configuration>
<argLine>-Dco.paralleluniverse.fibers.verifyInstrumentation=true</argLine>
<argLine>-javaagent:${co.paralleluniverse:quasar-core:jar}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<workingDirectory>target/classes</workingDirectory>
<executable>echo</executable>
<arguments>
<argument>-javaagent:${co.paralleluniverse:quasar-core:jar}</argument>
<argument>-classpath</argument> <classpath/>
<argument>com.baeldung.quasar.QuasarHelloWorldKt</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<quasar.version>0.8.0</quasar.version>
<kotlin.version>1.3.31</kotlin.version>
</properties>
</project>
@@ -0,0 +1,19 @@
package com.baeldung.quasar
import co.paralleluniverse.fibers.Fiber
import co.paralleluniverse.strands.SuspendableRunnable
/**
* Entrypoint into the application
*/
fun main(args: Array<String>) {
class Runnable : SuspendableRunnable {
override fun run() {
println("Hello")
}
}
val result = Fiber<Void>(Runnable()).start()
result.join()
println("World")
}
@@ -0,0 +1,155 @@
package com.baeldung.quasar
import co.paralleluniverse.fibers.Suspendable
import co.paralleluniverse.kotlin.fiber
import co.paralleluniverse.strands.channels.Channels
import co.paralleluniverse.strands.channels.Selector
import com.google.common.base.Function
import org.junit.Test
class ChannelsTest {
@Test
fun createChannel() {
Channels.newChannel<String>(0, // The size of the channel buffer
Channels.OverflowPolicy.BLOCK, // The policy for when the buffer is full
true, // Whether we should optimize for a single message producer
true) // Whether we should optimize for a single message consumer
}
@Test
fun blockOnMessage() {
val channel = Channels.newChannel<String>(0, Channels.OverflowPolicy.BLOCK, true, true)
fiber @Suspendable {
while (!channel.isClosed) {
val message = channel.receive()
println("Received: $message")
}
println("Stopped receiving messages")
}
channel.send("Hello")
channel.send("World")
channel.close()
}
@Test
fun selectReceiveChannels() {
val channel1 = Channels.newChannel<String>(0, Channels.OverflowPolicy.BLOCK, true, true)
val channel2 = Channels.newChannel<String>(0, Channels.OverflowPolicy.BLOCK, true, true)
fiber @Suspendable {
while (!channel1.isClosed && !channel2.isClosed) {
val received = Selector.select(Selector.receive(channel1), Selector.receive(channel2))
println("Received: $received")
}
}
fiber @Suspendable {
for (i in 0..10) {
channel1.send("Channel 1: $i")
}
}
fiber @Suspendable {
for (i in 0..10) {
channel2.send("Channel 2: $i")
}
}
}
@Test
fun selectSendChannels() {
val channel1 = Channels.newChannel<String>(0, Channels.OverflowPolicy.BLOCK, true, true)
val channel2 = Channels.newChannel<String>(0, Channels.OverflowPolicy.BLOCK, true, true)
fiber @Suspendable {
for (i in 0..10) {
Selector.select(
Selector.send(channel1, "Channel 1: $i"),
Selector.send(channel2, "Channel 2: $i")
)
}
}
fiber @Suspendable {
while (!channel1.isClosed) {
val msg = channel1.receive()
println("Read: $msg")
}
}
fiber @Suspendable {
while (!channel2.isClosed) {
val msg = channel2.receive()
println("Read: $msg")
}
}
}
@Test
fun tickerChannel() {
val channel = Channels.newChannel<String>(3, Channels.OverflowPolicy.DISPLACE)
for (i in 0..10) {
val tickerConsumer = Channels.newTickerConsumerFor(channel)
fiber @Suspendable {
while (!tickerConsumer.isClosed) {
val message = tickerConsumer.receive()
println("Received on $i: $message")
}
println("Stopped receiving messages on $i")
}
}
for (i in 0..50) {
channel.send("Message $i")
}
channel.close()
}
@Test
fun transformOnSend() {
val channel = Channels.newChannel<String>(0, Channels.OverflowPolicy.BLOCK, true, true)
fiber @Suspendable {
while (!channel.isClosed) {
val message = channel.receive()
println("Received: $message")
}
println("Stopped receiving messages")
}
val transformOnSend = Channels.mapSend(channel, Function<String, String> { msg: String? -> msg?.toUpperCase() })
transformOnSend.send("Hello")
transformOnSend.send("World")
channel.close()
}
@Test
fun transformOnReceive() {
val channel = Channels.newChannel<String>(0, Channels.OverflowPolicy.BLOCK, true, true)
val transformOnReceive = Channels.map(channel, Function<String, String> { msg: String? -> msg?.reversed() })
fiber @Suspendable {
while (!transformOnReceive.isClosed) {
val message = transformOnReceive.receive()
println("Received: $message")
}
println("Stopped receiving messages")
}
channel.send("Hello")
channel.send("World")
channel.close()
}
}
@@ -0,0 +1,35 @@
package com.baeldung.quasar
import co.paralleluniverse.strands.dataflow.Val
import co.paralleluniverse.strands.dataflow.Var
import org.junit.Assert
import org.junit.Test
import java.util.concurrent.TimeUnit
class DataflowTest {
@Test
fun testValVar() {
val a = Var<Int>()
val b = Val<Int>()
val c = Var<Int> { a.get() + b.get() }
val d = Var<Int> { a.get() * b.get() }
// (a*b) - (a+b)
val initialResult = Val<Int> { d.get() - c.get() }
val currentResult = Var<Int> { d.get() - c.get() }
a.set(2)
b.set(4)
Assert.assertEquals(2, initialResult.get())
Assert.assertEquals(2, currentResult.get())
a.set(3)
TimeUnit.SECONDS.sleep(1)
Assert.assertEquals(2, initialResult.get())
Assert.assertEquals(5, currentResult.get())
}
}
@@ -0,0 +1,53 @@
package com.baeldung.quasar
import co.paralleluniverse.fibers.Fiber
import co.paralleluniverse.fibers.FiberAsync
import co.paralleluniverse.fibers.Suspendable
import co.paralleluniverse.kotlin.fiber
import co.paralleluniverse.strands.Strand
import org.junit.Assert
import org.junit.Test
import java.math.BigDecimal
import java.util.concurrent.TimeUnit
interface PiCallback {
fun success(result: BigDecimal)
fun failure(error: Exception)
}
fun computePi(callback: PiCallback) {
println("Starting calculations")
TimeUnit.SECONDS.sleep(2)
println("Finished calculations")
callback.success(BigDecimal("3.14"))
}
class PiAsync : PiCallback, FiberAsync<BigDecimal, Exception>() {
override fun success(result: BigDecimal) {
asyncCompleted(result)
}
override fun failure(error: Exception) {
asyncFailed(error)
}
override fun requestAsync() {
computePi(this)
}
}
class PiAsyncTest {
@Test
fun testPi() {
val result = fiber @Suspendable {
val pi = PiAsync()
println("Waiting to get PI on: " + Fiber.currentFiber().name)
val result = pi.run()
println("Got PI")
result
}.get()
Assert.assertEquals(BigDecimal("3.14"), result)
}
}
@@ -0,0 +1,48 @@
package com.baeldung.quasar
import co.paralleluniverse.fibers.Fiber
import co.paralleluniverse.fibers.Suspendable
import co.paralleluniverse.kotlin.fiber
import co.paralleluniverse.strands.SuspendableCallable
import org.junit.Assert
import org.junit.Test
import java.util.concurrent.TimeUnit
class SuspendableCallableTest {
@Test
fun createFiber() {
class Callable : SuspendableCallable<String> {
override fun run(): String {
println("Inside Fiber")
return "Hello"
}
}
val result = Fiber<String>(Callable()).start()
Assert.assertEquals("Hello", result.get())
}
@Test
fun createFiberLambda() {
val lambda: (() -> String) = {
println("Inside Fiber Lambda")
"Hello"
}
val result = Fiber<String>(lambda)
result.start()
Assert.assertEquals("Hello", result.get())
}
@Test
fun createFiberDsl() {
val result = fiber @Suspendable {
TimeUnit.SECONDS.sleep(5)
println("Inside Fiber DSL")
"Hello"
}
Assert.assertEquals("Hello", result.get())
}
}
@@ -0,0 +1,47 @@
package com.baeldung.quasar
import co.paralleluniverse.fibers.Fiber
import co.paralleluniverse.fibers.Suspendable
import co.paralleluniverse.kotlin.fiber
import co.paralleluniverse.strands.SuspendableRunnable
import org.junit.Test
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
class SuspensableRunnableTest {
@Test
fun createFiber() {
class Runnable : SuspendableRunnable {
override fun run() {
println("Inside Fiber")
}
}
val result = Fiber<Void>(Runnable()).start()
result.join()
}
@Test
fun createFiberLambda() {
val result = Fiber<Void> {
println("Inside Fiber Lambda")
}
result.start()
result.join()
}
@Test
fun createFiberDsl() {
fiber @Suspendable {
println("Inside Fiber DSL")
}.join()
}
@Test(expected = TimeoutException::class)
fun fiberTimeout() {
fiber @Suspendable {
TimeUnit.SECONDS.sleep(5)
println("Inside Fiber DSL")
}.join(2, TimeUnit.SECONDS)
}
}
+1
View File
@@ -16,3 +16,4 @@
- [Multi-Module Project with Maven](https://www.baeldung.com/maven-multi-module)
- [Maven Enforcer Plugin](https://www.baeldung.com/maven-enforcer-plugin)
- [Eclipse Error: web.xml is missing and failOnMissingWebXml is set to true](https://www.baeldung.com/eclipse-error-web-xml-missing)
- [Guide to Maven Profiles](https://www.baeldung.com/maven-profiles)
-6
View File
@@ -1,6 +0,0 @@
### Relevant Articles:
- [A Guide to the Front Controller Pattern in Java](http://www.baeldung.com/java-front-controller-pattern)
- [Introduction to Intercepting Filter Pattern in Java](http://www.baeldung.com/intercepting-filter-pattern-in-java)
- [Introduction to the Null Object Pattern](https://www.baeldung.com/java-null-object-pattern)
- [The Dependency Inversion Principle in Java](https://www.baeldung.com/java-dependency-inversion-principle)
- [Avoid Check for Null Statement in Java](https://www.baeldung.com/java-avoid-null-check)
+2
View File
@@ -1,3 +1,5 @@
### Relevant Articles
- [The Mediator Pattern in Java](https://www.baeldung.com/java-mediator-pattern)
- [Introduction to the Null Object Pattern](https://www.baeldung.com/java-null-object-pattern)
- [Avoid Check for Null Statement in Java](https://www.baeldung.com/java-avoid-null-check)
+3
View File
@@ -0,0 +1,3 @@
### Relevant Articles:
- [The Dependency Inversion Principle in Java](https://www.baeldung.com/java-dependency-inversion-principle)
+2
View File
@@ -0,0 +1,2 @@
### Relevant Articles:
- [A Guide to the Front Controller Pattern in Java](http://www.baeldung.com/java-front-controller-pattern)
+2
View File
@@ -0,0 +1,2 @@
### Relevant Articles:
- [Introduction to Intercepting Filter Pattern in Java](http://www.baeldung.com/intercepting-filter-pattern-in-java)
-17
View File
@@ -1,17 +0,0 @@
## Persistence Modules
### Relevant Articles:
- [Introduction to Hibernate Search](http://www.baeldung.com/hibernate-search)
- [Introduction to Lettuce – the Java Redis Client](http://www.baeldung.com/java-redis-lettuce)
- [A Guide to Jdbi](http://www.baeldung.com/jdbi)
- [Pessimistic Locking in JPA](http://www.baeldung.com/jpa-pessimistic-locking)
- [Get All Data from a Table with Hibernate](https://www.baeldung.com/hibernate-select-all)
- [Spring Data with Reactive Cassandra](https://www.baeldung.com/spring-data-cassandra-reactive)
- [Spring Data JPA – Derived Delete Methods](https://www.baeldung.com/spring-data-jpa-deleteby)
- [Difference Between save() and saveAndFlush() in Spring Data JPA](https://www.baeldung.com/spring-data-jpa-save-saveandflush)
- [Spring Boot with Hibernate](https://www.baeldung.com/spring-boot-hibernate)
- [Persisting Maps with Hibernate](https://www.baeldung.com/hibernate-persisting-maps)
- [Difference Between @Size, @Length, and @Column(length=value)](https://www.baeldung.com/jpa-size-length-column-differences)

Some files were not shown because too many files have changed in this diff Show More