Merge branch 'master' into JAVA-7244-Review_log_statements_for_projects
This commit is contained in:
@@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -40,7 +39,6 @@
|
||||
<properties>
|
||||
<maven.compiler.source.version>10</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>10</maven.compiler.target.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -12,7 +12,7 @@
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../..</relativePath>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -21,35 +21,11 @@
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mock-server</groupId>
|
||||
<artifactId>mockserver-junit-jupiter</artifactId>
|
||||
<version>${mockserver.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-params</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
@@ -60,18 +36,6 @@
|
||||
<artifactId>jakarta.xml.ws-api</artifactId>
|
||||
<version>${jakarta.ws-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.ws</groupId>
|
||||
<artifactId>jaxws-rt</artifactId>
|
||||
<version>${jaxws-rt.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.ws</groupId>
|
||||
<artifactId>jaxws-ri</artifactId>
|
||||
<version>${jaxws-ri.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -106,13 +70,9 @@
|
||||
<maven.compiler.source.version>11</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>11</maven.compiler.target.version>
|
||||
<guava.version>29.0-jre</guava.version>
|
||||
<assertj.version>3.17.2</assertj.version>
|
||||
<mockserver.version>5.11.1</mockserver.version>
|
||||
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||
<jakarta.ws-api.version>3.0.0</jakarta.ws-api.version>
|
||||
<jaxws-rt.version>3.0.0</jaxws-rt.version>
|
||||
<jaxws-ri.version>2.3.1</jaxws-ri.version>
|
||||
<jaxws-maven-plugin.version>2.3.2</jaxws-maven-plugin.version>
|
||||
<jakarta.ws-api.version>3.0.1</jakarta.ws-api.version>
|
||||
<jaxws-maven-plugin.version>3.0.2</jaxws-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.httpsclientauthentication;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import javax.net.SocketFactory;
|
||||
import javax.net.ssl.SSLSocket;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
|
||||
public class SSLScocketClient {
|
||||
|
||||
static void startClient(String host, int port) throws IOException {
|
||||
|
||||
SocketFactory factory = SSLSocketFactory.getDefault();
|
||||
|
||||
try (SSLSocket socket = (SSLSocket) factory.createSocket(host, port)) {
|
||||
socket.setEnabledCipherSuites(new String[] { "TLS_AES_128_GCM_SHA256" });
|
||||
socket.setEnabledProtocols(new String[] { "TLSv1.3" });
|
||||
InputStream is = new BufferedInputStream(socket.getInputStream());
|
||||
String message = "Hello World Message";
|
||||
System.out.println("sending message: " + message);
|
||||
OutputStream os = new BufferedOutputStream(socket.getOutputStream());
|
||||
os.write(message.getBytes());
|
||||
os.flush();
|
||||
byte[] data = new byte[2048];
|
||||
int len = is.read(data);
|
||||
if (len <= 0) {
|
||||
throw new IOException("no data received");
|
||||
}
|
||||
System.out.printf("client received %d bytes: %s%n", len, new String(data, 0, len));
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
|
||||
startClient("localhost", 8443);
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.httpsclientauthentication;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
import javax.net.ServerSocketFactory;
|
||||
import javax.net.ssl.SSLServerSocket;
|
||||
import javax.net.ssl.SSLServerSocketFactory;
|
||||
|
||||
public class SSLSocketEchoServer {
|
||||
|
||||
static void startServer(int port) throws IOException {
|
||||
|
||||
ServerSocketFactory factory = SSLServerSocketFactory.getDefault();
|
||||
|
||||
try (SSLServerSocket listener = (SSLServerSocket) factory.createServerSocket(port)) {
|
||||
listener.setNeedClientAuth(true);
|
||||
listener.setEnabledCipherSuites(new String[] { "TLS_AES_128_GCM_SHA256" });
|
||||
listener.setEnabledProtocols(new String[] { "TLSv1.3" });
|
||||
System.out.println("listening for messages...");
|
||||
try (Socket socket = listener.accept()) {
|
||||
InputStream is = new BufferedInputStream(socket.getInputStream());
|
||||
OutputStream os = new BufferedOutputStream(socket.getOutputStream());
|
||||
byte[] data = new byte[2048];
|
||||
int len = is.read(data);
|
||||
if (len <= 0) {
|
||||
throw new IOException("no data received");
|
||||
}
|
||||
String message = new String(data, 0, len);
|
||||
System.out.printf("server received %d bytes: %s%n", len, message);
|
||||
String response = message + " processed by server";
|
||||
os.write(response.getBytes(), 0, response.getBytes().length);
|
||||
os.flush();
|
||||
}
|
||||
System.out.println("message processed, exiting");
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
startServer(8443);
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -1,10 +1,10 @@
|
||||
|
||||
package com.baeldung.soap.ws.client.generated;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlSchemaType;
|
||||
import javax.xml.bind.annotation.XmlType;
|
||||
import jakarta.xml.bind.annotation.XmlAccessType;
|
||||
import jakarta.xml.bind.annotation.XmlAccessorType;
|
||||
import jakarta.xml.bind.annotation.XmlSchemaType;
|
||||
import jakarta.xml.bind.annotation.XmlType;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
+9
-9
@@ -1,19 +1,19 @@
|
||||
|
||||
package com.baeldung.soap.ws.client.generated;
|
||||
|
||||
import javax.jws.WebMethod;
|
||||
import javax.jws.WebParam;
|
||||
import javax.jws.WebResult;
|
||||
import javax.jws.WebService;
|
||||
import javax.jws.soap.SOAPBinding;
|
||||
import javax.xml.bind.annotation.XmlSeeAlso;
|
||||
import javax.xml.ws.Action;
|
||||
import jakarta.jws.WebMethod;
|
||||
import jakarta.jws.WebParam;
|
||||
import jakarta.jws.WebResult;
|
||||
import jakarta.jws.WebService;
|
||||
import jakarta.jws.soap.SOAPBinding;
|
||||
import jakarta.xml.bind.annotation.XmlSeeAlso;
|
||||
import jakarta.xml.ws.Action;
|
||||
|
||||
|
||||
/**
|
||||
* This class was generated by the JAX-WS RI.
|
||||
* JAX-WS RI 2.3.2
|
||||
* Generated source version: 2.2
|
||||
* JAX-WS RI 3.0.2
|
||||
* Generated source version: 3.0
|
||||
*
|
||||
*/
|
||||
@WebService(name = "CountryService", targetNamespace = "http://server.ws.soap.baeldung.com/")
|
||||
|
||||
+8
-8
@@ -4,17 +4,17 @@ package com.baeldung.soap.ws.client.generated;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.ws.Service;
|
||||
import javax.xml.ws.WebEndpoint;
|
||||
import javax.xml.ws.WebServiceClient;
|
||||
import javax.xml.ws.WebServiceException;
|
||||
import javax.xml.ws.WebServiceFeature;
|
||||
import jakarta.xml.ws.Service;
|
||||
import jakarta.xml.ws.WebEndpoint;
|
||||
import jakarta.xml.ws.WebServiceClient;
|
||||
import jakarta.xml.ws.WebServiceException;
|
||||
import jakarta.xml.ws.WebServiceFeature;
|
||||
|
||||
|
||||
/**
|
||||
* This class was generated by the JAX-WS RI.
|
||||
* JAX-WS RI 2.3.2
|
||||
* Generated source version: 2.2
|
||||
* JAX-WS RI 3.0.2
|
||||
* Generated source version: 3.0
|
||||
*
|
||||
*/
|
||||
@WebServiceClient(name = "CountryServiceImplService", targetNamespace = "http://server.ws.soap.baeldung.com/", wsdlLocation = "http://localhost:8888/ws/country?wsdl")
|
||||
@@ -75,7 +75,7 @@ public class CountryServiceImplService
|
||||
/**
|
||||
*
|
||||
* @param features
|
||||
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
|
||||
* A list of {@link jakarta.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
|
||||
* @return
|
||||
* returns CountryService
|
||||
*/
|
||||
|
||||
+2
-3
@@ -1,15 +1,14 @@
|
||||
|
||||
package com.baeldung.soap.ws.client.generated;
|
||||
|
||||
import javax.xml.bind.annotation.XmlEnum;
|
||||
import javax.xml.bind.annotation.XmlType;
|
||||
import jakarta.xml.bind.annotation.XmlEnum;
|
||||
import jakarta.xml.bind.annotation.XmlType;
|
||||
|
||||
|
||||
/**
|
||||
* <p>Java class for currency.
|
||||
*
|
||||
* <p>The following schema fragment specifies the expected content contained within this class.
|
||||
* <p>
|
||||
* <pre>
|
||||
* <simpleType name="currency">
|
||||
* <restriction base="{http://www.w3.org/2001/XMLSchema}string">
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
|
||||
package com.baeldung.soap.ws.client.generated;
|
||||
|
||||
import javax.xml.bind.annotation.XmlRegistry;
|
||||
import jakarta.xml.bind.annotation.XmlRegistry;
|
||||
|
||||
|
||||
/**
|
||||
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
@javax.xml.bind.annotation.XmlSchema(namespace = "http://server.ws.soap.baeldung.com/")
|
||||
@jakarta.xml.bind.annotation.XmlSchema(namespace = "http://server.ws.soap.baeldung.com/")
|
||||
package com.baeldung.soap.ws.client.generated;
|
||||
|
||||
+2
-1
@@ -5,7 +5,8 @@ import org.assertj.core.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class VersionUnitTest {
|
||||
// manual test as the runtime JDK version can be different depending on where the test is run
|
||||
public class VersionManualTest {
|
||||
|
||||
@Test
|
||||
public void givenJava_whenUsingRuntime_thenGetVersion() {
|
||||
@@ -13,7 +13,8 @@
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../..</relativePath>
|
||||
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -22,12 +23,6 @@
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
@@ -104,7 +99,6 @@
|
||||
<maven.compiler.source.version>11</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>11</maven.compiler.target.version>
|
||||
<guava.version>27.1-jre</guava.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
<uberjar.name>benchmarks</uberjar.name>
|
||||
<eclipse.collections.version>10.0.0</eclipse.collections.version>
|
||||
<shade.plugin.version>3.2.4</shade.plugin.version>
|
||||
|
||||
@@ -13,20 +13,13 @@
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.11.0</version>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -54,7 +47,6 @@
|
||||
<properties>
|
||||
<maven.compiler.source.version>12</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>12</maven.compiler.target.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -14,18 +14,8 @@
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -53,7 +43,6 @@
|
||||
<properties>
|
||||
<maven.compiler.source.version>13</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>13</maven.compiler.target.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -12,30 +12,8 @@
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -62,7 +40,6 @@
|
||||
|
||||
<properties>
|
||||
<maven.compiler.release>14</maven.compiler.release>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
||||
</properties>
|
||||
|
||||
@@ -21,24 +21,6 @@
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -67,7 +49,6 @@
|
||||
|
||||
<properties>
|
||||
<maven.compiler.release>15</maven.compiler.release>
|
||||
<assertj.version>3.17.2</assertj.version>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
|
||||
</properties>
|
||||
|
||||
@@ -17,29 +17,11 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.12.0</version>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -50,37 +32,35 @@
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<release>${maven.compiler.release}</release>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
<source>${maven.compiler.source.version}</source>
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
<forkCount>1</forkCount>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.surefire</groupId>
|
||||
<artifactId>surefire-api</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
<forkCount>1</forkCount>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.surefire</groupId>
|
||||
<artifactId>surefire-api</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>16</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>16</maven.compiler.target.version>
|
||||
<maven.compiler.release>16</maven.compiler.release>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
|
||||
<assertj.version>3.17.2</assertj.version>
|
||||
<maven.compiler.release>16</maven.compiler.release>
|
||||
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,3 +1,5 @@
|
||||
### Relevant articles:
|
||||
|
||||
- [Pattern Matching for Switch](https://www.baeldung.com/java-switch-pattern-matching)
|
||||
- [Introduction to HexFormat in Java 17](https://www.baeldung.com/java-hexformat)
|
||||
- [New Features in Java 17](https://www.baeldung.com/java-17-new-features)
|
||||
|
||||
@@ -16,27 +16,6 @@
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@@ -50,32 +29,31 @@
|
||||
<target>${maven.compiler.target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
<forkCount>1</forkCount>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.surefire</groupId>
|
||||
<artifactId>surefire-api</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>--enable-preview</argLine>
|
||||
<argLine>--enable-native-access=core.java</argLine>
|
||||
<forkCount>1</forkCount>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.maven.surefire</groupId>
|
||||
<artifactId>surefire-api</artifactId>
|
||||
<version>${surefire.plugin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source.version>17</maven.compiler.source.version>
|
||||
<maven.compiler.target.version>17</maven.compiler.target.version>
|
||||
<maven.compiler.release>17</maven.compiler.release>
|
||||
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
|
||||
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
|
||||
<assertj.version>3.17.2</assertj.version>
|
||||
<maven.compiler.release>17</maven.compiler.release>
|
||||
<surefire.plugin.version>3.0.0-M5</surefire.plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import java.util.random.RandomGeneratorFactory;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class JEP356 {
|
||||
|
||||
public Stream<String> getAllAlgorithms() {
|
||||
return RandomGeneratorFactory.all().map(RandomGeneratorFactory::name);
|
||||
}
|
||||
|
||||
public IntStream getPseudoInts(String algorithm, int streamSize) {
|
||||
// returns an IntStream with size @streamSize of random numbers generated using the @algorithm
|
||||
// where the lower bound is 0 and the upper is 100 (exclusive)
|
||||
return RandomGeneratorFactory.of(algorithm)
|
||||
.create()
|
||||
.ints(streamSize, 0,100);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import com.baeldung.features.JEP409.Circle;
|
||||
import com.baeldung.features.JEP409.Shape;
|
||||
import com.baeldung.features.JEP409.Triangle;
|
||||
|
||||
public class JEP406 {
|
||||
|
||||
static record Human (String name, int age, String profession) {}
|
||||
|
||||
public String checkObject(Object obj) {
|
||||
return switch (obj) {
|
||||
case Human h -> "Name: %s, age: %s and profession: %s".formatted(h.name(), h.age(), h.profession());
|
||||
case Circle c -> "This is a circle";
|
||||
case Shape s -> "It is just a shape";
|
||||
case null -> "It is null";
|
||||
default -> "It is an object";
|
||||
};
|
||||
}
|
||||
|
||||
public String checkShape(Shape shape) {
|
||||
return switch (shape) {
|
||||
case Triangle t && (t.getNumberOfSides() != 3) -> "This is a weird triangle";
|
||||
case Circle c && (c.getNumberOfSides() != 0) -> "This is a weird circle";
|
||||
default -> "Just a normal shape";
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
public class JEP409 {
|
||||
|
||||
sealed interface Shape permits Rectangle, Circle, Square, Triangle {
|
||||
int getNumberOfSides();
|
||||
}
|
||||
|
||||
static final class Rectangle implements Shape {
|
||||
@Override
|
||||
public int getNumberOfSides() {
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
|
||||
static final class Circle implements Shape {
|
||||
@Override
|
||||
public int getNumberOfSides() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static final class Square implements Shape {
|
||||
@Override
|
||||
public int getNumberOfSides() {
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
|
||||
static non-sealed class Triangle implements Shape {
|
||||
|
||||
@Override
|
||||
public int getNumberOfSides() {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import jdk.incubator.foreign.CLinker;
|
||||
import jdk.incubator.foreign.FunctionDescriptor;
|
||||
import jdk.incubator.foreign.MemoryAddress;
|
||||
import jdk.incubator.foreign.SymbolLookup;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.invoke.MethodType;
|
||||
|
||||
import static jdk.incubator.foreign.ResourceScope.newImplicitScope;
|
||||
|
||||
public class JEP412 {
|
||||
|
||||
private static final SymbolLookup libLookup;
|
||||
|
||||
static {
|
||||
var resource = JEP412.class.getResource("/compile_c.sh");
|
||||
try {
|
||||
var process = new ProcessBuilder("sh", resource.getPath()).start();
|
||||
while (process.isAlive()) {}
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
||||
var path = JEP412.class.getResource("/print_name.so").getPath();
|
||||
System.load(path);
|
||||
libLookup = SymbolLookup.loaderLookup();
|
||||
}
|
||||
|
||||
public String getPrintNameFormat(String name){
|
||||
|
||||
var printMethod = libLookup.lookup("printName");
|
||||
|
||||
if (printMethod.isPresent()) {
|
||||
var methodReference = CLinker.getInstance()
|
||||
.downcallHandle(
|
||||
printMethod.get(),
|
||||
MethodType.methodType(MemoryAddress.class, MemoryAddress.class),
|
||||
FunctionDescriptor.of(CLinker.C_POINTER, CLinker.C_POINTER)
|
||||
);
|
||||
|
||||
try {
|
||||
var nativeString = CLinker.toCString(name, newImplicitScope());
|
||||
var invokeReturn = methodReference.invoke(nativeString.address());
|
||||
var memoryAddress = (MemoryAddress) invokeReturn;
|
||||
return CLinker.toJavaString(memoryAddress);
|
||||
} catch (Throwable throwable) {
|
||||
throw new RuntimeException(throwable);
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("printName function not found.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import jdk.incubator.vector.FloatVector;
|
||||
import jdk.incubator.vector.VectorSpecies;
|
||||
|
||||
public class JEP414 {
|
||||
|
||||
private static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_PREFERRED;
|
||||
|
||||
|
||||
public void newVectorComputation(float[] a, float[] b, float[] c) {
|
||||
for (var i = 0; i < a.length; i += SPECIES.length()) {
|
||||
var m = SPECIES.indexInRange(i, a.length);
|
||||
var va = FloatVector.fromArray(SPECIES, a, i, m);
|
||||
var vb = FloatVector.fromArray(SPECIES, b, i, m);
|
||||
var vc = va.mul(vb);
|
||||
vc.intoArray(c, i, m);
|
||||
}
|
||||
}
|
||||
|
||||
public void commonVectorComputation(float[] a, float[] b, float[] c) {
|
||||
for (var i = 0; i < a.length; i ++) {
|
||||
c[i] = a[i] * b[i];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
module core.java {
|
||||
requires jdk.incubator.vector;
|
||||
requires jdk.incubator.foreign;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
|
||||
|
||||
gcc -c -fPIC $SCRIPTPATH/print_name.c
|
||||
gcc -shared -rdynamic -o print_name.so print_name.o
|
||||
|
||||
mv print_name.so $SCRIPTPATH/
|
||||
mv print_name.o $SCRIPTPATH/
|
||||
@@ -0,0 +1,8 @@
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
char* printName(char *name) {
|
||||
char* newString = (char*)malloc((15 + sizeof(name))*sizeof(char));
|
||||
sprintf(newString, "Your name is %s", name);
|
||||
return newString;
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class JEP356UnitTest {
|
||||
|
||||
@Test
|
||||
void getPseudoInts_whenUsingAlgorithmXoroshiro128PlusPlus_shouldReturnStreamOfRandomInteger() {
|
||||
var algorithm = "Xoshiro256PlusPlus";
|
||||
var streamSize = 100;
|
||||
|
||||
JEP356 jep356 = new JEP356();
|
||||
|
||||
jep356.getPseudoInts(algorithm, streamSize)
|
||||
.forEach(value -> assertThat(value).isLessThanOrEqualTo(99));
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import com.baeldung.features.JEP406.Human;
|
||||
import com.baeldung.features.JEP409.Square;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class JEP406UnitTest {
|
||||
|
||||
@Test
|
||||
void checkObject_shouldMatchWithHuman() {
|
||||
var jep406 = new JEP406();
|
||||
|
||||
var human = new Human("John", 31, "Developer");
|
||||
|
||||
var checkResult = jep406.checkObject(human);
|
||||
|
||||
assertEquals("Name: John, age: 31 and profession: Developer", checkResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkShape_shouldMatchWithShape() {
|
||||
var jep406 = new JEP406();
|
||||
|
||||
var square = new Square();
|
||||
|
||||
var checkResult = jep406.checkShape(square);
|
||||
|
||||
assertEquals("Just a normal shape", checkResult);
|
||||
}
|
||||
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import com.baeldung.features.JEP409.*;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
class JEP409UnitTest {
|
||||
|
||||
static class WeirdTriangle extends Triangle {
|
||||
@Override public int getNumberOfSides() {
|
||||
return 40;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSealedClass_shouldInvokeRightClass() {
|
||||
|
||||
Shape shape = spy(new WeirdTriangle());
|
||||
|
||||
int numberOfSides = getNumberOfSides(shape);
|
||||
|
||||
assertEquals(40, numberOfSides);
|
||||
verify(shape).getNumberOfSides();
|
||||
}
|
||||
|
||||
int getNumberOfSides(Shape shape) {
|
||||
return switch (shape) {
|
||||
case WeirdTriangle t -> t.getNumberOfSides();
|
||||
case Circle c -> c.getNumberOfSides();
|
||||
case Triangle t -> t.getNumberOfSides();
|
||||
case Rectangle r -> r.getNumberOfSides();
|
||||
case Square s -> s.getNumberOfSides();
|
||||
};
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class JEP412UnitTest {
|
||||
|
||||
@Test
|
||||
void getPrintNameFormat_whenPassingAName_shouldReceiveItFormatted() {
|
||||
var jep412 = new JEP412();
|
||||
|
||||
var formattedName = jep412.getPrintNameFormat("John");
|
||||
|
||||
assertEquals("Your name is John", formattedName);
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.features;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class JEP414UnitTest {
|
||||
|
||||
@Test
|
||||
void vectorComputation_shouldMultiplyVectors() {
|
||||
var jep414 = new JEP414();
|
||||
|
||||
float[] a = initArray(100);
|
||||
float[] b = initArray(100);
|
||||
float[] c = new float[100];
|
||||
float[] d = new float[100];
|
||||
|
||||
jep414.newVectorComputation(a, b, c);
|
||||
jep414.commonVectorComputation(a, b, d);
|
||||
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
assertEquals(c[i], d[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private float[] initArray(int size) {
|
||||
final var jep356 = new JEP356();
|
||||
final var values = new float[size];
|
||||
final var pseudoInts = jep356.getPseudoInts("Xoshiro256PlusPlus", size).mapToObj(Float::valueOf).collect(toList());
|
||||
|
||||
for (int i = 0; i < pseudoInts.size(); i++) {
|
||||
values[i] = pseudoInts.get(i);
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.hexformat;
|
||||
|
||||
import java.util.HexFormat;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class ByteHexadecimalConversionUnitTest {
|
||||
|
||||
private HexFormat hexFormat = HexFormat.of();
|
||||
|
||||
@Test
|
||||
void givenInitialisedHexFormat_whenHexStringIsPassed_thenByteArrayRepresentationIsReturned() {
|
||||
byte[] hexBytes = hexFormat.parseHex("ABCDEF0123456789");
|
||||
assertArrayEquals(new byte[] { -85, -51, -17, 1, 35, 69, 103, -119 }, hexBytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInitialisedHexFormat_whenByteArrayIsPassed_thenHexStringRepresentationIsReturned() {
|
||||
String bytesAsString = hexFormat.formatHex(new byte[] { -85, -51, -17, 1, 35, 69, 103, -119});
|
||||
assertEquals("abcdef0123456789", bytesAsString);
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.hexformat;
|
||||
|
||||
import java.util.HexFormat;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class PrimitiveTypeHexadecimalConversionUnitTest {
|
||||
|
||||
private HexFormat hexFormat = HexFormat.of();
|
||||
|
||||
@Test
|
||||
void givenInitialisedHexFormat_whenPrimitiveByteIsPassed_thenHexStringRepresentationIsReturned() {
|
||||
String fromByte = hexFormat.toHexDigits((byte)64);
|
||||
assertEquals("40", fromByte);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenInitialisedHexFormat_whenPrimitiveLongIsPassed_thenHexStringRepresentationIsReturned() {
|
||||
String fromLong = hexFormat.toHexDigits(1234_5678_9012_3456L);
|
||||
assertEquals("000462d53c8abac0", fromLong);
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.hexformat;
|
||||
|
||||
import java.util.HexFormat;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class StringFormattingUnitTest {
|
||||
|
||||
private HexFormat hexFormat = HexFormat.of().withPrefix("[").withSuffix("]").withDelimiter(", ");
|
||||
|
||||
@Test
|
||||
public void givenInitialisedHexFormatWithFormattedStringOptions_whenByteArrayIsPassed_thenHexStringRepresentationFormattedCorrectlyIsReturned() {
|
||||
assertEquals("[48], [0c], [11]", hexFormat.formatHex(new byte[] {72, 12, 17}));
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.hexformat;
|
||||
|
||||
import java.util.HexFormat;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class UppercaseLowercaseOutputUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenInitialisedHexFormat_whenByteArrayIsPassed_thenLowerCaseHexStringRepresentationIsReturned() {
|
||||
HexFormat hexFormat = HexFormat.of();
|
||||
String bytesAsString = hexFormat.formatHex(new byte[] { -85, -51, -17, 1, 35, 69, 103, -119});
|
||||
assertTrue(isLowerCase(bytesAsString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInitialisedHexFormatWithUpperCaseOption_whenByteArrayIsPassed_thenLowerCaseHexStringRepresentationIsReturned() {
|
||||
HexFormat hexFormat = HexFormat.of().withUpperCase();
|
||||
String bytesAsString = hexFormat.formatHex(new byte[] { -85, -51, -17, 1, 35, 69, 103, -119});
|
||||
assertTrue(isUpperCase(bytesAsString));
|
||||
}
|
||||
|
||||
private boolean isLowerCase(String str) {
|
||||
char[] charArray = str.toCharArray();
|
||||
for (int i=0; i < charArray.length; i++) {
|
||||
if (Character.isUpperCase(charArray[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isUpperCase(String str) {
|
||||
char[] charArray = str.toCharArray();
|
||||
for (int i=0; i < charArray.length; i++) {
|
||||
if (Character.isLowerCase(charArray[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -20,17 +20,10 @@
|
||||
<artifactId>icu4j</artifactId>
|
||||
<version>${icu.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<icu.version>64.2</icu.version>
|
||||
<assertj.version>3.12.2</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -25,12 +25,6 @@
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
@@ -63,8 +57,6 @@
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -25,12 +25,6 @@
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
@@ -64,8 +58,6 @@
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -31,13 +31,6 @@
|
||||
<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>
|
||||
@@ -50,11 +43,4 @@
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -21,12 +21,6 @@
|
||||
<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>
|
||||
@@ -68,8 +62,6 @@
|
||||
</pluginRepositories>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<awaitility.version>1.7.0</awaitility.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
|
||||
+2
@@ -16,6 +16,7 @@ import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.Ignore;
|
||||
|
||||
public class ModuleAPIUnitTest {
|
||||
|
||||
@@ -110,6 +111,7 @@ public class ModuleAPIUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore // fixing in http://team.baeldung.com/browse/JAVA-8679
|
||||
public void givenModules_whenAccessingModuleDescriptorProvides_thenProvidesAreReturned() {
|
||||
Set<Provides> javaBaseProvides = javaBaseModule.getDescriptor().provides();
|
||||
Set<Provides> javaSqlProvides = javaSqlModule.getDescriptor().provides();
|
||||
|
||||
@@ -20,12 +20,6 @@
|
||||
<artifactId>rxjava</artifactId>
|
||||
<version>${rxjava.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>
|
||||
@@ -156,8 +150,6 @@
|
||||
|
||||
<properties>
|
||||
<rxjava.version>3.0.0</rxjava.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<awaitility.version>4.0.2</awaitility.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
|
||||
@@ -21,12 +21,6 @@
|
||||
<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>
|
||||
@@ -78,14 +72,10 @@
|
||||
</pluginRepositories>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<awaitility.version>1.7.0</awaitility.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
<guava.version>25.1-jre</guava.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<commons-collections3.version>3.2.2</commons-collections3.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,7 +1,7 @@
|
||||
<?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">
|
||||
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-annotations</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
@@ -14,19 +14,6 @@
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-annotations</finalName>
|
||||
<resources>
|
||||
@@ -37,4 +24,4 @@
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
@@ -24,12 +24,6 @@
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.19.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -19,32 +19,18 @@
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
<version>${jmh-core.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
<jmh.version>1.33</jmh.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
||||
@@ -30,12 +30,6 @@
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -67,7 +61,6 @@
|
||||
|
||||
<properties>
|
||||
<shade.plugin.version>3.2.0</shade.plugin.version>
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -36,13 +36,6 @@
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<!-- Testing -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -75,7 +68,6 @@
|
||||
<properties>
|
||||
<shade.plugin.version>3.2.0</shade.plugin.version>
|
||||
<guava.version>28.2-jre</guava.version>
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -15,12 +15,6 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
@@ -33,8 +27,4 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -34,12 +34,6 @@
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.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>
|
||||
@@ -50,8 +44,6 @@
|
||||
|
||||
<properties>
|
||||
<eclipse.collections.version>7.1.0</eclipse.collections.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
<commons-exec.version>1.3</commons-exec.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
- [Performance of contains() in a HashSet vs ArrayList](https://www.baeldung.com/java-hashset-arraylist-contains-performance)
|
||||
- [Fail-Safe Iterator vs Fail-Fast Iterator](https://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator)
|
||||
- [Quick Guide to the Java Stack](https://www.baeldung.com/java-stack)
|
||||
- [Convert an Array of Primitives to a List](https://www.baeldung.com/java-primitive-array-to-list)
|
||||
- [A Guide to BitSet in Java](https://www.baeldung.com/java-bitset)
|
||||
- [Get the First Key and Value From a HashMap](https://www.baeldung.com/java-hashmap-get-first-entry)
|
||||
- [Performance of removeAll() in a HashSet](https://www.baeldung.com/java-hashset-removeall-performance)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-collections-2) [[next -->]](/core-java-modules/core-java-collections-4)
|
||||
|
||||
@@ -25,12 +25,6 @@
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
@@ -39,7 +33,6 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
<jol-core.version>0.10</jol-core.version>
|
||||
</properties>
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.stack;
|
||||
package com.baeldung.collections.stack;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
@@ -14,17 +14,4 @@
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.19.0</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.collections.mulipletypesinmap;
|
||||
|
||||
public interface DynamicTypeValue {
|
||||
String valueDescription();
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.collections.mulipletypesinmap;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class InstantTypeValue implements DynamicTypeValue {
|
||||
private static DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
||||
.withZone(ZoneId.systemDefault());
|
||||
private Instant value;
|
||||
|
||||
public InstantTypeValue(Instant value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String valueDescription() {
|
||||
if (value == null) {
|
||||
return "The value is null.";
|
||||
}
|
||||
return String.format("The value is an instant: %s", FORMATTER.format(value));
|
||||
}
|
||||
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.collections.mulipletypesinmap;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class IntArrayTypeValue implements DynamicTypeValue {
|
||||
private int[] value;
|
||||
|
||||
public IntArrayTypeValue(int[] value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String valueDescription() {
|
||||
if (value == null) {
|
||||
return "The value is null.";
|
||||
}
|
||||
return String.format("The value is an array of %d integers: %s", value.length, Arrays.toString(value));
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.collections.mulipletypesinmap;
|
||||
|
||||
public class IntegerTypeValue implements DynamicTypeValue {
|
||||
private Integer value;
|
||||
|
||||
public IntegerTypeValue(Integer value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String valueDescription() {
|
||||
if(value == null){
|
||||
return "The value is null.";
|
||||
}
|
||||
return String.format("The value is a %s integer: %d", value > 0 ? "positive" : "negative", value);
|
||||
}
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package com.baeldung.multipletypesinmap;
|
||||
|
||||
import com.baeldung.collections.mulipletypesinmap.DynamicTypeValue;
|
||||
import com.baeldung.collections.mulipletypesinmap.InstantTypeValue;
|
||||
import com.baeldung.collections.mulipletypesinmap.IntArrayTypeValue;
|
||||
import com.baeldung.collections.mulipletypesinmap.IntegerTypeValue;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class MultipleTypesInMapUnitTest {
|
||||
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
|
||||
.withZone(ZoneId.systemDefault());
|
||||
|
||||
private static final Integer intValue = 777;
|
||||
private static final int[] intArray = new int[]{2, 3, 5, 7, 11, 13};
|
||||
private static final Instant instant = Instant.now();
|
||||
|
||||
private static final String KEY_INT = "E1 (Integer)";
|
||||
private static final String KEY_INT_ARRAY = "E2 (IntArray)";
|
||||
private static final String KEY_INSTANT = "E3 (Instant)";
|
||||
|
||||
@Test
|
||||
void givenThreeTypes_whenUsingRawMap_thenPrintDescription() {
|
||||
Map<String, Object> rawMap = new HashMap<>();
|
||||
rawMap.put(KEY_INT, intValue);
|
||||
rawMap.put(KEY_INT_ARRAY, intArray);
|
||||
rawMap.put(KEY_INSTANT, instant);
|
||||
|
||||
rawMap.forEach((k, v) -> {
|
||||
if (v instanceof Integer) {
|
||||
Integer theV = (Integer) v;
|
||||
String desc = String.format("The value is a %s integer: %d", theV > 0 ? "positive" : "negative", theV);
|
||||
System.out.println(k + " -> " + desc);
|
||||
assertThat(k).isEqualTo(KEY_INT);
|
||||
assertThat(desc).isEqualTo("The value is a positive integer: 777");
|
||||
} else if (v instanceof int[]) {
|
||||
int[] theV = (int[]) v;
|
||||
String desc = String.format("The value is an array of %d integers: %s", theV.length, Arrays.toString(theV));
|
||||
System.out.println(k + " -> " + desc);
|
||||
assertThat(k).isEqualTo(KEY_INT_ARRAY);
|
||||
assertThat(desc).isEqualTo("The value is an array of 6 integers: [2, 3, 5, 7, 11, 13]");
|
||||
} else if (v instanceof Instant) {
|
||||
Instant theV = (Instant) v;
|
||||
String desc = String.format("The value is an instant: %s", FORMATTER.format(theV));
|
||||
System.out.println(k + " -> " + desc);
|
||||
assertThat(k).isEqualTo(KEY_INSTANT);
|
||||
assertThat(desc).matches("^The value is an instant: \\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}");
|
||||
} else {
|
||||
throw new IllegalStateException("Unknown Type found.");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenThreeTypes_whenUsingAnInterface_thenPrintDescription() {
|
||||
Map<String, DynamicTypeValue> theMap = new HashMap<>();
|
||||
theMap.put(KEY_INT, new IntegerTypeValue(intValue));
|
||||
theMap.put(KEY_INT_ARRAY, new IntArrayTypeValue(intArray));
|
||||
theMap.put(KEY_INSTANT, new InstantTypeValue(instant));
|
||||
|
||||
theMap.forEach((k, v) -> System.out.println(k + " -> " + v.valueDescription()));
|
||||
|
||||
assertThat(theMap.get(KEY_INT).valueDescription()).isEqualTo("The value is a positive integer: 777");
|
||||
assertThat(theMap.get(KEY_INT_ARRAY).valueDescription()).isEqualTo("The value is an array of 6 integers: [2, 3, 5, 7, 11, 13]");
|
||||
assertThat(theMap.get(KEY_INSTANT).valueDescription()).matches("^The value is an instant: \\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}");
|
||||
}
|
||||
}
|
||||
@@ -20,17 +20,6 @@
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -20,12 +20,6 @@
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
@@ -34,9 +28,4 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -26,12 +26,6 @@
|
||||
<version>${guava.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sf.trove4j</groupId>
|
||||
<artifactId>trove4j</artifactId>
|
||||
@@ -60,8 +54,6 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
<trove4j.version>3.0.2</trove4j.version>
|
||||
<fastutil.version>8.1.0</fastutil.version>
|
||||
<colt.version>1.2.0</colt.version>
|
||||
|
||||
@@ -25,17 +25,6 @@
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -51,22 +51,14 @@
|
||||
<version>${avaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<streamex.version>0.6.5</streamex.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
<eclipse-collections.version>8.2.0</eclipse-collections.version>
|
||||
<hppc.version>0.7.2</hppc.version>
|
||||
<fastutil.version>8.1.0</fastutil.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -4,3 +4,5 @@ This module contains articles about Map data structures in Java.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Using a Custom Class as a Key in a Java HashMap](https://www.baeldung.com/java-custom-class-map-key)
|
||||
- [Nested HashMaps Examples in Java](https://www.baeldung.com/java-nested-hashmaps)
|
||||
- [Java HashMap With Different Value Types](https://www.baeldung.com/java-hashmap-different-value-types)
|
||||
|
||||
@@ -1,34 +1,49 @@
|
||||
<?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-collections-maps-4</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-collections-maps-4</name>
|
||||
<packaging>jar</packaging>
|
||||
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-collections-maps-4</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-collections-maps-4</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0-M5</version>
|
||||
<configuration>
|
||||
<!-- those tests are not made for CI purposes, but for manual testing -->
|
||||
<groups>!performance</groups>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0-M5</version>
|
||||
<configuration>
|
||||
<!-- those tests are not made for CI purposes, but for manual testing -->
|
||||
<groups>!performance</groups>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.nestedhashmaps;
|
||||
|
||||
public class Address {
|
||||
|
||||
private Integer addressId;
|
||||
private String addressLocation;
|
||||
|
||||
public Address() {
|
||||
}
|
||||
|
||||
public Address(Integer addressId, String addressLocation) {
|
||||
this.addressId = addressId;
|
||||
this.addressLocation = addressLocation;
|
||||
}
|
||||
|
||||
public Integer getAddressId() {
|
||||
return addressId;
|
||||
}
|
||||
|
||||
public void setAddressId(Integer addressId) {
|
||||
this.addressId = addressId;
|
||||
}
|
||||
|
||||
public String getAddressLocation() {
|
||||
return addressLocation;
|
||||
}
|
||||
|
||||
public void setAddressLocation(String addressLocation) {
|
||||
this.addressLocation = addressLocation;
|
||||
}
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.nestedhashmaps;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private Integer employeeId;
|
||||
private Address address;
|
||||
private String employeeName;
|
||||
|
||||
public Employee() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Employee(Integer employeeId, Address address, String employeeName) {
|
||||
this.employeeId = employeeId;
|
||||
this.address = address;
|
||||
this.employeeName = employeeName;
|
||||
}
|
||||
|
||||
public Integer getEmployeeId() {
|
||||
return employeeId;
|
||||
}
|
||||
|
||||
public void setEmployeeId(Integer employeeId) {
|
||||
this.employeeId = employeeId;
|
||||
}
|
||||
|
||||
public Address getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(Address address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getEmployeeName() {
|
||||
return employeeName;
|
||||
}
|
||||
|
||||
public void setEmployeeName(String employeeName) {
|
||||
this.employeeName = employeeName;
|
||||
}
|
||||
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.nestedhashmaps;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MapsUtil {
|
||||
|
||||
MapsUtil() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Map<Integer, String> buildInnerMap(List<String> batterList) {
|
||||
Map<Integer, String> innerBatterMap = new HashMap<Integer, String>();
|
||||
|
||||
int index = 1;
|
||||
for (String item : batterList) {
|
||||
innerBatterMap.put(index, item);
|
||||
index++;
|
||||
}
|
||||
|
||||
return innerBatterMap;
|
||||
}
|
||||
|
||||
public Map<Integer, Map<String, String>> createNestedMapfromStream(List<Employee> listEmployee) {
|
||||
Map<Integer, Map<String, String>> employeeAddressMap = listEmployee.stream()
|
||||
.collect(Collectors.groupingBy(e -> e.getAddress().getAddressId(),
|
||||
Collectors.toMap(f -> f.getAddress().getAddressLocation(), Employee::getEmployeeName)));
|
||||
return employeeAddressMap;
|
||||
}
|
||||
|
||||
public Map<Integer, Map<Integer, Address>> createNestedObjectMap(List<Employee> listEmployee) {
|
||||
Map<Integer, Map<Integer, Address>> employeeMap = new HashMap<>();
|
||||
|
||||
employeeMap = listEmployee.stream().collect(Collectors.groupingBy((Employee emp) -> emp.getEmployeeId(),
|
||||
Collectors.toMap((Employee emp) -> emp.getAddress().getAddressId(), fEmpObj -> fEmpObj.getAddress())));
|
||||
|
||||
return employeeMap;
|
||||
}
|
||||
|
||||
public Map<String, String> flattenMap(Map<?, ?> source) {
|
||||
Map<String, String> converted = new HashMap<>();
|
||||
|
||||
for (Entry<?, ?> entry : source.entrySet()) {
|
||||
if (entry.getValue() instanceof Map) {
|
||||
flattenMap((Map<String, Object>) entry.getValue())
|
||||
.forEach((key, value) -> converted.put(entry.getKey() + "." + key, value));
|
||||
} else {
|
||||
converted.put(entry.getKey().toString(), entry.getValue().toString());
|
||||
}
|
||||
}
|
||||
return converted;
|
||||
}
|
||||
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package com.baeldung.nestedhashmaps;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class NestedHashMapExamplesClass {
|
||||
public static void main(String[] args) {
|
||||
|
||||
MapsUtil mUtil = new MapsUtil();
|
||||
|
||||
List<String> batterList = new ArrayList<>();
|
||||
Map<String, Map<Integer, String>> outerBakedGoodsMap = new HashMap<>();
|
||||
Map<String, Map<Integer, String>> outerBakedGoodsMap2 = new HashMap<>();
|
||||
Map<String, Map<Integer, String>> outerBakedGoodsMap3 = new HashMap<>();
|
||||
Map<String, Map<Integer, String>> outerBakedGoodsMap4 = new HashMap<>();
|
||||
|
||||
batterList.add("Mulberry");
|
||||
batterList.add("Cranberry");
|
||||
batterList.add("Blackberry");
|
||||
batterList.add("Mixed fruit");
|
||||
batterList.add("Orange");
|
||||
|
||||
outerBakedGoodsMap.put("Cake", mUtil.buildInnerMap(batterList));
|
||||
|
||||
batterList.clear();
|
||||
batterList.add("Candy");
|
||||
batterList.add("Dark Chocolate");
|
||||
batterList.add("Chocolate");
|
||||
batterList.add("Jam filled");
|
||||
batterList.add("Pineapple");
|
||||
|
||||
outerBakedGoodsMap.put("Donut", mUtil.buildInnerMap(batterList));
|
||||
|
||||
outerBakedGoodsMap2.put("Eclair", mUtil.buildInnerMap(batterList));
|
||||
outerBakedGoodsMap2.put("Donut", mUtil.buildInnerMap(batterList));
|
||||
|
||||
outerBakedGoodsMap3.put("Cake", mUtil.buildInnerMap(batterList));
|
||||
batterList.clear();
|
||||
batterList.add("Banana");
|
||||
batterList.add("Red Velvet");
|
||||
batterList.add("Blackberry");
|
||||
batterList.add("Passion fruit");
|
||||
batterList.add("Kiwi");
|
||||
|
||||
outerBakedGoodsMap3.put("Donut", mUtil.buildInnerMap(batterList));
|
||||
|
||||
outerBakedGoodsMap4.putAll(outerBakedGoodsMap);
|
||||
|
||||
System.out.println(outerBakedGoodsMap.equals(outerBakedGoodsMap2));
|
||||
System.out.println(outerBakedGoodsMap.equals(outerBakedGoodsMap3));
|
||||
System.out.println(outerBakedGoodsMap.equals(outerBakedGoodsMap4));
|
||||
|
||||
outerBakedGoodsMap.get("Cake")
|
||||
.put(6, "Cranberry");
|
||||
System.out.println(outerBakedGoodsMap);
|
||||
|
||||
outerBakedGoodsMap.get("Cake")
|
||||
.remove(5);
|
||||
System.out.println(outerBakedGoodsMap);
|
||||
|
||||
outerBakedGoodsMap.put("Eclair", new HashMap<Integer, String>() {
|
||||
{
|
||||
put(1, "Dark Chocolate");
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println(outerBakedGoodsMap);
|
||||
outerBakedGoodsMap.remove("Eclair");
|
||||
System.out.println(outerBakedGoodsMap);
|
||||
System.out.println("Baked Goods Map Flattened: " + mUtil.flattenMap(outerBakedGoodsMap));
|
||||
|
||||
// Employees Map
|
||||
List<Employee> listEmployee = new ArrayList<Employee>();
|
||||
|
||||
listEmployee.add(new Employee(1, new Address(124, "Timbuktoo"), "Thorin Oakenshield"));
|
||||
listEmployee.add(new Employee(2, new Address(100, "Misty Lanes"), "Balin"));
|
||||
listEmployee.add(new Employee(3, new Address(156, "Bramles Lane"), "Bofur"));
|
||||
listEmployee.add(new Employee(4, new Address(200, "Bag End"), "Bilbo Baggins"));
|
||||
listEmployee.add(new Employee(5, new Address(23, "Rivendell"), "Elrond"));
|
||||
|
||||
Map<Integer, Map<String, String>> employeeAddressMap = mUtil.createNestedMapfromStream(listEmployee);
|
||||
|
||||
Map<Integer, Map<Integer, Address>> employeeMap = mUtil.createNestedObjectMap(listEmployee);
|
||||
Map<Integer, Map<Integer, Address>> employeeMap2 = mUtil.createNestedObjectMap(listEmployee);
|
||||
|
||||
listEmployee.clear();
|
||||
listEmployee.add(new Employee(1, new Address(500, "Timbuktoo"), "Thorin Oakenshield"));
|
||||
listEmployee.add(new Employee(2, new Address(600, "Misty Lanes"), "Balin"));
|
||||
listEmployee.add(new Employee(3, new Address(700, "Bramles Lane"), "Bofur"));
|
||||
listEmployee.add(new Employee(4, new Address(800, "Bag End"), "Bilbo Baggins"));
|
||||
listEmployee.add(new Employee(5, new Address(900, "Rivendell"), "Elrond"));
|
||||
|
||||
Map<Integer, Map<String, String>> employeeAddressMap1 = mUtil.createNestedMapfromStream(listEmployee);
|
||||
|
||||
Map<Integer, Map<Integer, Address>> employeeMap1 = mUtil.createNestedObjectMap(listEmployee);
|
||||
|
||||
System.out.println(employeeMap.equals(employeeMap1));
|
||||
System.out.println(employeeMap.equals(employeeMap2));
|
||||
|
||||
for (Map.Entry<String, Map<Integer, String>> outerBakedGoodsMapEntrySet : outerBakedGoodsMap.entrySet()) {
|
||||
Map<Integer, String> valueMap = outerBakedGoodsMapEntrySet.getValue();
|
||||
System.out.println(valueMap.entrySet());
|
||||
}
|
||||
|
||||
for (Map.Entry<Integer, Map<String, String>> employeeEntrySet : employeeAddressMap.entrySet()) {
|
||||
Map<String, String> valueMap = employeeEntrySet.getValue();
|
||||
System.out.println(valueMap.entrySet());
|
||||
}
|
||||
|
||||
System.out.println("Employee Address Map Flattened: " + mUtil.flattenMap(employeeAddressMap));
|
||||
|
||||
System.out.println(employeeAddressMap.equals(employeeAddressMap1));
|
||||
}
|
||||
|
||||
}
|
||||
+243
@@ -0,0 +1,243 @@
|
||||
package com.baeldung.nestedhashmaps;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import org.hamcrest.collection.IsMapContaining;
|
||||
|
||||
public class NestedHashMapExamplesClassUnitTest {
|
||||
private MapsUtil mUtil = new MapsUtil();
|
||||
private List<String> batterList = new ArrayList<>();
|
||||
private List<Employee> listEmployee = new ArrayList<Employee>();
|
||||
private Map<String, Map<Integer, String>> actualBakedGoodsMap = new HashMap<>();
|
||||
private Map<Integer, Map<String, String>> actualEmployeeAddressMap = new HashMap<>();
|
||||
private Map<Integer, Map<Integer, Address>> actualEmployeeMap = new HashMap<>();
|
||||
|
||||
@Test
|
||||
public void whenCreateNestedHashMap_thenNestedMap() {
|
||||
assertThat(mUtil.buildInnerMap(batterList), is(notNullValue()));
|
||||
Assert.assertEquals(actualBakedGoodsMap.keySet().size(), 2);
|
||||
Assert.assertThat(actualBakedGoodsMap, IsMapContaining.hasValue(equalTo(mUtil.buildInnerMap(batterList))));
|
||||
}
|
||||
|
||||
private Map<Integer, Map<String, String>> setup() {
|
||||
Map<Integer, Map<String, String>> expectedMap = new HashMap<>();
|
||||
expectedMap.put(Integer.valueOf(100), new HashMap<String, String>() {
|
||||
{
|
||||
put("Misty Lanes", "Balin");
|
||||
}
|
||||
});
|
||||
expectedMap.put(Integer.valueOf(200), new HashMap<String, String>() {
|
||||
{
|
||||
put("Bag End", "Bilbo Baggins");
|
||||
}
|
||||
});
|
||||
expectedMap.put(Integer.valueOf(156), new HashMap<String, String>() {
|
||||
{
|
||||
put("Brambles Lane", "Bofur");
|
||||
}
|
||||
});
|
||||
expectedMap.put(Integer.valueOf(124), new HashMap<String, String>() {
|
||||
{
|
||||
put("Timbuktoo", "Thorin Oakenshield");
|
||||
}
|
||||
});
|
||||
expectedMap.put(Integer.valueOf(23), new HashMap<String, String>() {
|
||||
{
|
||||
put("Rivendell", "Elrond");
|
||||
}
|
||||
});
|
||||
|
||||
return expectedMap;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateNestedHashMapwithStreams_thenNestedMap() {
|
||||
|
||||
Map<Integer, Map<String, String>> expectedMap = setup();
|
||||
|
||||
assertThat(actualEmployeeAddressMap, equalTo(expectedMap));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCompareTwoHashMapswithDifferenctValues_usingEquals_thenFail() {
|
||||
Map<String, Map<Integer, String>> outerBakedGoodsMap2 = new HashMap<>();
|
||||
outerBakedGoodsMap2.put("Eclair", mUtil.buildInnerMap(batterList));
|
||||
outerBakedGoodsMap2.put("Donut", mUtil.buildInnerMap(batterList));
|
||||
assertNotEquals(outerBakedGoodsMap2, actualBakedGoodsMap);
|
||||
|
||||
Map<String, Map<Integer, String>> outerBakedGoodsMap3 = new HashMap<String, Map<Integer, String>>();
|
||||
outerBakedGoodsMap3.put("Cake", mUtil.buildInnerMap(batterList));
|
||||
batterList = new ArrayList<>();
|
||||
batterList = Arrays.asList("Banana", "Red Velvet", "Blackberry", "Passion fruit", "Kiwi");
|
||||
|
||||
outerBakedGoodsMap3.put("Donut", mUtil.buildInnerMap(batterList));
|
||||
|
||||
assertNotEquals(outerBakedGoodsMap2, actualBakedGoodsMap);
|
||||
|
||||
listEmployee.clear();
|
||||
listEmployee.add(new Employee(1, new Address(500, "Timbuktoo"), "Thorin Oakenshield"));
|
||||
listEmployee.add(new Employee(2, new Address(600, "Misty Lanes"), "Balin"));
|
||||
listEmployee.add(new Employee(3, new Address(700, "Bramles Lane"), "Bofur"));
|
||||
listEmployee.add(new Employee(4, new Address(800, "Bag End"), "Bilbo Baggins"));
|
||||
listEmployee.add(new Employee(5, new Address(900, "Rivendell"), "Elrond"));
|
||||
|
||||
Map<Integer, Map<String, String>> employeeAddressMap1 = mUtil.createNestedMapfromStream(listEmployee);
|
||||
|
||||
Map<Integer, Map<Integer, Address>> employeeMap1 = mUtil.createNestedObjectMap(listEmployee);
|
||||
|
||||
assertNotEquals(employeeAddressMap1, actualEmployeeAddressMap);
|
||||
|
||||
assertNotEquals(employeeMap1, actualEmployeeMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whencomparingDifferentObjectValuesUsingEquals_thenFail() {
|
||||
listEmployee.clear();
|
||||
listEmployee.add(new Employee(1, new Address(124, "Timbuktoo"), "Thorin Oakenshield"));
|
||||
listEmployee.add(new Employee(2, new Address(100, "Misty Lanes"), "Balin"));
|
||||
listEmployee.add(new Employee(3, new Address(156, "Brambles Lane"), "Bofur"));
|
||||
listEmployee.add(new Employee(4, new Address(200, "Bag End"), "Bilbo Baggins"));
|
||||
listEmployee.add(new Employee(5, new Address(23, "Rivendell"), "Elrond"));
|
||||
|
||||
Map<Integer, Map<Integer, Object>> employeeMap1 = listEmployee.stream().collect(Collectors.groupingBy(
|
||||
(Employee emp) -> emp.getEmployeeId(),
|
||||
Collectors.toMap((Employee emp) -> emp.getAddress().getAddressId(), fEmpObj -> fEmpObj.getAddress())));
|
||||
|
||||
assertNotSame(employeeMap1, actualEmployeeMap);
|
||||
assertNotEquals(employeeMap1, actualEmployeeMap);
|
||||
|
||||
Map<Integer, Map<Integer, Address>> expectedMap = setupAddressObjectMap();
|
||||
assertNotSame(expectedMap, actualEmployeeMap);
|
||||
assertNotEquals(expectedMap, actualEmployeeMap);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCompareTwoHashMapsUsingEquals_thenSuccess() {
|
||||
Map<String, Map<Integer, String>> outerBakedGoodsMap4 = new HashMap<>();
|
||||
outerBakedGoodsMap4.putAll(actualBakedGoodsMap);
|
||||
|
||||
assertEquals(actualBakedGoodsMap, outerBakedGoodsMap4);
|
||||
|
||||
Map<Integer, Map<Integer, Address>> employeeMap1 = new HashMap<>();
|
||||
employeeMap1.putAll(actualEmployeeMap);
|
||||
assertEquals(actualEmployeeMap, employeeMap1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddElementinHashMaps_thenSuccess() {
|
||||
assertEquals(actualBakedGoodsMap.get("Cake").size(), 5);
|
||||
actualBakedGoodsMap.get("Cake").put(6, "Cranberry");
|
||||
assertEquals(actualBakedGoodsMap.get("Cake").size(), 6);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeleteElementinHashMaps_thenSuccess() {
|
||||
assertNotEquals(actualBakedGoodsMap.get("Cake").get(5), null);
|
||||
actualBakedGoodsMap.get("Cake").remove(5);
|
||||
assertEquals(actualBakedGoodsMap.get("Cake").get(5), null);
|
||||
|
||||
actualBakedGoodsMap.put("Eclair", new HashMap<Integer, String>() {
|
||||
{
|
||||
put(1, "Dark Chocolate");
|
||||
}
|
||||
});
|
||||
|
||||
assertNotEquals(actualBakedGoodsMap.get("Eclair").get(1), null);
|
||||
actualBakedGoodsMap.get("Eclair").remove(1);
|
||||
assertEquals(actualBakedGoodsMap.get("Eclair").get(1), null);
|
||||
|
||||
actualBakedGoodsMap.put("Eclair", new HashMap<Integer, String>() {
|
||||
{
|
||||
put(1, "Dark Chocolate");
|
||||
}
|
||||
});
|
||||
|
||||
assertNotEquals(actualBakedGoodsMap.get("Eclair"), null);
|
||||
actualBakedGoodsMap.remove("Eclair");
|
||||
assertEquals(actualBakedGoodsMap.get("Eclair"), null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFlattenMap_thenRemovesNesting() {
|
||||
|
||||
Map<String, String> flattenedBakedGoodsMap = mUtil.flattenMap(actualBakedGoodsMap);
|
||||
assertThat(flattenedBakedGoodsMap, IsMapContaining.hasKey("Donut.2"));
|
||||
|
||||
Map<String, String> flattenedEmployeeAddressMap = mUtil.flattenMap(actualEmployeeAddressMap);
|
||||
assertThat(flattenedEmployeeAddressMap, IsMapContaining.hasKey("200.Bag End"));
|
||||
}
|
||||
|
||||
@Before
|
||||
public void buildMaps() {
|
||||
|
||||
batterList = Arrays.asList("Mulberry", "Cranberry", "Blackberry", "Mixed fruit", "Orange");
|
||||
|
||||
actualBakedGoodsMap.put("Cake", mUtil.buildInnerMap(batterList));
|
||||
|
||||
batterList = new ArrayList<>();
|
||||
batterList = Arrays.asList("Candy", "Dark Chocolate", "Chocolate", "Jam filled", "Pineapple");
|
||||
|
||||
actualBakedGoodsMap.put("Donut", mUtil.buildInnerMap(batterList));
|
||||
|
||||
listEmployee.add(new Employee(1, new Address(124, "Timbuktoo"), "Thorin Oakenshield"));
|
||||
listEmployee.add(new Employee(2, new Address(100, "Misty Lanes"), "Balin"));
|
||||
listEmployee.add(new Employee(3, new Address(156, "Brambles Lane"), "Bofur"));
|
||||
listEmployee.add(new Employee(4, new Address(200, "Bag End"), "Bilbo Baggins"));
|
||||
listEmployee.add(new Employee(5, new Address(23, "Rivendell"), "Elrond"));
|
||||
|
||||
actualEmployeeAddressMap = mUtil.createNestedMapfromStream(listEmployee);
|
||||
|
||||
actualEmployeeMap = mUtil.createNestedObjectMap(listEmployee);
|
||||
|
||||
}
|
||||
|
||||
private Map<Integer, Map<Integer, Address>> setupAddressObjectMap() {
|
||||
|
||||
Map<Integer, Map<Integer, Address>> expectedMap = new HashMap<>();
|
||||
|
||||
expectedMap.put(1, new HashMap<Integer, Address>() {
|
||||
{
|
||||
put(124, new Address(124, "Timbuktoo"));
|
||||
}
|
||||
});
|
||||
expectedMap.put(2, new HashMap<Integer, Address>() {
|
||||
{
|
||||
put(100, new Address(100, "Misty Lanes"));
|
||||
}
|
||||
});
|
||||
expectedMap.put(3, new HashMap<Integer, Address>() {
|
||||
{
|
||||
put(156, new Address(156, "Brambles Lane"));
|
||||
}
|
||||
});
|
||||
expectedMap.put(4, new HashMap<Integer, Address>() {
|
||||
{
|
||||
put(200, new Address(200, "Bag End"));
|
||||
}
|
||||
});
|
||||
expectedMap.put(5, new HashMap<Integer, Address>() {
|
||||
{
|
||||
put(23, new Address(23, "Rivendell"));
|
||||
}
|
||||
});
|
||||
return expectedMap;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,17 +20,6 @@
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -50,7 +49,6 @@
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<commons-collections4.version>4.3</commons-collections4.version>
|
||||
<gson.version>2.8.5</gson.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -12,4 +12,5 @@ This module contains articles about Java collections
|
||||
- [Defining a Char Stack in Java](https://www.baeldung.com/java-char-stack)
|
||||
- [Guide to the Java Queue Interface](https://www.baeldung.com/java-queue)
|
||||
- [An Introduction to Synchronized Java Collections](https://www.baeldung.com/java-synchronized-collections)
|
||||
- [[More -->]](/core-java-modules/core-java-collections-2)
|
||||
- [Convert an Array of Primitives to a List](https://www.baeldung.com/java-primitive-array-to-list)
|
||||
- More articles: [[next -->]](/core-java-modules/core-java-collections-2)
|
||||
|
||||
@@ -15,12 +15,6 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
@@ -31,10 +25,11 @@
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+5
-3
@@ -1,13 +1,15 @@
|
||||
package com.baeldung.collections.iterators;
|
||||
package com.baeldung.collections.convertarrayprimitives;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
|
||||
import com.google.common.primitives.Ints;
|
||||
|
||||
public class ConvertPrimitivesArrayToList {
|
||||
|
||||
public static void failConvert() {
|
||||
+5
-8
@@ -1,14 +1,11 @@
|
||||
package com.baeldung.collections.iterators;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
import com.google.common.primitives.Ints;
|
||||
import org.junit.Test;
|
||||
package com.baeldung.collections.convertarrayprimitives;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ConvertPrimitivesArrayToListUnitTest {
|
||||
|
||||
@Test
|
||||
@@ -15,11 +15,6 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.thread-weaver</groupId>
|
||||
<artifactId>threadweaver</artifactId>
|
||||
@@ -31,6 +26,12 @@
|
||||
<artifactId>tempus-fugit</artifactId>
|
||||
<version>${tempus-fugit.version}</version>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.multithreadedtc</groupId>
|
||||
|
||||
@@ -30,12 +30,6 @@
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -48,8 +42,4 @@
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -15,12 +15,6 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jcabi</groupId>
|
||||
<artifactId>jcabi-aspects</artifactId>
|
||||
@@ -94,7 +88,6 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.14.0</assertj.version>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<jcabi-aspects.version>0.22.6</jcabi-aspects.version>
|
||||
|
||||
+7
-10
@@ -3,17 +3,18 @@ package com.baeldung.abaproblem;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly;
|
||||
|
||||
public class Account {
|
||||
|
||||
private AtomicInteger balance;
|
||||
private AtomicInteger transactionCount;
|
||||
private ThreadLocal<Integer> currentThreadCASFailureCount;
|
||||
private final AtomicInteger balance;
|
||||
private final AtomicInteger transactionCount;
|
||||
private final ThreadLocal<Integer> currentThreadCASFailureCount;
|
||||
|
||||
public Account() {
|
||||
this.balance = new AtomicInteger(0);
|
||||
this.transactionCount = new AtomicInteger(0);
|
||||
this.currentThreadCASFailureCount = new ThreadLocal<>();
|
||||
this.currentThreadCASFailureCount.set(0);
|
||||
this.currentThreadCASFailureCount = ThreadLocal.withInitial(() -> 0);
|
||||
}
|
||||
|
||||
public int getBalance() {
|
||||
@@ -43,11 +44,7 @@ public class Account {
|
||||
|
||||
private void maybeWait() {
|
||||
if ("thread1".equals(Thread.currentThread().getName())) {
|
||||
try {
|
||||
TimeUnit.SECONDS.sleep(2);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
sleepUninterruptibly(2, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+23
-17
@@ -1,8 +1,13 @@
|
||||
package com.baeldung.abaproblem;
|
||||
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -30,45 +35,39 @@ public class AccountUnitTest {
|
||||
assertTrue(account.deposit(moneyToDeposit));
|
||||
|
||||
assertEquals(moneyToDeposit, account.getBalance());
|
||||
assertEquals(1, account.getTransactionCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withdrawTest() throws InterruptedException {
|
||||
public void withdrawTest() {
|
||||
final int defaultBalance = 50;
|
||||
final int moneyToWithdraw = 20;
|
||||
|
||||
account.deposit(defaultBalance);
|
||||
|
||||
assertTrue(account.withdraw(moneyToWithdraw));
|
||||
|
||||
assertEquals(defaultBalance - moneyToWithdraw, account.getBalance());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void abaProblemTest() throws InterruptedException {
|
||||
public void abaProblemTest() throws Exception {
|
||||
final int defaultBalance = 50;
|
||||
|
||||
final int amountToWithdrawByThread1 = 20;
|
||||
final int amountToWithdrawByThread2 = 10;
|
||||
final int amountToDepositByThread2 = 10;
|
||||
|
||||
assertEquals(0, account.getTransactionCount());
|
||||
assertEquals(0, account.getCurrentThreadCASFailureCount());
|
||||
account.deposit(defaultBalance);
|
||||
assertEquals(1, account.getTransactionCount());
|
||||
|
||||
Thread thread1 = new Thread(() -> {
|
||||
|
||||
Runnable thread1 = () -> {
|
||||
// this will take longer due to the name of the thread
|
||||
assertTrue(account.withdraw(amountToWithdrawByThread1));
|
||||
|
||||
// thread 1 fails to capture ABA problem
|
||||
assertNotEquals(1, account.getCurrentThreadCASFailureCount());
|
||||
};
|
||||
|
||||
}, "thread1");
|
||||
|
||||
Thread thread2 = new Thread(() -> {
|
||||
|
||||
Runnable thread2 = () -> {
|
||||
assertTrue(account.deposit(amountToDepositByThread2));
|
||||
assertEquals(defaultBalance + amountToDepositByThread2, account.getBalance());
|
||||
|
||||
@@ -79,12 +78,13 @@ public class AccountUnitTest {
|
||||
assertEquals(defaultBalance, account.getBalance());
|
||||
|
||||
assertEquals(0, account.getCurrentThreadCASFailureCount());
|
||||
}, "thread2");
|
||||
};
|
||||
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
thread1.join();
|
||||
thread2.join();
|
||||
Future<?> future1 = getSingleThreadExecutorService("thread1").submit(thread1);
|
||||
Future<?> future2 = getSingleThreadExecutorService("thread2").submit(thread2);
|
||||
|
||||
future1.get();
|
||||
future2.get();
|
||||
|
||||
// compareAndSet operation succeeds for thread 1
|
||||
assertEquals(defaultBalance - amountToWithdrawByThread1, account.getBalance());
|
||||
@@ -95,4 +95,10 @@ public class AccountUnitTest {
|
||||
// thread 2 did two modifications as well
|
||||
assertEquals(4, account.getTransactionCount());
|
||||
}
|
||||
|
||||
private static ExecutorService getSingleThreadExecutorService(String threadName) {
|
||||
return Executors.newSingleThreadExecutor(
|
||||
new ThreadFactoryBuilder().setNameFormat(threadName).build()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,12 +35,6 @@
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
@@ -60,13 +54,9 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- util -->
|
||||
<guava.version>21.0</guava.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -20,12 +20,6 @@
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
@@ -45,8 +39,6 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -29,18 +29,10 @@
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<guava.version>28.2-jre</guava.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -14,15 +14,6 @@
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-concurrency-collections</finalName>
|
||||
<resources>
|
||||
@@ -33,9 +24,4 @@
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -26,16 +25,11 @@
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.darwinsys</groupId>
|
||||
<artifactId>hirondelle-date4j</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<version>${hirondelle-date4j.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
@@ -63,8 +57,7 @@
|
||||
|
||||
<properties>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<hirondelle-date4j.version>RELEASE</hirondelle-date4j.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
@@ -30,18 +30,11 @@
|
||||
<artifactId>hirondelle-date4j</artifactId>
|
||||
<version>${hirondelle-date4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<hirondelle-date4j.version>1.5.1</hirondelle-date4j.version>
|
||||
<assertj.version>3.14.0</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -31,13 +31,6 @@
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -63,8 +56,6 @@
|
||||
|
||||
<properties>
|
||||
<joda-time.version>2.10</joda-time.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
@@ -32,13 +31,6 @@
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
@@ -47,7 +39,7 @@
|
||||
<dependency>
|
||||
<groupId>com.darwinsys</groupId>
|
||||
<artifactId>hirondelle-date4j</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<version>${hirondelle-date4j.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
@@ -76,8 +68,7 @@
|
||||
<properties>
|
||||
<commons-validator.version>1.6</commons-validator.version>
|
||||
<joda-time.version>2.10.10</joda-time.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<hirondelle-date4j.version>RELEASE</hirondelle-date4j.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
@@ -15,24 +15,11 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons.lang3.version}</version>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<commons.lang3.version>3.10</commons.lang3.version>
|
||||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -19,21 +19,13 @@
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.191</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<version>${h2.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
<h2.version>1.4.191</h2.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+5
-3
@@ -4,7 +4,9 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SynchronizedReceiver implements Runnable {
|
||||
private static Logger log = LoggerFactory.getLogger(SynchronizedReceiver.class);
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SynchronizedReceiver.class);
|
||||
|
||||
private final Data data;
|
||||
private String message;
|
||||
private boolean illegalMonitorStateExceptionOccurred;
|
||||
@@ -20,10 +22,10 @@ public class SynchronizedReceiver implements Runnable {
|
||||
data.wait();
|
||||
this.message = data.receive();
|
||||
} catch (InterruptedException e) {
|
||||
log.error("thread was interrupted", e);
|
||||
LOG.error("thread was interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IllegalMonitorStateException e) {
|
||||
log.error("illegal monitor state exception occurred", e);
|
||||
LOG.error("illegal monitor state exception occurred", e);
|
||||
illegalMonitorStateExceptionOccurred = true;
|
||||
}
|
||||
}
|
||||
|
||||
+5
-3
@@ -4,7 +4,9 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SynchronizedSender implements Runnable {
|
||||
private static Logger log = LoggerFactory.getLogger(SynchronizedSender.class);
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(SynchronizedSender.class);
|
||||
|
||||
private final Data data;
|
||||
private boolean illegalMonitorStateExceptionOccurred;
|
||||
|
||||
@@ -22,10 +24,10 @@ public class SynchronizedSender implements Runnable {
|
||||
|
||||
data.notifyAll();
|
||||
} catch (InterruptedException e) {
|
||||
log.error("thread was interrupted", e);
|
||||
LOG.error("thread was interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IllegalMonitorStateException e) {
|
||||
log.error("illegal monitor state exception occurred", e);
|
||||
LOG.error("illegal monitor state exception occurred", e);
|
||||
illegalMonitorStateExceptionOccurred = true;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-3
@@ -4,7 +4,8 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class UnsynchronizedReceiver implements Runnable {
|
||||
private static Logger log = LoggerFactory.getLogger(UnsynchronizedReceiver.class);
|
||||
private static final Logger LOG = LoggerFactory.getLogger(UnsynchronizedReceiver.class);
|
||||
|
||||
private final Data data;
|
||||
private String message;
|
||||
private boolean illegalMonitorStateExceptionOccurred;
|
||||
@@ -19,10 +20,10 @@ public class UnsynchronizedReceiver implements Runnable {
|
||||
data.wait();
|
||||
this.message = data.receive();
|
||||
} catch (InterruptedException e) {
|
||||
log.error("thread was interrupted", e);
|
||||
LOG.error("thread was interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IllegalMonitorStateException e) {
|
||||
log.error("illegal monitor state exception occurred", e);
|
||||
LOG.error("illegal monitor state exception occurred", e);
|
||||
illegalMonitorStateExceptionOccurred = true;
|
||||
}
|
||||
}
|
||||
|
||||
+4
-3
@@ -4,7 +4,8 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class UnsynchronizedSender implements Runnable {
|
||||
private static Logger log = LoggerFactory.getLogger(UnsynchronizedSender.class);
|
||||
private static final Logger LOG = LoggerFactory.getLogger(UnsynchronizedSender.class);
|
||||
|
||||
private final Data data;
|
||||
private boolean illegalMonitorStateExceptionOccurred;
|
||||
|
||||
@@ -21,10 +22,10 @@ public class UnsynchronizedSender implements Runnable {
|
||||
|
||||
data.notifyAll();
|
||||
} catch (InterruptedException e) {
|
||||
log.error("thread was interrupted", e);
|
||||
LOG.error("thread was interrupted", e);
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (IllegalMonitorStateException e) {
|
||||
log.error("illegal monitor state exception occurred", e);
|
||||
LOG.error("illegal monitor state exception occurred", e);
|
||||
illegalMonitorStateExceptionOccurred = true;
|
||||
}
|
||||
}
|
||||
|
||||
+8
-6
@@ -1,8 +1,11 @@
|
||||
package com.baeldung.exceptions.illegalmonitorstate;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class IllegalMonitorStateExceptionUnitTest {
|
||||
|
||||
@@ -18,10 +21,9 @@ public class IllegalMonitorStateExceptionUnitTest {
|
||||
Thread senderThread = new Thread(sender, "sender-thread");
|
||||
senderThread.start();
|
||||
|
||||
senderThread.join(1000);
|
||||
receiverThread.join(1000);
|
||||
|
||||
Thread.sleep(2000);
|
||||
// we need to wait for the sender and receiver threads to finish
|
||||
senderThread.join(10_000);
|
||||
receiverThread.join(10_000);
|
||||
|
||||
assertEquals("test", receiver.getMessage());
|
||||
assertFalse(sender.hasIllegalMonitorStateExceptionOccurred());
|
||||
|
||||
@@ -30,22 +30,12 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons.lang3.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<javax.mail.version>1.5.0-b01</javax.mail.version>
|
||||
<commons.lang3.version>3.10</commons.lang3.version>
|
||||
<!-- testing -->
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -14,16 +14,6 @@
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-function</finalName>
|
||||
<resources>
|
||||
@@ -34,9 +24,4 @@
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user