From ff84127f479517b6b15e6f224d91c22f42a06c3e Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Fri, 29 Jul 2016 11:55:35 +0200 Subject: [PATCH 1/7] Add immutables module --- immutables/pom.xml | 44 +++++++++++++++++++ .../java/com/baeldung/immutable/Address.java | 5 +++ .../java/com/baeldung/immutable/Person.java | 8 ++++ .../com/baeldung/immutable/ValueObject.java | 6 +++ pom.xml | 1 + 5 files changed, 64 insertions(+) create mode 100644 immutables/pom.xml create mode 100644 immutables/src/main/java/com/baeldung/immutable/Address.java create mode 100644 immutables/src/main/java/com/baeldung/immutable/Person.java create mode 100644 immutables/src/main/java/com/baeldung/immutable/ValueObject.java diff --git a/immutables/pom.xml b/immutables/pom.xml new file mode 100644 index 0000000000..a0c7717139 --- /dev/null +++ b/immutables/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + com.baeldung + immutables + 1.0.0-SNAPSHOT + + + + org.immutables + value + 2.2.10 + + + junit + junit + 4.12 + test + + + org.assertj + assertj-core + 3.5.2 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + + + + + \ No newline at end of file diff --git a/immutables/src/main/java/com/baeldung/immutable/Address.java b/immutables/src/main/java/com/baeldung/immutable/Address.java new file mode 100644 index 0000000000..5e7cd4f3f9 --- /dev/null +++ b/immutables/src/main/java/com/baeldung/immutable/Address.java @@ -0,0 +1,5 @@ +package com.baeldung.immutable; + +public class Address { + +} diff --git a/immutables/src/main/java/com/baeldung/immutable/Person.java b/immutables/src/main/java/com/baeldung/immutable/Person.java new file mode 100644 index 0000000000..9ffee3059e --- /dev/null +++ b/immutables/src/main/java/com/baeldung/immutable/Person.java @@ -0,0 +1,8 @@ +package com.baeldung.immutable; + +import org.immutables.value.Value; + +@Value.Immutable +public class Person { + private String name; +} diff --git a/immutables/src/main/java/com/baeldung/immutable/ValueObject.java b/immutables/src/main/java/com/baeldung/immutable/ValueObject.java new file mode 100644 index 0000000000..b598a9d12d --- /dev/null +++ b/immutables/src/main/java/com/baeldung/immutable/ValueObject.java @@ -0,0 +1,6 @@ +package com.baeldung.immutable; + +public class ValueObject { + public ValueObject() { + } +} diff --git a/pom.xml b/pom.xml index d2f5d83b46..419916de86 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,7 @@ guava19 handling-spring-static-resources httpclient + immutables jackson javaxval jjwt From eff343b59679b3525235b99ce6c7fefb6e354bc6 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 30 Jul 2016 10:50:47 +0200 Subject: [PATCH 2/7] Add first test --- .../java/com/baeldung/immutable/Address.java | 6 ++++- .../java/com/baeldung/immutable/Person.java | 5 ++-- .../immutable/ImmutablePersonTest.java | 23 +++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java diff --git a/immutables/src/main/java/com/baeldung/immutable/Address.java b/immutables/src/main/java/com/baeldung/immutable/Address.java index 5e7cd4f3f9..93474dc043 100644 --- a/immutables/src/main/java/com/baeldung/immutable/Address.java +++ b/immutables/src/main/java/com/baeldung/immutable/Address.java @@ -1,5 +1,9 @@ package com.baeldung.immutable; -public class Address { +import org.immutables.value.Value; +@Value.Immutable +public interface Address { + String getStreetName(); + Integer getNumber(); } diff --git a/immutables/src/main/java/com/baeldung/immutable/Person.java b/immutables/src/main/java/com/baeldung/immutable/Person.java index 9ffee3059e..466daf42c2 100644 --- a/immutables/src/main/java/com/baeldung/immutable/Person.java +++ b/immutables/src/main/java/com/baeldung/immutable/Person.java @@ -3,6 +3,7 @@ package com.baeldung.immutable; import org.immutables.value.Value; @Value.Immutable -public class Person { - private String name; +public abstract class Person { + abstract String getName(); + abstract Integer getAge(); } diff --git a/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java b/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java new file mode 100644 index 0000000000..8c53569836 --- /dev/null +++ b/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java @@ -0,0 +1,23 @@ +package com.baeldung.immutable; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ImmutablePersonTest { + + @Test + public void whenModifying_shouldCreateNewInstance() throws Exception { + final com.baeldung.immutable.ImmutablePerson john = com.baeldung.immutable.ImmutablePerson.builder() + .age(42) + .name("John") + .build(); + + final com.baeldung.immutable.ImmutablePerson john43 = john.withAge(43); + + assertThat(john) + .isNotSameAs(john43); + assertThat(john.getAge()) + .isEqualTo(42); + } +} \ No newline at end of file From 591e3507fb2f5ffa2019a0e805729599696d6911 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 30 Jul 2016 12:08:27 +0200 Subject: [PATCH 3/7] Add additional tests --- .../com/baeldung/immutable/ValueObject.java | 2 ++ .../baeldung/immutable/auxiliary/Person.java | 13 ++++++++ .../immutable/ImmutablePersonTest.java | 1 + .../ImmutablePersonAuxiliaryTest.java | 33 +++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 immutables/src/main/java/com/baeldung/immutable/auxiliary/Person.java create mode 100644 immutables/src/test/java/com/baeldung/immutable/auxiliary/ImmutablePersonAuxiliaryTest.java diff --git a/immutables/src/main/java/com/baeldung/immutable/ValueObject.java b/immutables/src/main/java/com/baeldung/immutable/ValueObject.java index b598a9d12d..475c6bf7b1 100644 --- a/immutables/src/main/java/com/baeldung/immutable/ValueObject.java +++ b/immutables/src/main/java/com/baeldung/immutable/ValueObject.java @@ -2,5 +2,7 @@ package com.baeldung.immutable; public class ValueObject { public ValueObject() { + + } } diff --git a/immutables/src/main/java/com/baeldung/immutable/auxiliary/Person.java b/immutables/src/main/java/com/baeldung/immutable/auxiliary/Person.java new file mode 100644 index 0000000000..78fe28c50c --- /dev/null +++ b/immutables/src/main/java/com/baeldung/immutable/auxiliary/Person.java @@ -0,0 +1,13 @@ +package com.baeldung.immutable.auxiliary; + + +import org.immutables.value.Value; + +@Value.Immutable +public abstract class Person { + abstract String getName(); + abstract Integer getAge(); + + @Value.Auxiliary + abstract String getAuxiliaryField(); +} \ No newline at end of file diff --git a/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java b/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java index 8c53569836..aabdb4300a 100644 --- a/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java +++ b/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java @@ -17,6 +17,7 @@ public class ImmutablePersonTest { assertThat(john) .isNotSameAs(john43); + assertThat(john.getAge()) .isEqualTo(42); } diff --git a/immutables/src/test/java/com/baeldung/immutable/auxiliary/ImmutablePersonAuxiliaryTest.java b/immutables/src/test/java/com/baeldung/immutable/auxiliary/ImmutablePersonAuxiliaryTest.java new file mode 100644 index 0000000000..a81eeb3a3a --- /dev/null +++ b/immutables/src/test/java/com/baeldung/immutable/auxiliary/ImmutablePersonAuxiliaryTest.java @@ -0,0 +1,33 @@ +package com.baeldung.immutable.auxiliary; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ImmutablePersonAuxiliaryTest { + + @Test + public void whenComparing_shouldIgnore() throws Exception { + final com.baeldung.immutable.auxiliary.ImmutablePerson john1 = com.baeldung.immutable.auxiliary.ImmutablePerson.builder() + .name("John") + .age(42) + .auxiliaryField("Value1") + .build(); + + final com.baeldung.immutable.auxiliary.ImmutablePerson john2 = com.baeldung.immutable.auxiliary.ImmutablePerson.builder() + .name("John") + .age(42) + .auxiliaryField("Value2") + .build(); + + + assertThat(john1.equals(john2)) + .isTrue(); + + assertThat(john1.toString()) + .isEqualTo(john2.toString()); + + assertThat(john1.hashCode()) + .isEqualTo(john2.hashCode()); + } +} \ No newline at end of file From 7b1c77c6bc34019c9214d74b0dc4c65f3a215469 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 30 Jul 2016 12:22:43 +0200 Subject: [PATCH 4/7] Add MutabilityDetector --- immutables/pom.xml | 6 ++++++ .../java/com/baeldung/immutable/ImmutablePersonTest.java | 3 +++ 2 files changed, 9 insertions(+) diff --git a/immutables/pom.xml b/immutables/pom.xml index a0c7717139..2b4aba59b1 100644 --- a/immutables/pom.xml +++ b/immutables/pom.xml @@ -26,6 +26,12 @@ 3.5.2 test + + org.mutabilitydetector + MutabilityDetector + 0.9.5 + test + diff --git a/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java b/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java index aabdb4300a..0f3b8116af 100644 --- a/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java +++ b/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java @@ -3,6 +3,7 @@ package com.baeldung.immutable; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.mutabilitydetector.unittesting.MutabilityAssert.assertImmutable; public class ImmutablePersonTest { @@ -20,5 +21,7 @@ public class ImmutablePersonTest { assertThat(john.getAge()) .isEqualTo(42); + + assertImmutable(com.baeldung.immutable.ImmutablePerson.class); } } \ No newline at end of file From eef41ec02865d7da287f65edb6bb259903a0dbe4 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 30 Jul 2016 12:54:27 +0200 Subject: [PATCH 5/7] Add remaining examples --- .../com/baeldung/immutable/default_/Person.java | 14 ++++++++++++++ .../baeldung/immutable/parameter/Person.java | 14 ++++++++++++++ .../com/baeldung/immutable/prehash/Person.java | 9 +++++++++ .../default_/ImmutablePersonDefaultTest.java | 17 +++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 immutables/src/main/java/com/baeldung/immutable/default_/Person.java create mode 100644 immutables/src/main/java/com/baeldung/immutable/parameter/Person.java create mode 100644 immutables/src/main/java/com/baeldung/immutable/prehash/Person.java create mode 100644 immutables/src/test/java/com/baeldung/immutable/default_/ImmutablePersonDefaultTest.java diff --git a/immutables/src/main/java/com/baeldung/immutable/default_/Person.java b/immutables/src/main/java/com/baeldung/immutable/default_/Person.java new file mode 100644 index 0000000000..bc48f11a38 --- /dev/null +++ b/immutables/src/main/java/com/baeldung/immutable/default_/Person.java @@ -0,0 +1,14 @@ +package com.baeldung.immutable.default_; + +import org.immutables.value.Value; + +@Value.Immutable(prehash = true) +public abstract class Person { + + abstract String getName(); + + @Value.Default + Integer getAge() { + return 42; + } +} diff --git a/immutables/src/main/java/com/baeldung/immutable/parameter/Person.java b/immutables/src/main/java/com/baeldung/immutable/parameter/Person.java new file mode 100644 index 0000000000..4e8218f99c --- /dev/null +++ b/immutables/src/main/java/com/baeldung/immutable/parameter/Person.java @@ -0,0 +1,14 @@ +package com.baeldung.immutable.parameter; + + +import org.immutables.value.Value; + +@Value.Immutable +public abstract class Person { + + @Value.Parameter + abstract String getName(); + + @Value.Parameter + abstract Integer getAge(); +} \ No newline at end of file diff --git a/immutables/src/main/java/com/baeldung/immutable/prehash/Person.java b/immutables/src/main/java/com/baeldung/immutable/prehash/Person.java new file mode 100644 index 0000000000..5e5dd4d9e9 --- /dev/null +++ b/immutables/src/main/java/com/baeldung/immutable/prehash/Person.java @@ -0,0 +1,9 @@ +package com.baeldung.immutable.prehash; + +import org.immutables.value.Value; + +@Value.Immutable(prehash = true) +public abstract class Person { + abstract String getName(); + abstract Integer getAge(); +} diff --git a/immutables/src/test/java/com/baeldung/immutable/default_/ImmutablePersonDefaultTest.java b/immutables/src/test/java/com/baeldung/immutable/default_/ImmutablePersonDefaultTest.java new file mode 100644 index 0000000000..7236212fc4 --- /dev/null +++ b/immutables/src/test/java/com/baeldung/immutable/default_/ImmutablePersonDefaultTest.java @@ -0,0 +1,17 @@ +package com.baeldung.immutable.default_; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ImmutablePersonDefaultTest { + + @Test + public void whenInstantiating_shouldUseDefaultValue() throws Exception { + + final com.baeldung.immutable.default_.ImmutablePerson john = com.baeldung.immutable.default_.ImmutablePerson.builder().name("John").build(); + + assertThat(john.getAge()).isEqualTo(42); + + } +} \ No newline at end of file From f5dbb497dfe60101216e92d7bd989ed7e5b85070 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 30 Jul 2016 12:56:20 +0200 Subject: [PATCH 6/7] Refactor examples --- .../java/com/baeldung/immutable/ImmutablePersonTest.java | 6 +++--- .../immutable/auxiliary/ImmutablePersonAuxiliaryTest.java | 4 ++-- .../immutable/default_/ImmutablePersonDefaultTest.java | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java b/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java index 0f3b8116af..bf075569db 100644 --- a/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java +++ b/immutables/src/test/java/com/baeldung/immutable/ImmutablePersonTest.java @@ -9,12 +9,12 @@ public class ImmutablePersonTest { @Test public void whenModifying_shouldCreateNewInstance() throws Exception { - final com.baeldung.immutable.ImmutablePerson john = com.baeldung.immutable.ImmutablePerson.builder() + final ImmutablePerson john = ImmutablePerson.builder() .age(42) .name("John") .build(); - final com.baeldung.immutable.ImmutablePerson john43 = john.withAge(43); + final ImmutablePerson john43 = john.withAge(43); assertThat(john) .isNotSameAs(john43); @@ -22,6 +22,6 @@ public class ImmutablePersonTest { assertThat(john.getAge()) .isEqualTo(42); - assertImmutable(com.baeldung.immutable.ImmutablePerson.class); + assertImmutable(ImmutablePerson.class); } } \ No newline at end of file diff --git a/immutables/src/test/java/com/baeldung/immutable/auxiliary/ImmutablePersonAuxiliaryTest.java b/immutables/src/test/java/com/baeldung/immutable/auxiliary/ImmutablePersonAuxiliaryTest.java index a81eeb3a3a..83f9e51ed5 100644 --- a/immutables/src/test/java/com/baeldung/immutable/auxiliary/ImmutablePersonAuxiliaryTest.java +++ b/immutables/src/test/java/com/baeldung/immutable/auxiliary/ImmutablePersonAuxiliaryTest.java @@ -8,13 +8,13 @@ public class ImmutablePersonAuxiliaryTest { @Test public void whenComparing_shouldIgnore() throws Exception { - final com.baeldung.immutable.auxiliary.ImmutablePerson john1 = com.baeldung.immutable.auxiliary.ImmutablePerson.builder() + final ImmutablePerson john1 = ImmutablePerson.builder() .name("John") .age(42) .auxiliaryField("Value1") .build(); - final com.baeldung.immutable.auxiliary.ImmutablePerson john2 = com.baeldung.immutable.auxiliary.ImmutablePerson.builder() + final ImmutablePerson john2 = ImmutablePerson.builder() .name("John") .age(42) .auxiliaryField("Value2") diff --git a/immutables/src/test/java/com/baeldung/immutable/default_/ImmutablePersonDefaultTest.java b/immutables/src/test/java/com/baeldung/immutable/default_/ImmutablePersonDefaultTest.java index 7236212fc4..5cf4ac0cf7 100644 --- a/immutables/src/test/java/com/baeldung/immutable/default_/ImmutablePersonDefaultTest.java +++ b/immutables/src/test/java/com/baeldung/immutable/default_/ImmutablePersonDefaultTest.java @@ -9,7 +9,7 @@ public class ImmutablePersonDefaultTest { @Test public void whenInstantiating_shouldUseDefaultValue() throws Exception { - final com.baeldung.immutable.default_.ImmutablePerson john = com.baeldung.immutable.default_.ImmutablePerson.builder().name("John").build(); + final ImmutablePerson john = ImmutablePerson.builder().name("John").build(); assertThat(john.getAge()).isEqualTo(42); From e700ce2b9ee577b4a9d1ebad1d615f72a112fdf6 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 30 Jul 2016 13:00:30 +0200 Subject: [PATCH 7/7] Remove unnecessary file --- .../src/main/java/com/baeldung/immutable/ValueObject.java | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 immutables/src/main/java/com/baeldung/immutable/ValueObject.java diff --git a/immutables/src/main/java/com/baeldung/immutable/ValueObject.java b/immutables/src/main/java/com/baeldung/immutable/ValueObject.java deleted file mode 100644 index 475c6bf7b1..0000000000 --- a/immutables/src/main/java/com/baeldung/immutable/ValueObject.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.baeldung.immutable; - -public class ValueObject { - public ValueObject() { - - - } -}