Skip to content

Commit 3b948c9

Browse files
committed
Merge remote-tracking branch 'upstream/master' into craedel-enterprise-patterns/front-controller
2 parents 51c438b + 5b29f1b commit 3b948c9

239 files changed

Lines changed: 6828 additions & 3235 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎core-java-9/.gitignore‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear

‎core-java-9/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=========
2+
3+
## Core Java 9 Examples
4+
5+
http://inprogress.baeldung.com/java-9-new-features/

‎core-java-9/pom.xml‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>core-java9</artifactId>
7+
<version>0.2-SNAPSHOT</version>
8+
9+
<name>core-java9</name>
10+
11+
<pluginRepositories>
12+
<pluginRepository>
13+
<id>apache.snapshots</id>
14+
<url>http://repository.apache.org/snapshots/</url>
15+
</pluginRepository>
16+
</pluginRepositories>
17+
18+
<dependencies>
19+
20+
<dependency>
21+
<groupId>org.slf4j</groupId>
22+
<artifactId>slf4j-api</artifactId>
23+
<version>${org.slf4j.version}</version>
24+
</dependency>
25+
26+
27+
28+
<dependency>
29+
<groupId>org.hamcrest</groupId>
30+
<artifactId>hamcrest-library</artifactId>
31+
<version>${org.hamcrest.version}</version>
32+
<scope>test</scope>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<version>${junit.version}</version>
39+
<scope>test</scope>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.mockito</groupId>
44+
<artifactId>mockito-core</artifactId>
45+
<version>${mockito.version}</version>
46+
<scope>test</scope>
47+
</dependency>
48+
49+
</dependencies>
50+
51+
<build>
52+
<finalName>core-java-9</finalName>
53+
54+
<plugins>
55+
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-compiler-plugin</artifactId>
59+
<version>${maven-compiler-plugin.version}</version>
60+
<configuration>
61+
<source>1.9</source>
62+
<target>1.9</target>
63+
64+
<verbose>true</verbose>
65+
<!-- <executable>C:\develop\jdks\jdk-9_ea122\bin\javac</executable>
66+
<compilerVersion>1.9</compilerVersion> -->
67+
</configuration>
68+
69+
</plugin>
70+
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-surefire-plugin</artifactId>
74+
<version>${maven-surefire-plugin.version}</version>
75+
</plugin>
76+
77+
</plugins>
78+
79+
</build>
80+
81+
<properties>
82+
<!-- logging -->
83+
<org.slf4j.version>1.7.13</org.slf4j.version>
84+
<logback.version>1.0.13</logback.version>
85+
86+
87+
<!-- maven plugins -->
88+
<!--
89+
<maven-war-plugin.version>2.6</maven-war-plugin.version>
90+
maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version> -->
91+
<maven-compiler-plugin.version>3.6-jigsaw-SNAPSHOT</maven-compiler-plugin.version>
92+
93+
94+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
95+
96+
<!-- testing -->
97+
<org.hamcrest.version>1.3</org.hamcrest.version>
98+
<junit.version>4.12</junit.version>
99+
<mockito.version>1.10.19</mockito.version>
100+
</properties>
101+
102+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.java9.language;
2+
3+
public interface PrivateInterface {
4+
5+
private static String staticPrivate() {
6+
return "static private";
7+
}
8+
9+
private String instancePrivate() {
10+
return "instance private";
11+
}
12+
13+
public default void check(){
14+
String result = staticPrivate();
15+
if (!result.equals("static private"))
16+
throw new AssertionError("Incorrect result for static private interface method");
17+
PrivateInterface pvt = new PrivateInterface() {
18+
};
19+
result = pvt.instancePrivate();
20+
if (!result.equals("instance private"))
21+
throw new AssertionError("Incorrect result for instance private interface method");
22+
}
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.java9.process;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.security.NoSuchAlgorithmException;
6+
import java.security.SecureRandom;
7+
import java.time.Duration;
8+
import java.time.Instant;
9+
import java.util.stream.Stream;
10+
11+
12+
public class ProcessUtils {
13+
14+
public static String getClassPath(){
15+
String cp = System.getProperty("java.class.path");
16+
System.out.println("ClassPath is "+cp);
17+
return cp;
18+
}
19+
20+
public static File getJavaCmd() throws IOException{
21+
String javaHome = System.getProperty("java.home");
22+
File javaCmd;
23+
if(System.getProperty("os.name").startsWith("Win")){
24+
javaCmd = new File(javaHome, "bin/java.exe");
25+
}else{
26+
javaCmd = new File(javaHome, "bin/java");
27+
}
28+
if(javaCmd.canExecute()){
29+
return javaCmd;
30+
}else{
31+
throw new UnsupportedOperationException(javaCmd.getCanonicalPath() + " is not executable");
32+
}
33+
}
34+
35+
public static String getMainClass(){
36+
return System.getProperty("sun.java.command");
37+
}
38+
39+
public static String getSystemProperties(){
40+
StringBuilder sb = new StringBuilder();
41+
System.getProperties().forEach((s1, s2) -> sb.append(s1 +" - "+ s2) );
42+
return sb.toString();
43+
}
44+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.java9.process;
2+
3+
import java.util.Optional;
4+
5+
public class ServiceMain {
6+
7+
public static void main(String[] args) throws InterruptedException {
8+
ProcessHandle thisProcess = ProcessHandle.current();
9+
long pid = thisProcess.getPid();
10+
11+
12+
Optional<String[]> opArgs = Optional.ofNullable(args);
13+
String procName = opArgs.map(str -> str.length > 0 ? str[0] : null).orElse(System.getProperty("sun.java.command"));
14+
15+
for (int i = 0; i < 10000; i++) {
16+
System.out.println("Process " + procName + " with ID " + pid + " is running!");
17+
Thread.sleep(10000);
18+
}
19+
20+
}
21+
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<configuration>
2+
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<!-- <logger name="org.springframework" level="WARN" /> -->
11+
12+
<root level="INFO">
13+
<appender-ref ref="STDOUT"/>
14+
</root>
15+
16+
</configuration>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.baeldung.java8;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.Optional;
8+
import java.util.stream.Collectors;
9+
import java.util.stream.Stream;
10+
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
14+
public class Java9OptionalsStreamTest {
15+
16+
private static List<Optional<String>> listOfOptionals = Arrays.asList(Optional.empty(), Optional.of("foo"), Optional.empty(), Optional.of("bar"));
17+
18+
@Test
19+
public void filterOutPresentOptionalsWithFilter() {
20+
assertEquals(4, listOfOptionals.size());
21+
22+
List<String> filteredList = listOfOptionals.stream()
23+
.filter(Optional::isPresent)
24+
.map(Optional::get)
25+
.collect(Collectors.toList());
26+
27+
assertEquals(2, filteredList.size());
28+
assertEquals("foo", filteredList.get(0));
29+
assertEquals("bar", filteredList.get(1));
30+
}
31+
32+
@Test
33+
public void filterOutPresentOptionalsWithFlatMap() {
34+
assertEquals(4, listOfOptionals.size());
35+
36+
List<String> filteredList = listOfOptionals.stream()
37+
.flatMap(o -> o.isPresent() ? Stream.of(o.get()) : Stream.empty())
38+
.collect(Collectors.toList());
39+
assertEquals(2, filteredList.size());
40+
41+
assertEquals("foo", filteredList.get(0));
42+
assertEquals("bar", filteredList.get(1));
43+
}
44+
45+
@Test
46+
public void filterOutPresentOptionalsWithJava9() {
47+
assertEquals(4, listOfOptionals.size());
48+
49+
List<String> filteredList = listOfOptionals.stream()
50+
.flatMap(Optional::stream)
51+
.collect(Collectors.toList());
52+
53+
assertEquals(2, filteredList.size());
54+
assertEquals("foo", filteredList.get(0));
55+
assertEquals("bar", filteredList.get(1));
56+
}
57+
58+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.java9;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertSame;
6+
7+
import java.awt.Image;
8+
import java.awt.image.BaseMultiResolutionImage;
9+
import java.awt.image.BufferedImage;
10+
import java.awt.image.MultiResolutionImage;
11+
import java.util.List;
12+
13+
import org.junit.Test;
14+
15+
public class MultiResultionImageTest {
16+
17+
18+
@Test
19+
public void baseMultiResImageTest() {
20+
int baseIndex = 1;
21+
int length = 4;
22+
BufferedImage[] resolutionVariants = new BufferedImage[length];
23+
for (int i = 0; i < length; i++) {
24+
resolutionVariants[i] = createImage(i);
25+
}
26+
MultiResolutionImage bmrImage = new BaseMultiResolutionImage(baseIndex, resolutionVariants);
27+
List<Image> rvImageList = bmrImage.getResolutionVariants();
28+
assertEquals("MultiResoltion Image shoudl contain the same number of resolution variants!", rvImageList.size(), length);
29+
30+
for (int i = 0; i < length; i++) {
31+
int imageSize = getSize(i);
32+
Image testRVImage = bmrImage.getResolutionVariant(imageSize, imageSize);
33+
assertSame("Images should be the same", testRVImage, resolutionVariants[i]);
34+
}
35+
36+
}
37+
38+
private static int getSize(int i) {
39+
return 8 * (i + 1);
40+
}
41+
42+
43+
private static BufferedImage createImage(int i) {
44+
return new BufferedImage(getSize(i), getSize(i),
45+
BufferedImage.TYPE_INT_RGB);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)