Merge pull request #548 from eugenp/grzegorz-immutables
Add examples for "Introduction to Immutables" article
This commit is contained in:
commit
05ed3d9d95
50
immutables/pom.xml
Normal file
50
immutables/pom.xml
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>immutables</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.immutables</groupId>
|
||||||
|
<artifactId>value</artifactId>
|
||||||
|
<version>2.2.10</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>3.5.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mutabilitydetector</groupId>
|
||||||
|
<artifactId>MutabilityDetector</artifactId>
|
||||||
|
<version>0.9.5</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.3</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -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 ImmutablePersonTest {
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
}
|
@ -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 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());
|
||||||
|
}
|
||||||
|
}
|
@ -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 ImmutablePerson john = ImmutablePerson.builder().name("John").build();
|
||||||
|
|
||||||
|
assertThat(john.getAge()).isEqualTo(42);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
1
pom.xml
1
pom.xml
@ -31,6 +31,7 @@
|
|||||||
<module>guava19</module>
|
<module>guava19</module>
|
||||||
<module>handling-spring-static-resources</module>
|
<module>handling-spring-static-resources</module>
|
||||||
<module>httpclient</module>
|
<module>httpclient</module>
|
||||||
|
<module>immutables</module>
|
||||||
<module>jackson</module>
|
<module>jackson</module>
|
||||||
<module>javaxval</module>
|
<module>javaxval</module>
|
||||||
<module>jjwt</module>
|
<module>jjwt</module>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user