JAVA-10625: Remove module immutables
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.immutable;
|
||||
|
||||
import org.immutables.value.Value;
|
||||
|
||||
@Value.Immutable
|
||||
public interface Address {
|
||||
String getStreetName();
|
||||
Integer getNumber();
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.immutable;
|
||||
|
||||
import org.immutables.value.Value;
|
||||
|
||||
@Value.Immutable
|
||||
public abstract class Person {
|
||||
abstract String getName();
|
||||
abstract Integer getAge();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
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 ImmutablePersonUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenModifying_shouldCreateNewInstance() throws Exception {
|
||||
final ImmutablePerson john = ImmutablePerson.builder()
|
||||
.age(42)
|
||||
.name("John")
|
||||
.build();
|
||||
|
||||
final ImmutablePerson john43 = john.withAge(43);
|
||||
|
||||
assertThat(john)
|
||||
.isNotSameAs(john43);
|
||||
|
||||
assertThat(john.getAge())
|
||||
.isEqualTo(42);
|
||||
|
||||
assertImmutable(ImmutablePerson.class);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.immutable.auxiliary;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ImmutablePersonAuxiliaryUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenComparing_shouldIgnore() throws Exception {
|
||||
final ImmutablePerson john1 = ImmutablePerson.builder()
|
||||
.name("John")
|
||||
.age(42)
|
||||
.auxiliaryField("Value1")
|
||||
.build();
|
||||
|
||||
final ImmutablePerson john2 = 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());
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.immutable.default_;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ImmutablePersonDefaultUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenInstantiating_shouldUseDefaultValue() throws Exception {
|
||||
|
||||
final ImmutablePerson john = ImmutablePerson.builder().name("John").build();
|
||||
|
||||
assertThat(john.getAge()).isEqualTo(42);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user