Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding null annotations to config.core.internal.normalization #4505

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,24 @@ public static void applyDefaultConfiguration(Configuration configuration,
* @throws IllegalArgumentException if an invalid type has been given
*/
public static Object normalizeType(Object value, @Nullable ConfigDescriptionParameter configDescriptionParameter) {
Object result = null;
if (configDescriptionParameter != null) {
Normalizer normalizer = NormalizerFactory.getNormalizer(configDescriptionParameter);
return normalizer.normalize(value);
result = normalizer.normalize(value);
} else if (value instanceof Boolean) {
return NormalizerFactory.getNormalizer(Type.BOOLEAN).normalize(value);
result = NormalizerFactory.getNormalizer(Type.BOOLEAN).normalize(value);
} else if (value instanceof String) {
return NormalizerFactory.getNormalizer(Type.TEXT).normalize(value);
result = NormalizerFactory.getNormalizer(Type.TEXT).normalize(value);
} else if (value instanceof Number) {
return NormalizerFactory.getNormalizer(Type.DECIMAL).normalize(value);
result = NormalizerFactory.getNormalizer(Type.DECIMAL).normalize(value);
} else if (value instanceof Collection collection) {
return normalizeCollection(collection);
result = normalizeCollection(collection);
}
if (result != null) {
return result;
}
throw new IllegalArgumentException(
"Invalid type '{" + value.getClass().getCanonicalName() + "}' of configuration value!");
"Invalid type '{%s}' of configuration value!".formatted(value.getClass().getCanonicalName()));
}

