-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1702 from DamnClin/simple-collections-utility
Simple collections utility
- Loading branch information
Showing
4 changed files
with
187 additions
and
2 deletions.
There are no files selected for viewing
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
65 changes: 65 additions & 0 deletions
65
src/main/resources/generator/server/javatool/base/ProjectCollections.java.mustache
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,65 @@ | ||
package {{packageName}}.common.domain; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* Null safe utility class to manage collections | ||
*/ | ||
public final class {{collectionClass}}Collections { | ||
private {{collectionClass}}Collections() {} | ||
|
||
/** | ||
* Get an immutable collection from the given collection | ||
* | ||
* @param <T> | ||
* Type of this collection | ||
* @param collection | ||
* input collection | ||
* @return An immutable collection | ||
*/ | ||
public static <T> Collection<T> immutable(Collection<T> collection) { | ||
if (collection == null) { | ||
return Set.of(); | ||
} | ||
|
||
return Collections.unmodifiableCollection(collection); | ||
} | ||
|
||
/** | ||
* Get an immutable set from the given set | ||
* | ||
* @param <T> | ||
* Type of this set | ||
* @param set | ||
* input set | ||
* @return An immutable set | ||
*/ | ||
public static <T> Set<T> immutable(Set<T> set) { | ||
if (set == null) { | ||
return Set.of(); | ||
} | ||
|
||
return Collections.unmodifiableSet(set); | ||
} | ||
|
||
/** | ||
* Get an immutable set from the given list | ||
* | ||
* @param <T> | ||
* Type of this list | ||
* @param list | ||
* input list | ||
* @return An immutable list | ||
*/ | ||
public static <T> List<T> immutable(List<T> list) { | ||
if (list == null) { | ||
return List.of(); | ||
} | ||
|
||
return Collections.unmodifiableList(list); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/main/resources/generator/server/javatool/base/ProjectCollectionsTest.java.mustache
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,89 @@ | ||
package {{packageName}}.common.domain; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import {{packageName}}.UnitTest; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
@UnitTest | ||
class {{collectionClass}}CollectionsTest { | ||
@Nested | ||
@DisplayName("Collections") | ||
class {{collectionClass}}CollectionsCollectionsTest { | ||
@Test | ||
void shouldGetEmptyImmutableCollectionFromNullCollection() { | ||
Collection<Object> input = null; | ||
Collection<Object> collection = {{collectionClass}}Collections.immutable(input); | ||
|
||
assertThat(collection).isEmpty(); | ||
assertThatThrownBy(() -> collection.clear()).isExactlyInstanceOf(UnsupportedOperationException.class); | ||
} | ||
|
||
@Test | ||
void shouldGetImmutableCollectionFromMutableCollection() { | ||
Collection<String> input = new ArrayList<>(); | ||
input.add("value"); | ||
Collection<String> collection = {{collectionClass}}Collections.immutable(input); | ||
|
||
assertThat(collection).containsExactly("value"); | ||
assertThatThrownBy(() -> collection.clear()).isExactlyInstanceOf(UnsupportedOperationException.class); | ||
} | ||
} | ||
|
||
@Nested | ||
@DisplayName("Set") | ||
class {{collectionClass}}CollectionsSetTest { | ||
@Test | ||
void shouldGetEmptyImmutableCollectionFromNullCollection() { | ||
Set<Object> input = null; | ||
Set<Object> set = {{collectionClass}}Collections.immutable(input); | ||
|
||
assertThat(set).isEmpty(); | ||
assertThatThrownBy(() -> set.clear()).isExactlyInstanceOf(UnsupportedOperationException.class); | ||
} | ||
|
||
@Test | ||
void shouldGetImmutableCollectionFromMutableCollection() { | ||
Set<String> input = new HashSet<>(); | ||
input.add("value"); | ||
Set<String> set = {{collectionClass}}Collections.immutable(input); | ||
|
||
assertThat(set).containsExactly("value"); | ||
assertThatThrownBy(() -> set.clear()).isExactlyInstanceOf(UnsupportedOperationException.class); | ||
} | ||
} | ||
|
||
@Nested | ||
@DisplayName("List") | ||
class {{collectionClass}}CollectionsListTest { | ||
@Test | ||
void shouldGetEmptyImmutableCollectionFromNullCollection() { | ||
List<Object> input = null; | ||
List<Object> list = {{collectionClass}}Collections.immutable(input); | ||
|
||
assertThat(list).isEmpty(); | ||
assertThatThrownBy(() -> list.clear()).isExactlyInstanceOf(UnsupportedOperationException.class); | ||
} | ||
|
||
@Test | ||
void shouldGetImmutableCollectionFromMutableCollection() { | ||
List<String> input = new ArrayList<>(); | ||
input.add("value"); | ||
List<String> list = {{collectionClass}}Collections.immutable(input); | ||
|
||
assertThat(list).containsExactly("value"); | ||
assertThatThrownBy(() -> list.clear()).isExactlyInstanceOf(UnsupportedOperationException.class); | ||
} | ||
} | ||
} |
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