-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
189 additions
and
0 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
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
72 changes: 72 additions & 0 deletions
72
src/main/java/ch/jalu/configme/properties/SetProperty.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,72 @@ | ||
package ch.jalu.configme.properties; | ||
|
||
import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; | ||
import ch.jalu.configme.properties.types.PropertyType; | ||
import ch.jalu.configme.resource.PropertyReader; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collector; | ||
import java.util.stream.Collectors; | ||
|
||
public class SetProperty<T> extends BaseProperty<Set<T>> { | ||
|
||
private final PropertyType<T> type; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param path the path of the property | ||
* @param type the property type | ||
* @param defaultValue the values that make up the entries of the default set | ||
*/ | ||
@SafeVarargs | ||
public SetProperty(String path, PropertyType<T> type, T... defaultValue) { | ||
this(path, type, newSet(defaultValue)); | ||
} | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param path the path of the property | ||
* @param type the property type | ||
* @param defaultValue the default value of the property | ||
*/ | ||
public SetProperty(String path, PropertyType<T> type, Set<T> defaultValue) { | ||
super(path, Collections.unmodifiableSet(defaultValue)); | ||
Objects.requireNonNull(type, "type"); | ||
this.type = type; | ||
} | ||
|
||
@Override | ||
protected Set<T> getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder) { | ||
List<?> list = reader.getList(getPath()); | ||
|
||
if (list != null) { | ||
return list.stream() | ||
.map(elem -> type.convert(elem, errorRecorder)) | ||
.filter(Objects::nonNull) | ||
.collect(setCollector()); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object toExportValue(Set<T> value) { | ||
return value.stream() | ||
.map(type::toExportValue) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
protected Collector<T, ?, Set<T>> setCollector() { | ||
return Collectors.collectingAndThen(Collectors.toCollection(LinkedHashSet::new), Collections::unmodifiableSet); | ||
} | ||
|
||
private static <E> Set<E> newSet(E[] array) { | ||
return Arrays.stream(array).collect(Collectors.toCollection(LinkedHashSet::new)); | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
src/test/java/ch/jalu/configme/properties/SetPropertyTest.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,70 @@ | ||
package ch.jalu.configme.properties; | ||
|
||
import ch.jalu.configme.properties.convertresult.PropertyValue; | ||
import ch.jalu.configme.properties.types.PrimitivePropertyType; | ||
import ch.jalu.configme.resource.PropertyReader; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.contains; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
|
||
/** | ||
* Test for {@link SetProperty}. | ||
*/ | ||
class SetPropertyTest { | ||
|
||
@Test | ||
void shouldReturnValueFromSource() { | ||
// given | ||
SetProperty<Double> property = new SetProperty<>("error.codes", PrimitivePropertyType.DOUBLE, | ||
1.414, 1.732, 2.0); | ||
PropertyReader reader = mock(PropertyReader.class); | ||
List list = Arrays.asList(3.6, 6.9, 10.2); | ||
given(reader.getList("error.codes")).willReturn(list); | ||
|
||
// when | ||
PropertyValue<Set<Double>> result = property.determineValue(reader); | ||
|
||
// then | ||
assertThat(result.isValidInResource(), equalTo(true)); | ||
assertThat(result.getValue(), contains(3.6, 6.9, 10.2)); | ||
} | ||
|
||
@Test | ||
void shouldReturnDefaultValue() { | ||
// given | ||
SetProperty<Integer> property = new SetProperty<>("error.codes", PrimitivePropertyType.INTEGER, | ||
-27, -8); | ||
PropertyReader reader = mock(PropertyReader.class); | ||
given(reader.getList("error.codes")).willReturn(null); | ||
|
||
// when | ||
PropertyValue<Set<Integer>> result = property.determineValue(reader); | ||
|
||
// then | ||
assertThat(result.isValidInResource(), equalTo(false)); | ||
assertThat(result.getValue(), contains(-27, -8)); // default value | ||
} | ||
|
||
@Test | ||
void shouldCreateExportValue() { | ||
// given | ||
SetProperty<Double> property = new SetProperty<>("error.codes", PrimitivePropertyType.DOUBLE, | ||
1.414, 1.732, 2.0); | ||
Set<Double> value = new LinkedHashSet<>(Arrays.asList(2.14, 3.28, 5.56)); | ||
|
||
// when | ||
Object exportValue = property.toExportValue(value); | ||
|
||
// then | ||
assertThat(exportValue, equalTo(Arrays.asList(2.14, 3.28, 5.56))); | ||
} | ||
} |