Skip to content

Commit

Permalink
Fix #111 Add SetProperty type
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacqu committed Jan 10, 2020
1 parent 0870bc3 commit 87354a6
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/java/ch/jalu/configme/properties/PropertyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.IntFunction;
import java.util.stream.Collectors;

/**
* Builder for complex types of properties.
Expand Down Expand Up @@ -191,6 +194,29 @@ public Property<List<T>> build() {
}
}

/**
* Builder for {@link SetProperty}.
*
* @param <T> the type of the elements in the set
*/
public static class SetPropertyBuilder<T> extends PropertyBuilder<T, Set<T>, SetPropertyBuilder<T>> {

public SetPropertyBuilder(PropertyType<T> type) {
super(type);
}

public SetPropertyBuilder<T> defaultValue(T... defaultValue) {
Set<T> defaultSet = Arrays.stream(defaultValue)
.collect(Collectors.toCollection(LinkedHashSet::new));
return super.defaultValue(defaultSet);
}

@Override
public Property<Set<T>> build() {
return new SetProperty<>(getPath(), getType(), getDefaultValue());
}
}

/**
* Function taking three arguments, which returns a property of the given type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public static <T> PropertyBuilder.ListPropertyBuilder<T> listProperty(PropertyTy
return new PropertyBuilder.ListPropertyBuilder<>(type);
}

public static <T> PropertyBuilder.SetPropertyBuilder<T> setProperty(PropertyType<T> type) {
return new PropertyBuilder.SetPropertyBuilder<>(type);
}

public static <T> PropertyBuilder.MapPropertyBuilder<T> mapProperty(PropertyType<T> type) {
return new PropertyBuilder.MapPropertyBuilder<>(type);
}
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/ch/jalu/configme/properties/SetProperty.java
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));
}
}
15 changes: 15 additions & 0 deletions src/test/java/ch/jalu/configme/properties/PropertyBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.List;
import java.util.Map;
import java.util.Set;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anEmptyMap;
Expand Down Expand Up @@ -167,6 +168,20 @@ void shouldCreateListProperty() {
assertThat(property.getDefaultValue(), contains(TestEnum.FOURTH, TestEnum.SECOND));
}

@Test
void shouldCreateSetProperty() {
// given / when
Property<Set<Integer>> property = new PropertyBuilder.SetPropertyBuilder<>(PrimitivePropertyType.INTEGER)
.path("path.to.set")
.defaultValue(1, 4, 2, 5)
.build();

// then
assertThat(property, instanceOf(SetProperty.class));
assertThat(property.getPath(), equalTo("path.to.set"));
assertThat(property.getDefaultValue(), contains(1, 4, 2, 5));
}

@Test
void shouldThrowForMissingDefaultValue() {
// given / when / then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static ch.jalu.configme.properties.PropertyInitializer.optionalEnumProperty;
import static ch.jalu.configme.properties.PropertyInitializer.optionalIntegerProperty;
import static ch.jalu.configme.properties.PropertyInitializer.optionalStringProperty;
import static ch.jalu.configme.properties.PropertyInitializer.setProperty;
import static ch.jalu.configme.properties.PropertyInitializer.typeBasedProperty;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
Expand Down Expand Up @@ -48,6 +49,7 @@ void shouldInstantiateProperties() {
void shouldInstantiateBuilders() {
assertThat(typeBasedProperty(PrimitivePropertyType.STRING), instanceOf(PropertyBuilder.TypeBasedPropertyBuilder.class));
assertThat(listProperty(PrimitivePropertyType.INTEGER), instanceOf(PropertyBuilder.ListPropertyBuilder.class));
assertThat(setProperty(PrimitivePropertyType.FLOAT), instanceOf(PropertyBuilder.SetPropertyBuilder.class));
assertThat(mapProperty(PrimitivePropertyType.DOUBLE), instanceOf(PropertyBuilder.MapPropertyBuilder.class));
assertThat(arrayProperty(PrimitivePropertyType.BOOLEAN, Boolean[]::new), instanceOf(PropertyBuilder.ArrayPropertyBuilder.class));
assertThat(inlineArrayProperty(StandardInlineArrayConverters.FLOAT), instanceOf(PropertyBuilder.InlineArrayPropertyBuilder.class));
Expand Down
70 changes: 70 additions & 0 deletions src/test/java/ch/jalu/configme/properties/SetPropertyTest.java
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)));
}
}

0 comments on commit 87354a6

Please sign in to comment.