JAVA-10625: Remove module immutables

This commit is contained in:
sampadawagde
2022-04-12 20:43:57 +05:30
parent 44726ef24c
commit bebfb65095
14 changed files with 14 additions and 53 deletions

View File

@@ -17,4 +17,5 @@ Remember, for advanced libraries like [Jackson](/jackson) and [JUnit](/testing-m
- [Using NullAway to Avoid NullPointerExceptions](https://www.baeldung.com/java-nullaway)
- [Introduction to Alibaba Arthas](https://www.baeldung.com/java-alibaba-arthas-intro)
- [Introduction to Structurizr](https://www.baeldung.com/structurizr)
- [Introduction to Immutables](https://www.baeldung.com/immutables)
- More articles [[<-- prev]](../libraries-2) [[next -->]](../libraries-4)

View File

@@ -112,6 +112,17 @@
<artifactId>structurizr-plantuml</artifactId>
<version>${structurizr.version}</version>
</dependency>
<dependency>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<version>${immutables.version}</version>
</dependency>
<dependency>
<groupId>org.mutabilitydetector</groupId>
<artifactId>MutabilityDetector</artifactId>
<version>${mutabilitydetector.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
@@ -256,6 +267,8 @@
<plexus-compiler.version>2.8</plexus-compiler.version>
<errorprone.version>2.1.3</errorprone.version>
<structurizr.version>1.0.0</structurizr.version>
<immutables.version>2.5.6</immutables.version>
<mutabilitydetector.version>0.9.6</mutabilitydetector.version>
</properties>
</project>

View File

@@ -0,0 +1,9 @@
package com.baeldung.immutable;
import org.immutables.value.Value;
@Value.Immutable
public interface Address {
String getStreetName();
Integer getNumber();
}

View File

@@ -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();
}

View File

@@ -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();
}

View File

@@ -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;
}
}

View File

@@ -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();
}

View File

@@ -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();
}

View File

@@ -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);
}
}

View File

@@ -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());
}
}

View File

@@ -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);
}
}