From 0769f11c5bbc02aac225c50e976b9b5d6bff8b8a Mon Sep 17 00:00:00 2001 From: Thang Toan Pham Date: Sun, 31 Dec 2017 23:15:03 +0900 Subject: [PATCH 1/5] immutable Set static factory methods in Java 9 --- .gitignore | 26 +++++ .../java9ex/Java9SetFactoryMethodTest.java | 101 ++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 .gitignore create mode 100644 java9-language-features-examples/src/test/java/com/howtoprogram/java9ex/Java9SetFactoryMethodTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6f5f31d --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +# Eclipse +.project +.classpath +.settings/ + +# Idea +.idea/ +*.iml +*.iws +*.ipr + +# OS +Thumbs.db +.DS_Store + +# Maven +target/ + +# Build +out/ +build/ +bin/ + + +# Others +*.log diff --git a/java9-language-features-examples/src/test/java/com/howtoprogram/java9ex/Java9SetFactoryMethodTest.java b/java9-language-features-examples/src/test/java/com/howtoprogram/java9ex/Java9SetFactoryMethodTest.java new file mode 100644 index 0000000..85a4ad1 --- /dev/null +++ b/java9-language-features-examples/src/test/java/com/howtoprogram/java9ex/Java9SetFactoryMethodTest.java @@ -0,0 +1,101 @@ +package com.howtoprogram.java9ex; + +import org.junit.jupiter.api.Test; + +import java.io.*; +import java.util.*; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + + +public class Java9SetFactoryMethodTest { + + + @Test + public void testImmutableSetJava9() { + + Set japanNoodles = Set.of("Ramen", "Yakisoba", "Udon", "Soba", "Somen"); + assertEquals(5, japanNoodles.size()); + } + + @Test + public void testImmutableSetNotModified() { + + + Set japanNoodles = Set.of("Ramen", "Yakisoba", "Udon", "Soba", "Somen"); + + assertEquals(5, japanNoodles.size()); + + assertThrows(UnsupportedOperationException.class, () -> { + // Add more element + japanNoodles.add("Negi"); + // Remove element + japanNoodles.remove(1); + }); + + } + + @Test + public void testImmutableSetNullAttempts() { + + assertThrows(NullPointerException.class, () -> { + Set stringList = Set.of(null); + }); + + } + + @Test + public void testImmutableSetRejectDupplicates() { + + assertThrows(IllegalArgumentException.class, () -> { + Set teaSet = Set.of("Shincha", "Aki Bancha", "Hojicha", "Hojicha"); + }); + + } + + @Test + public void testImmutableSetMutableElements() { + + Map noodleToppingsMap = new HashMap<>(); + noodleToppingsMap.put(1, "Negi"); + noodleToppingsMap.put(2, "Tamago"); + noodleToppingsMap.put(3, "Nori"); + + Map japanTeaMap = new HashMap<>(); + japanTeaMap.put(1, "Ryokucha"); + japanTeaMap.put(2, "Yamecha"); + japanTeaMap.put(3, "Ujicha"); + + List> choices = List.of(noodleToppingsMap, japanTeaMap); + + assertEquals(3, choices.get(0).size()); + + //Add more elements to the mutable element of the List + noodleToppingsMap.put(3, "Tsuyu"); + choices.get(0).put(4, "Aracha"); + + assertEquals(4, choices.get(0).size()); + + } + + @Test + public void testImmutableSetSerializable() throws IOException { + + Set drinks = Set.of("Gyokuro", "Tamaryokucha", "Kamairicha"); + //serialize the list + ObjectOutputStream oos = new ObjectOutputStream(System.out); + oos.writeObject(drinks); + + //create a list of non-serializable objects + List outputStreamList = List.of(new ByteArrayOutputStream(), + new PrintStream(System.out)); + + assertThrows(NotSerializableException.class, () -> { + + oos.writeObject(outputStreamList); + } + ); + } + +} From ed81a78fae75a4e4040a153c259e090799905cac Mon Sep 17 00:00:00 2001 From: howtoprogram Date: Sun, 31 Dec 2017 23:17:26 +0900 Subject: [PATCH 2/5] space --- .../howtoprogram/java9ex/Java9SetFactoryMethodTest.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/java9-language-features-examples/src/test/java/com/howtoprogram/java9ex/Java9SetFactoryMethodTest.java b/java9-language-features-examples/src/test/java/com/howtoprogram/java9ex/Java9SetFactoryMethodTest.java index 85a4ad1..d1df2ba 100644 --- a/java9-language-features-examples/src/test/java/com/howtoprogram/java9ex/Java9SetFactoryMethodTest.java +++ b/java9-language-features-examples/src/test/java/com/howtoprogram/java9ex/Java9SetFactoryMethodTest.java @@ -14,19 +14,14 @@ public class Java9SetFactoryMethodTest { @Test public void testImmutableSetJava9() { - Set japanNoodles = Set.of("Ramen", "Yakisoba", "Udon", "Soba", "Somen"); assertEquals(5, japanNoodles.size()); } @Test public void testImmutableSetNotModified() { - - Set japanNoodles = Set.of("Ramen", "Yakisoba", "Udon", "Soba", "Somen"); - assertEquals(5, japanNoodles.size()); - assertThrows(UnsupportedOperationException.class, () -> { // Add more element japanNoodles.add("Negi"); @@ -38,7 +33,6 @@ public void testImmutableSetNotModified() { @Test public void testImmutableSetNullAttempts() { - assertThrows(NullPointerException.class, () -> { Set stringList = Set.of(null); }); @@ -47,7 +41,6 @@ public void testImmutableSetNullAttempts() { @Test public void testImmutableSetRejectDupplicates() { - assertThrows(IllegalArgumentException.class, () -> { Set teaSet = Set.of("Shincha", "Aki Bancha", "Hojicha", "Hojicha"); }); @@ -81,7 +74,6 @@ public void testImmutableSetMutableElements() { @Test public void testImmutableSetSerializable() throws IOException { - Set drinks = Set.of("Gyokuro", "Tamaryokucha", "Kamairicha"); //serialize the list ObjectOutputStream oos = new ObjectOutputStream(System.out); From 530cd2fc4655c353e7fa508b5921e33c546def79 Mon Sep 17 00:00:00 2001 From: howtoprogram Date: Sun, 31 Dec 2017 23:18:09 +0900 Subject: [PATCH 3/5] updated --- .gitignore | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.gitignore b/.gitignore index 6f5f31d..c2629dc 100644 --- a/.gitignore +++ b/.gitignore @@ -9,10 +9,6 @@ *.iws *.ipr -# OS -Thumbs.db -.DS_Store - # Maven target/ From 64fa7180ec798c227eb15a0b62368c6a5b920dc2 Mon Sep 17 00:00:00 2001 From: howtoprogram Date: Sun, 31 Dec 2017 23:27:47 +0900 Subject: [PATCH 4/5] update ref --- java9-language-features-examples/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/java9-language-features-examples/README.md b/java9-language-features-examples/README.md index 1d04fce..fb3d14e 100644 --- a/java9-language-features-examples/README.md +++ b/java9-language-features-examples/README.md @@ -12,6 +12,7 @@ Demonstration for: [Java 9 HTTP 2 Client API Example](https://howtoprogram.xyz/2017/10/19/java-9-http-2-client-api-example/) +[Create An Immutable Set In Java 9 By Static Factory Methods](https://howtoprogram.xyz/2017/12/31/create-immutable-set-java-9-static-factory-methods/) ## Prerequisites: From a81c6f71fabd7decc5e83f9ac9891f8a1760bb49 Mon Sep 17 00:00:00 2001 From: howtoprogram Date: Sun, 31 Dec 2017 23:29:56 +0900 Subject: [PATCH 5/5] update ref --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 34829ed..a3aa7a2 100644 --- a/README.md +++ b/README.md @@ -15,4 +15,5 @@ Find more detail at: * [Create Immutable Lists In Java 9 By Static Factory Methods](https://howtoprogram.xyz/2017/09/24/java-9-create-immutable-lists-static-factory-method/) * [Java 9 Example With Maven And JUnit 5](https://howtoprogram.xyz/2017/09/23/java-9-maven-junit-5-example/) * [Copy Streams In Java 9 With The InputStream.transferTo() Method](https://howtoprogram.xyz/2017/10/01/java-9-inputstream-transferto-copy-streams/) -* [Java 9 HTTP 2 Client API Example](https://howtoprogram.xyz/2017/10/19/java-9-http-2-client-api-example/) \ No newline at end of file +* [Java 9 HTTP 2 Client API Example](https://howtoprogram.xyz/2017/10/19/java-9-http-2-client-api-example/) +* [Create An Immutable Set In Java 9 By Static Factory Methods](https://howtoprogram.xyz/2017/12/31/create-immutable-set-java-9-static-factory-methods/) \ No newline at end of file