forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bael 2712 lombok builder singular (eugenp#6435)
* updated lombok libs to 1.18.4 * sketched out basic test class for @Singular * more examples showing other collection types, non-standard plural * fixed indentation
- Loading branch information
1 parent
e506bc1
commit 65ae0ce
Showing
3 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
lombok/src/main/java/com/baeldung/lombok/builder/singular/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.baeldung.lombok.builder.singular; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Singular; | ||
|
||
import java.time.LocalDate; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
@Getter | ||
@Builder | ||
public class Person { | ||
|
||
private final String givenName; | ||
private final String additionalName; | ||
private final String familyName; | ||
|
||
private final List<String> tags; | ||
@Singular private final List<String> interests; | ||
@Singular private final Set<String> skills; | ||
@Singular private final Map<String, LocalDate> awards; | ||
} |
14 changes: 14 additions & 0 deletions
14
lombok/src/main/java/com/baeldung/lombok/builder/singular/Sea.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.baeldung.lombok.builder.singular; | ||
|
||
import java.util.List; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Singular; | ||
|
||
@Getter | ||
@Builder | ||
public class Sea { | ||
|
||
@Singular private final List<String> grasses; | ||
@Singular("oneFish") private final List<String> fish; | ||
} |
192 changes: 192 additions & 0 deletions
192
...om/baeldung/lombok/builder/singular/BuilderWithSingularSupportForCollectionsUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package com.baeldung.lombok.builder.singular; | ||
|
||
import org.junit.Test; | ||
|
||
import java.time.LocalDate; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
|
||
public class BuilderWithSingularSupportForCollectionsUnitTest { | ||
|
||
@Test | ||
public void canAddMultipleElementsAsNewCollection() throws Exception { | ||
Person person = Person.builder() | ||
.givenName("Aaron") | ||
.additionalName("A") | ||
.familyName("Aardvark") | ||
.tags(Arrays.asList("fictional", "incidental")) | ||
.build(); | ||
|
||
assertThat(person.getTags(), containsInAnyOrder("fictional", "incidental")); | ||
} | ||
|
||
@Test | ||
public void canUpdateCollectionAfterBuildIfMutableCollectionPassedToBuilder() throws Exception { | ||
|
||
List<String> tags = new ArrayList(); | ||
tags.add("fictional"); | ||
tags.add("incidental"); | ||
Person person = Person.builder() | ||
.givenName("Aaron") | ||
.additionalName("A") | ||
.familyName("Aardvark") | ||
.tags(tags) | ||
.build(); | ||
person.getTags() | ||
.clear(); | ||
person.getTags() | ||
.add("non-fictional"); | ||
person.getTags() | ||
.add("important"); | ||
|
||
assertThat(person.getTags(), containsInAnyOrder("non-fictional", "important")); | ||
} | ||
|
||
@Test(expected = UnsupportedOperationException.class) | ||
public void cannotUpdateCollectionAfterBuildIfImmutableCollectionPassedToBuilder() throws Exception { | ||
List<String> tags = Arrays.asList("fictional", "incidental"); | ||
Person person = Person.builder() | ||
.givenName("Aaron") | ||
.additionalName("A") | ||
.familyName("Aardvark") | ||
.tags(tags) | ||
.build(); | ||
person.getTags() | ||
.clear(); | ||
} | ||
|
||
@Test | ||
public void canAssignToSingularAnnotatedCollectionOneByOne() throws Exception { | ||
|
||
Person person = Person.builder() | ||
.givenName("Aaron") | ||
.additionalName("A") | ||
.familyName("Aardvark") | ||
.interest("history") | ||
.interest("sport") | ||
.build(); | ||
|
||
assertThat(person.getInterests(), containsInAnyOrder("sport", "history")); | ||
} | ||
|
||
@Test(expected = UnsupportedOperationException.class) | ||
public void singularAnnotatedBuilderCreatesImmutableCollection() throws Exception { | ||
|
||
Person person = Person.builder() | ||
.givenName("Aaron") | ||
.additionalName("A") | ||
.familyName("Aardvark") | ||
.interest("history") | ||
.interest("sport") | ||
.build(); | ||
person.getInterests() | ||
.clear(); | ||
} | ||
|
||
@Test | ||
public void unpopulatedListsCreatedAsNullIfNotSingularButEmptyArrayIfSingular() throws Exception { | ||
|
||
Person person = Person.builder() | ||
.givenName("Aaron") | ||
.additionalName("A") | ||
.familyName("Aardvark") | ||
.build(); | ||
assertThat(person.getInterests(), hasSize(0)); | ||
assertThat(person.getSkills(), hasSize(0)); | ||
assertThat(person.getAwards() | ||
.keySet(), hasSize(0)); | ||
assertThat(person.getTags(), is(nullValue())); | ||
} | ||
|
||
@Test | ||
public void singularSupportsSetsToo() throws Exception { | ||
|
||
Person person = Person.builder() | ||
.givenName("Aaron") | ||
.additionalName("A") | ||
.familyName("Aardvark") | ||
.skill("singing") | ||
.skill("dancing") | ||
.build(); | ||
assertThat(person.getSkills(), contains("singing", "dancing")); | ||
} | ||
|
||
@Test | ||
public void singularSetsAreLenientWithDuplicates() throws Exception { | ||
|
||
Person person = Person.builder() | ||
.givenName("Aaron") | ||
.additionalName("A") | ||
.familyName("Aardvark") | ||
.interest("singing") | ||
.interest("singing") | ||
.skill("singing") | ||
.skill("singing") | ||
.build(); | ||
assertThat(person.getInterests(), contains("singing", "singing")); | ||
assertThat(person.getSkills(), contains("singing")); | ||
} | ||
|
||
@Test | ||
public void singularSupportsMapsToo() throws Exception { | ||
|
||
Person person = Person.builder() | ||
.givenName("Aaron") | ||
.additionalName("A") | ||
.familyName("Aardvark") | ||
.award("Singer of the Year", LocalDate.now() | ||
.minusYears(5)) | ||
.award("Best Dancer", LocalDate.now() | ||
.minusYears(2)) | ||
.build(); | ||
assertThat(person.getAwards() | ||
.keySet(), contains("Singer of the Year", "Best Dancer")); | ||
assertThat(person.getAwards() | ||
.get("Best Dancer"), | ||
is(LocalDate.now() | ||
.minusYears(2))); | ||
} | ||
|
||
@Test | ||
public void singularIsLenientWithMapKeys() throws Exception { | ||
|
||
Person person = Person.builder() | ||
.givenName("Aaron") | ||
.additionalName("A") | ||
.familyName("Aardvark") | ||
.award("Best Dancer", LocalDate.now() | ||
.minusYears(5)) | ||
.award("Best Dancer", LocalDate.now() | ||
.minusYears(4)) | ||
.award("Best Dancer", LocalDate.now() | ||
.minusYears(3)) | ||
.award("Best Dancer", LocalDate.now() | ||
.minusYears(2)) | ||
.award("Best Dancer", LocalDate.now() | ||
.minusYears(1)) | ||
.build(); | ||
assertThat(person.getAwards() | ||
.keySet(), hasSize(1)); | ||
assertThat(person.getAwards() | ||
.get("Best Dancer"), | ||
is(LocalDate.now() | ||
.minusYears(1))); | ||
} | ||
|
||
@Test | ||
public void wordsWithNonStandardPlurals() throws Exception { | ||
Sea sea = Sea.builder() | ||
.grass("Dulse") | ||
.grass("Kelp") | ||
.oneFish("Cod") | ||
.oneFish("Mackerel") | ||
.build(); | ||
assertThat(sea.getGrasses(), contains("Dulse", "Kelp")); | ||
assertThat(sea.getFish(), contains("Cod", "Mackerel")); | ||
} | ||
|
||
} |