Skip to content

Commit

Permalink
#318 Remove/add some NotNull pertaining to property values
Browse files Browse the repository at this point in the history
- ConfigMe doesn't provide null values for properties by default, but is somewhat "agnostic" at allowing other property implementations doing so. This is difficult to represent with the annotations, so some have been replaced by a comment "PV" (for property value) that should always be not null by default, but we cannot technically guarantee it.
  • Loading branch information
ljacqu committed Jun 20, 2023
1 parent 00f0724 commit 4c35833
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/main/java/ch/jalu/configme/SettingsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface SettingsManager {
* @param <T> The property's type
* @return The property's value
*/
<T> T getProperty(@NotNull Property<T> property);
<T> /* PV */ T getProperty(@NotNull Property<T> property);

/**
* Sets a new value for the given property.
Expand All @@ -35,7 +35,7 @@ public interface SettingsManager {
* @param value The new value to assign to the property
* @param <T> The property's type
*/
<T> void setProperty(@NotNull Property<T> property, @NotNull T value);
<T> void setProperty(@NotNull Property<T> property, /* PV */ T value);

/**
* Reloads the configuration.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ch/jalu/configme/SettingsManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected SettingsManagerImpl(@NotNull PropertyResource resource, @NotNull Confi
* @return the property's value
*/
@Override
public <T> T getProperty(@NotNull Property<T> property) {
public <T> @NotNull T getProperty(@NotNull Property<T> property) {
return configurationData.getValue(property);
}

Expand All @@ -66,7 +66,7 @@ public <T> T getProperty(@NotNull Property<T> property) {
* @param <T> the property's type
*/
@Override
public <T> void setProperty(@NotNull Property<T> property, @NotNull T value) {
public <T> void setProperty(@NotNull Property<T> property, /* PV */ T value) {
configurationData.setValue(property, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public interface ConfigurationData {
* @param <T> property type
* @return value associated with the property, or null if not present
*/
<T> @Nullable T getValue(@NotNull Property<T> property);
<T> /* PV */ T getValue(@NotNull Property<T> property);

/**
* Sets the given value for the given property. May throw an exception
Expand All @@ -74,7 +74,7 @@ public interface ConfigurationData {
* @param value the value to set
* @param <T> the property type
*/
<T> void setValue(@NotNull Property<T> property, @NotNull T value);
<T> void setValue(@NotNull Property<T> property, /* PV */ T value);

/**
* Returns if the last call of {@link #initializeValues} had fully valid values in the resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected ConfigurationDataImpl(@NotNull List<? extends Property<?>> allProperti

@Override
@SuppressWarnings("unchecked")
public <T> T getValue(@NotNull Property<T> property) {
public <T> @NotNull T getValue(@NotNull Property<T> property) {
Object value = values.get(property.getPath());
if (value == null) {
throw new ConfigMeException(format("No value exists for property with path '%s'. This may happen if "
Expand All @@ -65,7 +65,7 @@ public <T> T getValue(@NotNull Property<T> property) {
}

@Override
public <T> void setValue(@NotNull Property<T> property, @NotNull T value) {
public <T> void setValue(@NotNull Property<T> property, /* PV */ T value) {
if (property.isValidValue(value)) {
values.put(property.getPath(), value);
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/ch/jalu/configme/properties/BaseProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public boolean isValidValue(@Nullable T value) {
return value != null;
}

/*
* Overridden to set the param as @NotNull. If the value from #determineValue is not null and #isValidValue rejects
* nulls, it is guaranteed that this method will never be called with a null value.
*/
@Override
@SuppressWarnings("NullableProblems")
public abstract @Nullable Object toExportValue(@NotNull T value);

/**
* Constructs the value of the property from the property reader. Returns null if no value is
* available in the reader or if it cannot be used to construct a value for this property.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public InlineArrayProperty(@NotNull String path, T @NotNull [] defaultValue,
}

@Override
public Object toExportValue(T[] value) {
public Object toExportValue(T @NotNull [] value) {
return inlineConverter.toExportValue(value);
}
}
2 changes: 1 addition & 1 deletion src/main/java/ch/jalu/configme/properties/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ default boolean isValidInResource(@NotNull PropertyReader propertyReader) {
*
* @return the default value
*/
@NotNull T getDefaultValue();
@Nullable T getDefaultValue();

/**
* Returns whether the value can be associated to the given property, i.e. whether it fulfills all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public TypeBasedProperty(@NotNull String path, @NotNull T defaultValue, @NotNull
}

@Override
public @Nullable Object toExportValue(T value) {
public @Nullable Object toExportValue(@NotNull T value) {
return type.toExportValue(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ch.jalu.configme.properties.Property;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Return value of {@link Property#determineValue}. Wraps the value to associate with the property, along with a field
Expand All @@ -26,7 +27,7 @@ public class PropertyValue<T> {
* @param value the value associated with the property
* @param isValidInResource true if the value in the resource was fully valid
*/
public PropertyValue(@NotNull T value, boolean isValidInResource) {
public PropertyValue(/* PV */ T value, boolean isValidInResource) {
this.value = value;
this.isValidInResource = isValidInResource;
}
Expand All @@ -38,7 +39,7 @@ public PropertyValue(@NotNull T value, boolean isValidInResource) {
* @param <T> the value type
* @return property value with the given value and the valid flag set to true
*/
public static <T> @NotNull PropertyValue<T> withValidValue(@NotNull T value) {
public static <T> @NotNull PropertyValue<T> withValidValue(/* PV */ T value) {
return new PropertyValue<>(value, true);
}

Expand All @@ -49,14 +50,14 @@ public PropertyValue(@NotNull T value, boolean isValidInResource) {
* @param <T> the value type
* @return property value with the given value and the valid flag set to false
*/
public static <T> @NotNull PropertyValue<T> withValueRequiringRewrite(@NotNull T value) {
public static <T> @NotNull PropertyValue<T> withValueRequiringRewrite(/* PV */ T value) {
return new PropertyValue<>(value, false);
}

/**
* @return the value to associate with the property
*/
public @NotNull T getValue() {
public /* PV */ T getValue() {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public interface PropertyType<T> {
* @param value the value to convert
* @return the value to use in the property export
*/
@Nullable Object toExportValue(T value);
@Nullable Object toExportValue(@Nullable T value);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ch.jalu.configme.beanmapper.leafvaluehandler;

import ch.jalu.configme.properties.types.PrimitivePropertyType;
import ch.jalu.configme.utils.TypeInformation;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.w3c.dom.TypeInfo;

import java.util.Arrays;

Expand All @@ -26,9 +28,9 @@ void shouldRespectOrderOfTransformers() {
CombiningLeafValueHandler result2 = new CombiningLeafValueHandler(Arrays.asList(t1, t2));

// then
assertThat(result1.convert(null, null), equalTo("value_1"));
assertThat(result1.convert(new TypeInformation(String.class), null), equalTo("value_1"));
assertThat(result1.toExportValue(null), equalTo("export_1"));
assertThat(result2.convert(null, null), equalTo("value_1"));
assertThat(result2.convert(new TypeInformation(String.class), null), equalTo("value_1"));
assertThat(result2.toExportValue(null), equalTo("export_1"));
}

Expand All @@ -43,7 +45,7 @@ void shouldGetFirstNonNullResult() {
CombiningLeafValueHandler result = new CombiningLeafValueHandler(t1, t2, t3);

// then
assertThat(result.convert(null, null), equalTo("value_2"));
assertThat(result.convert(new TypeInformation(String.class), null), equalTo("value_2"));
assertThat(result.toExportValue(null), equalTo("export_2"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder;
import ch.jalu.configme.resource.PropertyReader;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;

import static ch.jalu.configme.TestUtils.getExceptionTypeForNullArg;
Expand Down Expand Up @@ -67,7 +68,7 @@ private static final class PropertyTestImpl extends BaseProperty<Byte> {
}

@Override
protected Byte getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder) {
protected Byte getFromReader(@NotNull PropertyReader reader, @NotNull ConvertErrorRecorder errorRecorder) {
Integer value = reader.getInt(getPath());
return value != null && value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE
? value.byteValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void shouldReturnUnknown() {
EnumPropertyType<TimeUnit> propertyType = new EnumPropertyType<>(TimeUnit.class);

// when
TimeUnit result = propertyType.convert("unknown", null);
TimeUnit result = propertyType.convert("unknown", new ConvertErrorRecorder());

// then
assertThat(result, nullValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import ch.jalu.configme.properties.Property;
import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder;
import ch.jalu.configme.samples.TestEnum;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -73,7 +74,8 @@ private static final class EnumSetProperty extends BaseProperty<Set<TestEnum>> {
}

@Override
protected Set<TestEnum> getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder) {
protected Set<TestEnum> getFromReader(@NotNull PropertyReader reader,
@NotNull ConvertErrorRecorder errorRecorder) {
List<?> list = reader.getList(getPath());
if (list == null) {
return null;
Expand Down

0 comments on commit 4c35833

Please sign in to comment.