diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2629dc --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +# Eclipse +.project +.classpath +.settings/ + +# Idea +.idea/ +*.iml +*.iws +*.ipr + +# Maven +target/ + +# Build +out/ +build/ +bin/ + + +# Others +*.log 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 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: 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..d1df2ba --- /dev/null +++ b/java9-language-features-examples/src/test/java/com/howtoprogram/java9ex/Java9SetFactoryMethodTest.java @@ -0,0 +1,93 @@ +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); + } + ); + } + +}