-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create CollectionProperty parent type
- Loading branch information
Showing
8 changed files
with
189 additions
and
60 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
src/main/java/ch/jalu/configme/properties/CollectionProperty.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,71 @@ | ||
package ch.jalu.configme.properties; | ||
|
||
import ch.jalu.configme.properties.types.CollectionPropertyType; | ||
import ch.jalu.configme.properties.types.PropertyType; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.stream.Collector; | ||
|
||
/** | ||
* General {@link Collection} property. Used as common parent type for all collection properties. | ||
* | ||
* @param <E> the type of the entries in the collection | ||
* @param <C> the concrete collection type | ||
* @see ListProperty | ||
* @see SetProperty | ||
*/ | ||
public class CollectionProperty<E, C extends Collection<E>> extends TypeBasedProperty<C> { | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param path the path of the property | ||
* @param collectionType the property type | ||
* @param defaultValue the default value of the property | ||
*/ | ||
public CollectionProperty(@NotNull String path, @NotNull PropertyType<C> collectionType, @NotNull C defaultValue) { | ||
super(path, collectionType, defaultValue); | ||
} | ||
|
||
/** | ||
* Constructs a new collection property based on the given entry type and collector. | ||
* | ||
* @param path the path of the property | ||
* @param entryType the collection type | ||
* @param collector the collection type | ||
* @param defaultValue the default value | ||
* @param <E> the type of the entries in the collection | ||
* @param <C> the concrete collection type | ||
* @return a new collection property | ||
*/ | ||
public static <E, C extends Collection<E>> @NotNull Property<C> of(@NotNull String path, | ||
@NotNull PropertyType<E> entryType, | ||
@NotNull Collector<E, ?, C> collector, | ||
@NotNull C defaultValue) { | ||
CollectionPropertyType<E, C> collectionPropertyType = CollectionPropertyType.of(entryType, collector); | ||
return new CollectionProperty<>(path, collectionPropertyType, defaultValue); | ||
} | ||
|
||
/** | ||
* Constructs a new collection property based on the given entry type and collector. | ||
* | ||
* @param path the path of the property | ||
* @param entryType the collection type | ||
* @param collector the collection type | ||
* @param defaultValueEntries the entries that make up the default value | ||
* @param <E> the type of the entries in the collection | ||
* @param <C> the concrete collection type | ||
* @return a new collection property | ||
*/ | ||
@SafeVarargs | ||
public static <E, C extends Collection<E>> @NotNull Property<C> of(@NotNull String path, | ||
@NotNull PropertyType<E> entryType, | ||
@NotNull Collector<E, ?, C> collector, | ||
E @NotNull ... defaultValueEntries) { | ||
CollectionPropertyType<E, C> collectionPropertyType = CollectionPropertyType.of(entryType, collector); | ||
C defaultValue = Arrays.stream(defaultValueEntries).collect(collector); | ||
return new CollectionProperty<>(path, collectionPropertyType, defaultValue); | ||
} | ||
} |
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
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
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
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
96 changes: 96 additions & 0 deletions
96
src/test/java/ch/jalu/configme/properties/CollectionPropertyTest.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,96 @@ | ||
package ch.jalu.configme.properties; | ||
|
||
import ch.jalu.configme.properties.convertresult.PropertyValue; | ||
import ch.jalu.configme.properties.types.CollectionPropertyType; | ||
import ch.jalu.configme.properties.types.NumberType; | ||
import ch.jalu.configme.resource.PropertyReader; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.TreeSet; | ||
import java.util.Vector; | ||
import java.util.stream.Collectors; | ||
|
||
import static ch.jalu.configme.TestUtils.isErrorValueOf; | ||
import static ch.jalu.configme.TestUtils.isValidValueOf; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.empty; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
/** | ||
* Test for {@link CollectionProperty}. | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
class CollectionPropertyTest { | ||
|
||
@Mock | ||
private PropertyReader reader; | ||
|
||
@Test | ||
void shouldUseGivenPropertyType() { | ||
// given | ||
CollectionPropertyType<Double, TreeSet<Double>> treeSetType = | ||
CollectionPropertyType.of(NumberType.DOUBLE, Collectors.toCollection(TreeSet::new)); | ||
Property<TreeSet<Double>> sortedValuesProperty = | ||
new CollectionProperty<>("values", treeSetType, new TreeSet<>()); | ||
given(reader.getObject("values")).willReturn(Arrays.asList(16, 9, 4)); | ||
|
||
// when | ||
PropertyValue<TreeSet<Double>> value = sortedValuesProperty.determineValue(reader); | ||
|
||
// then | ||
assertThat(value.isValidInResource(), equalTo(true)); | ||
assertThat(value.getValue(), instanceOf(TreeSet.class)); | ||
assertThat(value.getValue(), contains(4.0, 9.0, 16.0)); | ||
} | ||
|
||
@Test | ||
void shouldCreatePropertyWithEntryTypeAndCollector() { | ||
// given | ||
Property<Vector<Double>> valuesProperty = CollectionProperty.of( | ||
"values", | ||
NumberType.DOUBLE, | ||
Collectors.toCollection(Vector::new), | ||
new Vector<>(Arrays.asList(3.0, 4.0))); | ||
given(reader.getObject("values")).willReturn(Arrays.asList(1.0, 2.5, 4.0)); | ||
|
||
// when | ||
PropertyValue<Vector<Double>> value = valuesProperty.determineValue(reader); | ||
|
||
// then | ||
assertThat(valuesProperty.getPath(), equalTo("values")); | ||
assertThat(valuesProperty.getDefaultValue(), instanceOf(Vector.class)); | ||
assertThat(valuesProperty.getDefaultValue(), contains(3.0, 4.0)); | ||
|
||
assertThat(value.isValidInResource(), equalTo(true)); | ||
assertThat(value.getValue(), instanceOf(Vector.class)); | ||
assertThat(value.getValue(), contains(1.0, 2.5, 4.0)); | ||
} | ||
|
||
@Test | ||
void shouldCreatePropertyWithEntryTypeAndCollectorAndVarargsDefaultValue() { | ||
// given | ||
Property<List<Long>> property1 = CollectionProperty.of("p1", NumberType.LONG, Collectors.toList()); | ||
Property<List<Long>> property2 = CollectionProperty.of("p2", NumberType.LONG, Collectors.toList(), 4L, 6L); | ||
given(reader.getObject("p1")).willReturn("invalid"); | ||
given(reader.getObject("p2")).willReturn(Arrays.asList("1", "2")); | ||
|
||
// when | ||
PropertyValue<List<Long>> value1 = property1.determineValue(reader); | ||
PropertyValue<List<Long>> value2 = property2.determineValue(reader); | ||
|
||
// then | ||
assertThat(property1.getDefaultValue(), empty()); | ||
assertThat(property2.getDefaultValue(), contains(4L, 6L)); | ||
assertThat(value1, isErrorValueOf(Collections.emptyList())); // default value | ||
assertThat(value2, isValidValueOf(Arrays.asList(1L, 2L))); | ||
} | ||
} |
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
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