/**
Expand Down Expand Up @@ -242,7 +246,7 @@ public static Map<String, Object> normalizeTypes(Map<String, Object> configurati
* @param value the value to return as normalized type
* @return corresponding value as a valid type
*/
public static Object normalizeType(Object value) {
public static @Nullable Object normalizeType(Object value) {
return normalizeType(value, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.core.config.core.internal.normalization;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -21,12 +23,13 @@
* @author Simon Kaufmann - Initial contribution
* @author Thomas Höfer - renamed normalizer interface and added javadoc
*/
@NonNullByDefault
abstract class AbstractNormalizer implements Normalizer {

protected final Logger logger = LoggerFactory.getLogger(AbstractNormalizer.class);

@Override
public final Object normalize(Object value) {
public final @Nullable Object normalize(@Nullable Object value) {
if (value == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
package org.openhab.core.config.core.internal.normalization;

import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.config.core.ConfigDescriptionParameter;

/**
Expand All @@ -24,42 +27,38 @@
* @author Simon Kaufmann - Initial contribution
* @author Thomas Höfer - made class final and minor javadoc changes
*/
@NonNullByDefault
final class BooleanNormalizer extends AbstractNormalizer {
private static final Set<String> TRUES = Set.of("true", "yes", "on", "1");
private static final Set<String> FALSES = Set.of("false", "no", "off", "0");

@Override
public Object doNormalize(Object value) {
if (value instanceof Boolean) {
return value;
}
if (value instanceof Byte byteValue) {
return handleNumeric(byteValue.longValue());
}
if (value instanceof Integer integerValue) {
return handleNumeric(integerValue.longValue());
}
if (value instanceof Long longValue) {
return handleNumeric(longValue);
}
String s = value.toString();
if ("true".equalsIgnoreCase(s) || "yes".equalsIgnoreCase(s) || "on".equalsIgnoreCase(s)
|| "1".equalsIgnoreCase(s)) {
return true;
} else if ("false".equalsIgnoreCase(s) || "no".equalsIgnoreCase(s) || "off".equalsIgnoreCase(s)
|| "0".equalsIgnoreCase(s)) {
return false;
}
logger.trace("Class \"{}\" cannot be converted to boolean.", value.getClass().getName());
return value;
return switch (value) {
case Boolean bool -> bool;
case Byte byteValue -> handleNumeric(byteValue.longValue());
case Integer integerValue -> handleNumeric(integerValue.longValue());
case Long longValue -> handleNumeric(longValue);
default -> {
String s = value.toString().toLowerCase();
if (TRUES.contains(s)) {
yield true;
} else if (FALSES.contains(s)) {
yield false;
}
logger.trace("Class \"{}\" cannot be converted to boolean.", value.getClass().getName());
yield value;
}
};
}

private Object handleNumeric(long numeric) {
if (numeric == 1) {
return true;
} else if (numeric == 0) {
return false;
} else {
logger.trace("\"{}\" cannot be interpreted as a boolean.", numeric);
return numeric;
}
logger.trace("\"{}\" cannot be interpreted as a boolean.", numeric);
return numeric;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.math.BigDecimal;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.config.core.ConfigDescriptionParameter;

/**
Expand All @@ -23,41 +24,30 @@
* @author Simon Kaufmann - Initial contribution
* @author Thomas Höfer - made class final and minor javadoc changes
*/
@NonNullByDefault
final class DecimalNormalizer extends AbstractNormalizer {

@Override
public Object doNormalize(Object value) {
try {
if (value instanceof BigDecimal bigDecimalValue) {
return stripTrailingZeros(bigDecimalValue);
}
if (value instanceof String stringValue) {
return stripTrailingZeros(new BigDecimal(stringValue));
}
if (value instanceof Byte byteValue) {
return new BigDecimal(byteValue);
}
if (value instanceof Short shortValue) {
return new BigDecimal(shortValue);
}
if (value instanceof Integer integerValue) {
return new BigDecimal(integerValue);
}
if (value instanceof Long longValue) {
return new BigDecimal(longValue);
}
if (value instanceof Float floatValue) {
return new BigDecimal(floatValue.toString());
}
if (value instanceof Double doubleValue) {
return BigDecimal.valueOf(doubleValue);
}
return switch (value) {
case BigDecimal bigDecimalValue -> stripTrailingZeros(bigDecimalValue);
case String stringValue -> stripTrailingZeros(new BigDecimal(stringValue));
case Byte byteValue -> new BigDecimal(byteValue);
case Short shortValue -> new BigDecimal(shortValue);
case Integer integerValue -> new BigDecimal(integerValue);
case Long longValue -> new BigDecimal(longValue);
case Float floatValue -> new BigDecimal(floatValue.toString());
case Double doubleValue -> BigDecimal.valueOf(doubleValue);
default -> {
logger.trace("Class \"{}\" cannot be converted to a decimal number.", value.getClass().getName());
yield value;
}
};
} catch (ArithmeticException | NumberFormatException e) {
logger.trace("\"{}\" is not a valid decimal number.", value, e);
return value;
}
logger.trace("Class \"{}\" cannot be converted to a decimal number.", value.getClass().getName());
return value;
}

private BigDecimal stripTrailingZeros(BigDecimal value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,22 @@ final class IntNormalizer extends AbstractNormalizer {
@Override
public Object doNormalize(Object value) {
try {
if (value instanceof BigDecimal bigDecimalValue) {
return bigDecimalValue.setScale(0, RoundingMode.UNNECESSARY);
}
if (value instanceof Byte byteValue) {
return new BigDecimal(byteValue);
}
if (value instanceof Integer integerValue) {
return new BigDecimal(integerValue);
}
if (value instanceof Long longValue) {
return BigDecimal.valueOf(longValue);
}
if (value instanceof String stringValue) {
return new BigDecimal(stringValue).setScale(0, RoundingMode.UNNECESSARY);
}
if (value instanceof Float floatValue) {
return new BigDecimal(floatValue.toString()).setScale(0, RoundingMode.UNNECESSARY);
}
if (value instanceof Double doubleValue) {
return BigDecimal.valueOf(doubleValue).setScale(0, RoundingMode.UNNECESSARY);
}
return switch (value) {
case BigDecimal bigDecimalValue -> bigDecimalValue.setScale(0, RoundingMode.UNNECESSARY);
case Byte byteValue -> new BigDecimal(byteValue);
case Integer integerValue -> new BigDecimal(integerValue);
case Long longValue -> BigDecimal.valueOf(longValue);
case String stringValue -> new BigDecimal(stringValue).setScale(0, RoundingMode.UNNECESSARY);
case Float floatValue -> new BigDecimal(floatValue.toString()).setScale(0, RoundingMode.UNNECESSARY);
case Double doubleValue -> BigDecimal.valueOf(doubleValue).setScale(0, RoundingMode.UNNECESSARY);
default -> {
logger.trace("Class \"{}\" cannot be converted to an integer number.", value.getClass().getName());
yield value;
}
};
} catch (ArithmeticException | NumberFormatException e) {
logger.trace("\"{}\" is not a valid integer number.", value, e);
return value;
}
logger.trace("Class \"{}\" cannot be converted to an integer number.", value.getClass().getName());
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
*/
package org.openhab.core.config.core.internal.normalization;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.StreamSupport;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The normalizer for configuration parameters allowing multiple values. It converts all collections/arrays to a
Expand All @@ -22,6 +26,7 @@
* @author Simon Kaufmann - Initial contribution
* @author Thomas Höfer - made class final and minor javadoc changes
*/
@NonNullByDefault
final class ListNormalizer extends AbstractNormalizer {

private final Normalizer delegate;
Expand All @@ -30,43 +35,26 @@ final class ListNormalizer extends AbstractNormalizer {
this.delegate = delegate;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@SuppressWarnings({ "unchecked" })
@Override
public Object doNormalize(Object value) {
if (!isList(value)) {
List ret = new ArrayList(1);
ret.add(delegate.normalize(value));
return ret;
}
if (isArray(value)) {
List ret = new ArrayList(((Object[]) value).length);
for (Object object : ((Object[]) value)) {
ret.add(delegate.normalize(object));
}
return ret;
}
if (value instanceof List list) {
List ret = new ArrayList(list.size());
for (Object object : list) {
ret.add(delegate.normalize(object));
}
return ret;
}
if (value instanceof Iterable iterable) {
List ret = new ArrayList();
for (Object object : iterable) {
ret.add(delegate.normalize(object));
}
return ret;
return Set.of(value).stream().map(delegate::normalize).toList();
} else if (isArray(value)) {
return Arrays.stream((Object[]) value).map(delegate::normalize).toList();
} else if (value instanceof List list) {
return list.stream().map(delegate::normalize).toList();
} else if (value instanceof Iterable iterable) {
return StreamSupport.stream(iterable.spliterator(), false).map(delegate::normalize).toList();
}
return value;
}

static boolean isList(Object value) {
private static boolean isList(Object value) {
return isArray(value) || value instanceof Iterable;
}

private static boolean isArray(Object object) {
return object != null && object.getClass().isArray();
return object.getClass().isArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package org.openhab.core.config.core.internal.normalization;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.core.ConfigDescriptionParameter;

/**
Expand All @@ -22,6 +24,7 @@
* @author Simon Kaufmann - Initial contribution
* @author Thomas Höfer - renamed from INormalizer and minor javadoc changes
*/
@NonNullByDefault
public interface Normalizer {

/**
Expand All @@ -31,5 +34,6 @@ public interface Normalizer {
* @param value the object to be normalized
* @return the well-defined type or the given object, if it was not possible to convert it
*/
Object normalize(Object value);
@Nullable
Object normalize(@Nullable Object value);
}
Loading