diff --git a/.gitignore b/.gitignore index 375be92..b0c8a56 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ # Compiled class file *.class +**/target/ +**/tmp/ # Log file *.log @@ -23,4 +25,6 @@ hs_err_pid* .idea/ out/ -*.iml \ No newline at end of file +*.iml +*.png +*.dot \ No newline at end of file diff --git a/README.md b/README.md index 63bd836..4af4635 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,20 @@ # Java-9-Discovery Java 9 Meetup -For more details see https://github.com/gurukulkarni/Java-9-Discovery/wiki \ No newline at end of file +To run this program, you need to (of course install Java 9 and setup you PATH properly) +1. Change to the directory where this README.md is. +2. cd jpms +3. javac simple/module-info.java simple/com/linux/simple/SimpleUiApp.java simple/com/linux/simple/generators/ContentsGenerator.java simple/com/linux/simple/generators/internal/DefaultContentsGenerator.java +4. java --module-path simple --module simple/com.linux.simple.SimpleUiApp + +More commands to run : can be found : (better syntax highlighting) + +https://github.com/gurukulkarni/Java-9-Discovery/blob/05-packages/jpms/simple/com/linux/simple/SimpleUiApp.java + +https://github.com/gurukulkarni/Java-9-Discovery/blob/05-packages/jpms/complex/com/linux/complex/ComplexApp.java + +For more details see https://github.com/gurukulkarni/Java-9-Discovery/wiki + +find . -type f -name "*.class" -exec rm -v {} + + +Linux (actually GNU) command to remove all .class files from the project. This is needed so that when you switch with class files remaining and the directory structure changes, git cannot remove the directories as it is not empty. And no too mention I am too lazy to figure out how to configure VS Code to sent proper command to force this. \ No newline at end of file diff --git a/com/linux/java/se/nine/collection/addition/CollectionOfMethods.java b/com/linux/java/se/nine/collection/addition/CollectionOfMethods.java deleted file mode 100644 index 204d606..0000000 --- a/com/linux/java/se/nine/collection/addition/CollectionOfMethods.java +++ /dev/null @@ -1,99 +0,0 @@ -package com.linux.java.se.nine.collection.addition; - -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * CollectionOfMethods - */ -public class CollectionOfMethods { - public static void main(String[] args) { - System.out.printf("%n%nPre Java 9 Vs Java 9 %s%n%n", "Lists"); - - // Old - List oldStyleList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); - print(oldStyleList, "oldStyleList"); - List oldStyleSingleElementList = Collections.singletonList(100); - print(oldStyleSingleElementList, "oldStyleSingleElementList"); - List oldStyleEmptyList = Collections.emptyList(); - print(oldStyleEmptyList, "oldStyleEmptyList"); - - System.out.printf("%n%n--%n%n"); - - // Java 9 - List newStyleList = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); - print(newStyleList, "newStyleList"); - List newStyleSingleElementList = List.of(100); - print(newStyleSingleElementList, "newStyleSingleElementList"); - List newStyleEmptyList = List.of(); - print(newStyleEmptyList, "newStyleEmptyList"); - - System.out.printf("%n%nPre Java 9 Vs Java 9 %s%n%n", "Sets"); - // ---------- Sets ---------- - // Old - Set oldStyleSet = new HashSet<>(); - oldStyleSet.add("One"); - oldStyleSet.add("Two"); - oldStyleSet.add("Three"); - oldStyleSet.add("Four"); - oldStyleSet.add("Five"); - print(oldStyleSet, "oldStyleSet"); - - Set oldStyleSingleElementSet = Collections.singleton("Hundred!"); - print(oldStyleSingleElementSet, "oldStyleSingleElementSet"); - - Set oldStyleEmptySet = Collections.emptySet(); - print(oldStyleEmptySet, "oldStyleEmptySet"); - - System.out.printf("%n%n--%n%n"); - // Java 9 - Set newStyleSet = Set.of("One", "Two", "Three", "Four", "Five"); - print(newStyleSet, "newStyleSet"); - - Set newStyleSingleElementSet = Set.of("Hundred!"); - print(newStyleSingleElementSet, "newStyleSingleElementSet"); - - Set newStyleEmptySet = Set.of(); - print(newStyleEmptySet, "newStyleEmptySet"); - - System.out.printf("%n%nPre Java 9 Vs Java 9 %s%n%n", "Maps"); - Map oldStyleMap = new HashMap<>(); - oldStyleMap.put(1, "One"); - oldStyleMap.put(2, "Two"); - oldStyleMap.put(3, "Three"); - oldStyleMap.put(4, "Four"); - oldStyleMap.put(5, "Five"); - print(oldStyleMap, "oldStyleMap"); - - Map oldStyleBadMap = new HashMap<>(){{put(1, "One");put(2, "Two");put(3, "Three");put(4, "Four");put(5, "Five");}}; - print(oldStyleBadMap, "oldStyleBadMap"); - - Map oldStyleSingleElementMap = Collections.singletonMap(100, "Hundred!"); - print(oldStyleSingleElementMap, "oldStyleSingleElementMap"); - - Map oldStyleEmptyMap = Collections.emptyMap(); - print(oldStyleEmptyMap, "oldStyleEmptyMap"); - - System.out.printf("%n%n--%n%n"); - - Map newStyleMap = Map.of(1, "One", 2, "Two", 3, "Three", 4, "Four", 5, "Five"); - print(newStyleMap, "newStyleMap"); - - Map newStyleSingleElementMap = Map.of(1, "Hundred!"); - print(newStyleSingleElementMap, "newStyleSingleElementMap"); - - Map newStyleEmptyMap = Map.of(); - print(newStyleEmptyMap, "newStyleEmptyMap"); - } - - private static void print(Object c, String identifier) { - System.out.println(String.join("", Collections.nCopies(150, "-"))); - System.out.printf("%-30s :: Type : %-50s | Elements : %s%n",identifier, c.getClass(), c); - } - -} \ No newline at end of file diff --git a/com/linux/java/se/nine/completable/future/improvment/CompletableFutureAdditions.java b/com/linux/java/se/nine/completable/future/improvment/CompletableFutureAdditions.java deleted file mode 100644 index 459f601..0000000 --- a/com/linux/java/se/nine/completable/future/improvment/CompletableFutureAdditions.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.linux.java.se.nine.completable.future.improvment; - -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; - -/** - * CompletableFutureAdditions - */ -public class CompletableFutureAdditions { - public static void main(String[] args) { - CompletableFutureAdditions additions = new CompletableFutureAdditions(); - CompletableFuture cf = CompletableFuture.supplyAsync(() -> additions.sleep(2)); - System.out.println(" Original Completable Future is done ?" + cf.isDone()); - - CompletableFuture newCopy = cf.copy(); - System.out.println(" Copy of Completable Future is done ?" + cf.isDone()); - newCopy.completeExceptionally(new InterruptedException("Some message")); - - CompletableFuture completeOnTimeOut = cf.copy().completeOnTimeout("Failed to complete", 1, TimeUnit.SECONDS); - System.out.println("Completable Future Complete on Timeout is done ?" + cf.isDone()); - - cf.thenAccept(System.out::println); - newCopy.thenAccept(System.out::println); - completeOnTimeOut.thenAccept(System.out::println); - - - additions.sleep(5); - - } - - - private String sleep(int sleepTimeSeconds) { - try { - Thread.sleep(TimeUnit.SECONDS.toMillis(sleepTimeSeconds)); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - return "Done : " + System.currentTimeMillis() + " By " + Thread.currentThread().getName(); - } - -} \ No newline at end of file diff --git a/com/linux/java/se/nine/privateinterfacemethods/SimpleUserService.java b/com/linux/java/se/nine/privateinterfacemethods/SimpleUserService.java deleted file mode 100644 index a782fab..0000000 --- a/com/linux/java/se/nine/privateinterfacemethods/SimpleUserService.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.linux.java.se.nine.privateinterfacemethods; - -interface UserService{ - - default String getUser() { - return getUserFromEnv(); - } - - static String getSystemUser(){ - return getUserFromEnv(); - } - - private static String getUserFromEnv() { - return System.getenv("USER"); - } - -} - -public class SimpleUserService implements UserService{ - - @Override - public String getUser() { - return "john"; - } - - public static void main(String[] args) { - UserService s = new SimpleUserService(); - System.out.println("Current User :: " + s.getUser()); - System.out.println("Current System User :: " + UserService.getSystemUser()); - } - -} \ No newline at end of file diff --git a/com/linux/java/se/nine/streams/improvment/StreamsImprovements.java b/com/linux/java/se/nine/streams/improvment/StreamsImprovements.java deleted file mode 100644 index d3cbf0f..0000000 --- a/com/linux/java/se/nine/streams/improvment/StreamsImprovements.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.linux.java.se.nine.streams.improvment; - -import java.time.LocalDate; -import java.time.Month; -import java.time.Period; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.function.Predicate; -import java.util.stream.Stream; - -import static java.util.Comparator.comparing; -import static java.util.stream.Collectors.toList; - - -/** - * StreamsImprovements - */ - -public class StreamsImprovements { - private static final Book ej3e = new Book("Effective Java 3rd Edition", "Joshua Bloch", "978-0-13-468599-1", LocalDate.of(2017, Month.DECEMBER, 20)); - private static final Book cjs9fi = new Book("Core Java SE 9 for the Impatient, 2nd Edition", "Cay S. Horstmann", "978-0-13-469472-6", LocalDate.of(2017, Month.SEPTEMBER, 15)); - private static final Book j9fp = new Book("Java 9 for Programmers, 4th Edition", "Paul J. Deitel, Harvey Deitel", "978-0-13-477756-6", LocalDate.of(2017, Month.MAY, 11)); - private static final Book ej2e = new Book("Effective Java 2nd Edition", "Joshua Bloch", "978-0-321-35668-0", LocalDate.of(2008, Month.MAY, 8)); - private static final Book rwjeep = new Book("Real World Java EE Patterns", "Adam Bien", "978-1-300-14931-6", LocalDate.of(2012, Month.SEPTEMBER, 5)); - private static final Book rwjeenh = new Book("Real World Java EE Night Hacks", "Adam Bien", "978-0-557-07832-5", LocalDate.of(2008, Month.MAY, 8)); - - - public static void main(String[] args) { - Predicate publishDateGreaterThan2012 = b -> b.getPublishDate().getYear() > 2012; - - print(getBooks().collect(toList()), "***** Original List *****"); - - List booksWithSkip = getBooks() - .skip(3) - .collect(toList()); - print(booksWithSkip, "Java 8 ... skip 3"); - - List booksWithDropWhile = getBooks() - .dropWhile(publishDateGreaterThan2012) - .collect(toList()); - print(booksWithDropWhile, "Java 9 ... dropWhile publishDateGreaterThan2012"); - - List booksWithLimit = getBooks() - .limit(3) - .collect(toList()); - print(booksWithLimit, "Java 8 ... limit"); - - List booksWithTakeWhile = getBooks() - .takeWhile(publishDateGreaterThan2012) - .collect(toList()); - print(booksWithTakeWhile, "Java 9 ... takeWhile publishDateGreaterThan2012"); - - } - - - /** - * New recommendation from Joshua Bloch -- If you know that the callers of your method are happy using Stream as a return, use it!!!! - */ - private static Stream getBooks() { - return Stream.of(ej3e, cjs9fi, j9fp, ej2e, rwjeep, rwjeenh).sorted(comparing(Book::getPublishDate).reversed()); - } - - private static void print(List list, String identifier) { - System.out.println(identifier); - list.forEach(System.out::println); - System.out.println(String.join("", Collections.nCopies(150, "-"))); - } - - - static class Book { - private final String name; - private final String author; - private final String isbn; - private final LocalDate publishDate; - - Book(String name, String author, String isbn, LocalDate publishDate) { - this.name = name; - this.author = author; - this.isbn = isbn; - this.publishDate = publishDate; - } - - public Period getBookAgeinMonths() { - return Period.between(publishDate, LocalDate.now()); - } - - /** - * @return the name - */ - public String getName() { - return name; - } - - /** - * @return the isbn - */ - public String getIsbn() { - return isbn; - } - - /** - * @return the publishDate - */ - public LocalDate getPublishDate() { - return publishDate; - } - - /** - * @return the author - */ - public String getAuthor() { - return author; - } - - @Override - public String toString() { - return String.format("Book{name=%-50s\tauthor=%-30s\tisbn=%-20s\tpublishDate=%10s}", name, author, isbn, publishDate); - } - - } -} diff --git a/com/linux/java/se/nine/trywithresources/TryWithResources.java b/com/linux/java/se/nine/trywithresources/TryWithResources.java deleted file mode 100644 index 9edaefb..0000000 --- a/com/linux/java/se/nine/trywithresources/TryWithResources.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.linux.java.se.nine.trywithresources; - -import java.util.concurrent.TimeUnit; - -class SimpleResource implements AutoCloseable{ - private final String name; - - SimpleResource(String name) { - this.name = name; - } - - public String doWork(String context) { - try{ - TimeUnit.SECONDS.sleep(2); - } catch(InterruptedException ie) { - throw new RuntimeException(ie); - } - return "Work Done for " + context; - } - - @Override - public void close() { - System.out.println("Resource " + name + " closed"); - } -} - -/** - * TryWithResources - */ -public class TryWithResources { - public static void main(String[] args) { - System.out.println("-- Before :: "); - try(SimpleResource res = new SimpleResource("insideTryWithResource")){ - String workResponse = res.doWork("old style try with resources"); - System.out.println(workResponse); - } - - - System.out.println("-- After :: "); - SimpleResource resource = new SimpleResource("OutsideTryWithResource"); - try(resource) { - String workResponse = resource.doWork("yay! java 9"); - System.out.println(workResponse); - } - - } -} \ No newline at end of file diff --git a/jpms/complex/com/linux/complex/ComplexApp.java b/jpms/complex/com/linux/complex/ComplexApp.java new file mode 100644 index 0000000..57cea10 --- /dev/null +++ b/jpms/complex/com/linux/complex/ComplexApp.java @@ -0,0 +1,34 @@ +package com.linux.complex; + +import javafx.application.Application; +import javafx.stage.Stage; + +import com.linux.simple.generators.ContentsGenerator; + +/** + * ComplexApp + */ +public class ComplexApp extends Application{ + public static void main(String[] args) { + launch(args); + } + + public void start(Stage primaryStage) { + ContentsGenerator contentsGenerator = ContentsGenerator.getInstance(); + contentsGenerator.generateContent(primaryStage, "Complex JavaFX Application", ComplexApp.class.getSimpleName()); + primaryStage.show(); + } + +} + +// javac --module-path simple complex/module-info.java complex/com/linux/complex/ComplexApp.java +// java --module-path simple:complex --module complex/com.linux.complex.ComplexApp + +// using jars +// javac -d target --module-path simple.jar complex/module-info.java complex/com/linux/complex/ComplexApp.java +// jar -cvf complex.jar -C target/ . +// java --module-path simple.jar:complex.jar --module complex/com.linux.complex.ComplexApp + +// using executable jars :: seperating the options is important!! +// jar --create --verbose --file complex.jar --main-class com.linux.complex.ComplexApp -C target/ . +// java --module-path simple.jar:complex.jar -m complex diff --git a/jpms/complex/module-info.java b/jpms/complex/module-info.java new file mode 100644 index 0000000..cac0122 --- /dev/null +++ b/jpms/complex/module-info.java @@ -0,0 +1,6 @@ +module complex { + requires javafx.graphics; + requires javafx.controls; + requires simple; + exports com.linux.complex; +} \ No newline at end of file diff --git a/jpms/simple/com/linux/simple/SimpleUiApp.java b/jpms/simple/com/linux/simple/SimpleUiApp.java new file mode 100644 index 0000000..a42e23a --- /dev/null +++ b/jpms/simple/com/linux/simple/SimpleUiApp.java @@ -0,0 +1,41 @@ +package com.linux.simple; + +import javafx.application.Application; +import javafx.stage.Stage; + +import com.linux.simple.generators.ContentsGenerator; + +public class SimpleUiApp extends Application{ + + public static void main(String[] args) { + launch(args); + } + + public void start(Stage primaryStage) { + ContentsGenerator contentGenerator = ContentsGenerator.getInstance(); + contentGenerator.generateContent(primaryStage, "Hello Java9", SimpleUiApp.class.getSimpleName()); + primaryStage.show(); + } +} + + +// cd jpms +// javac simple/module-info.java simple/com/linux/simple/SimpleUiApp.java simple/com/linux/simple/generators/ContentsGenerator.java simple/com/linux/simple/generators/internal/DefaultContentsGenerator.java +// java --module-path simple --module simple/com.linux.simple.SimpleUiApp + +// jdeps --module-path simple --dot-output . --module simple +// jdeps --module-path simple --dot-output . --module simple -s +// dot -Tpng simple.dot > simple.png +// http://www.webgraphviz.com/ + +// javac -d target simple/module-info.java simple/com/linux/simple/SimpleUiApp.java simple/com/linux/simple/generators/ContentsGenerator.java simple/com/linux/simple/generators/internal/DefaultContentsGenerator.java +// simple jar (library) +// jar -cvf simple.jar -C target/ . +// java --module-path simple.jar --module simple/com.linux.simple.SimpleUiApp + +// jdeps --dot-output . -s simple.jar +// dot -Tpng summary.dot > summary.png + +// Executable jar +// jar -cvfe simple.jar com.linux.simple.SimpleUiApp -C target/ . +// java -jar simple.jar \ No newline at end of file diff --git a/jpms/simple/com/linux/simple/generators/ContentsGenerator.java b/jpms/simple/com/linux/simple/generators/ContentsGenerator.java new file mode 100644 index 0000000..42b337a --- /dev/null +++ b/jpms/simple/com/linux/simple/generators/ContentsGenerator.java @@ -0,0 +1,13 @@ +package com.linux.simple.generators; + +import javafx.stage.Stage; + +import com.linux.simple.generators.internal.DefaultContentsGenerator; + +public interface ContentsGenerator { + void generateContent(Stage stage, String title, String messagePrefix); + + static ContentsGenerator getInstance() { + return new DefaultContentsGenerator(); + } +} \ No newline at end of file diff --git a/jpms/simple/com/linux/simple/generators/internal/DefaultContentsGenerator.java b/jpms/simple/com/linux/simple/generators/internal/DefaultContentsGenerator.java new file mode 100644 index 0000000..586afe4 --- /dev/null +++ b/jpms/simple/com/linux/simple/generators/internal/DefaultContentsGenerator.java @@ -0,0 +1,27 @@ +package com.linux.simple.generators.internal; + +import javafx.scene.Scene; +import javafx.scene.layout.StackPane; +import javafx.stage.Stage; +import javafx.scene.control.Button; + +import com.linux.simple.generators.ContentsGenerator; + +/** + * DefaultContentsGenerator + */ +public class DefaultContentsGenerator implements ContentsGenerator{ + + @Override + public void generateContent(Stage stage, String title, String messagePrefix) { + System.out.println("Default content generated."); + stage.setTitle(title); + Button button = new Button(); + button.setText(title); + button.setOnAction(e -> System.out.println(messagePrefix + " -- " + e.getEventType().toString())); + + StackPane root = new StackPane(); + root.getChildren().add(button); + stage.setScene(new Scene(root, 400, 300)); + } +} \ No newline at end of file diff --git a/jpms/simple/module-info.java b/jpms/simple/module-info.java new file mode 100644 index 0000000..8927e9d --- /dev/null +++ b/jpms/simple/module-info.java @@ -0,0 +1,6 @@ +module simple { + requires javafx.graphics; + requires javafx.controls; + exports com.linux.simple.generators; + exports com.linux.simple; +} \ No newline at end of file diff --git a/jpms/src/com/linux/java/se/nine/jpms/SimpleUiApp.java b/jpms/src/com/linux/java/se/nine/jpms/SimpleUiApp.java deleted file mode 100644 index 6f1ba0a..0000000 --- a/jpms/src/com/linux/java/se/nine/jpms/SimpleUiApp.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.linux.java.se.nine.jpms; - -import javafx.application.Application; -import javafx.scene.Scene; -import javafx.scene.control.Button; -import javafx.scene.layout.StackPane; -import javafx.stage.Stage; - -public class SimpleUiApp extends Application{ - - public static void main(String[] args) { - launch(args); - } - - public void start(Stage primaryStage) { - primaryStage.setTitle("Hello Java9 World"); - Button button = new Button(); - button.setText("Hello Java9 World"); - button.setOnAction(e -> System.out.println("Performed " + e.getEventType().toString())); - - StackPane root = new StackPane(); - root.getChildren().add(button); - primaryStage.setScene(new Scene(root, 400, 300)); - primaryStage.show(); - } -} diff --git a/jpms/src/com/module-info.java b/jpms/src/com/module-info.java deleted file mode 100644 index f3e3700..0000000 --- a/jpms/src/com/module-info.java +++ /dev/null @@ -1,3 +0,0 @@ -module com.linux.java.se.nine.jpms { - -} \ No newline at end of file