Skip to content

Commit 05ed3d9

Browse files
authored
Merge pull request eugenp#548 from eugenp/grzegorz-immutables
Add examples for "Introduction to Immutables" article
2 parents abe4c07 + e700ce2 commit 05ed3d9

11 files changed

Lines changed: 196 additions & 0 deletions

File tree

‎immutables/pom.xml‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>immutables</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.immutables</groupId>
14+
<artifactId>value</artifactId>
15+
<version>2.2.10</version>
16+
</dependency>
17+
<dependency>
18+
<groupId>junit</groupId>
19+
<artifactId>junit</artifactId>
20+
<version>4.12</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.assertj</groupId>
25+
<artifactId>assertj-core</artifactId>
26+
<version>3.5.2</version>
27+
<scope>test</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.mutabilitydetector</groupId>
31+
<artifactId>MutabilityDetector</artifactId>
32+
<version>0.9.5</version>
33+
<scope>test</scope>
34+
</dependency>
35+
</dependencies>
36+
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<groupId>org.apache.maven.plugins</groupId>
41+
<artifactId>maven-compiler-plugin</artifactId>
42+
<version>3.3</version>
43+
<configuration>
44+
<source>1.8</source>
45+
<target>1.8</target>
46+
</configuration>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
</project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.immutable;
2+
3+
import org.immutables.value.Value;
4+
5+
@Value.Immutable
6+
public interface Address {
7+
String getStreetName();
8+
Integer getNumber();
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.immutable;
2+
3+
import org.immutables.value.Value;
4+
5+
@Value.Immutable
6+
public abstract class Person {
7+
abstract String getName();
8+
abstract Integer getAge();
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.immutable.auxiliary;
2+
3+
4+
import org.immutables.value.Value;
5+
6+
@Value.Immutable
7+
public abstract class Person {
8+
abstract String getName();
9+
abstract Integer getAge();
10+
11+
@Value.Auxiliary
12+
abstract String getAuxiliaryField();
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.immutable.default_;
2+
3+
import org.immutables.value.Value;
4+
5+
@Value.Immutable(prehash = true)
6+
public abstract class Person {
7+
8+
abstract String getName();
9+
10+
@Value.Default
11+
Integer getAge() {
12+
return 42;
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.immutable.parameter;
2+
3+
4+
import org.immutables.value.Value;
5+
6+
@Value.Immutable
7+
public abstract class Person {
8+
9+
@Value.Parameter
10+
abstract String getName();
11+
12+
@Value.Parameter
13+
abstract Integer getAge();
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.immutable.prehash;
2+
3+
import org.immutables.value.Value;
4+
5+
@Value.Immutable(prehash = true)
6+
public abstract class Person {
7+
abstract String getName();
8+
abstract Integer getAge();
9+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.immutable;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertImmutable;
7+
8+
public class ImmutablePersonTest {
9+
10+
@Test
11+
public void whenModifying_shouldCreateNewInstance() throws Exception {
12+
final ImmutablePerson john = ImmutablePerson.builder()
13+
.age(42)
14+
.name("John")
15+
.build();
16+
17+
final ImmutablePerson john43 = john.withAge(43);
18+
19+
assertThat(john)
20+
.isNotSameAs(john43);
21+
22+
assertThat(john.getAge())
23+
.isEqualTo(42);
24+
25+
assertImmutable(ImmutablePerson.class);
26+
}
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.immutable.auxiliary;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class ImmutablePersonAuxiliaryTest {
8+
9+
@Test
10+
public void whenComparing_shouldIgnore() throws Exception {
11+
final ImmutablePerson john1 = ImmutablePerson.builder()
12+
.name("John")
13+
.age(42)
14+
.auxiliaryField("Value1")
15+
.build();
16+
17+
final ImmutablePerson john2 = ImmutablePerson.builder()
18+
.name("John")
19+
.age(42)
20+
.auxiliaryField("Value2")
21+
.build();
22+
23+
24+
assertThat(john1.equals(john2))
25+
.isTrue();
26+
27+
assertThat(john1.toString())
28+
.isEqualTo(john2.toString());
29+
30+
assertThat(john1.hashCode())
31+
.isEqualTo(john2.hashCode());
32+
}
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.immutable.default_;
2+
3+
import org.junit.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class ImmutablePersonDefaultTest {
8+
9+
@Test
10+
public void whenInstantiating_shouldUseDefaultValue() throws Exception {
11+
12+
final ImmutablePerson john = ImmutablePerson.builder().name("John").build();
13+
14+
assertThat(john.getAge()).isEqualTo(42);
15+
16+
}
17+
}

0 commit comments

Comments
 (0)