From eef41ec02865d7da287f65edb6bb259903a0dbe4 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 30 Jul 2016 12:54:27 +0200 Subject: [PATCH] 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