TAMAYA-63: Aligned implementations for integral, float and BD, BI-Converters. Aligned tests for Java 7/8.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/c5415ce4 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/c5415ce4 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/c5415ce4 Branch: refs/heads/master Commit: c5415ce48f30fb871ec50f77b80ac43d2ed58420 Parents: 44b28c8 Author: anatole <[email protected]> Authored: Sat Jan 31 01:25:19 2015 +0100 Committer: anatole <[email protected]> Committed: Sat Jan 31 01:25:19 2015 +0100 ---------------------------------------------------------------------- .../core/internal/PropertyConverterManager.java | 141 ++++++--------- .../converters/BigDecimalConverter.java | 29 ++- .../converters/BigIntegerConverter.java | 60 ++++++- .../internal/converters/BooleanConverter.java | 1 - .../core/internal/converters/ByteConverter.java | 13 +- .../internal/converters/DoubleConverter.java | 42 ++++- .../internal/converters/FloatConverter.java | 47 ++++- .../internal/converters/IntegerConverter.java | 13 +- .../core/internal/converters/LongConverter.java | 13 +- .../internal/converters/ShortConverter.java | 12 +- .../internal/PropertyConverterManagerTest.java | 75 ++++++++ .../converters/BigDecimalConverterTest.java | 103 +++++++++++ .../internal/converters/ByteConverterTest.java | 29 +++ .../ConverterTestsPropertySource.java | 111 +++++++++++- .../converters/DoubleConverterTest.java | 177 ++++++++++++++++++ .../internal/converters/FloatConverterTest.java | 178 +++++++++++++++++++ .../converters/IntegerConverterTest.java | 111 ++++++++++++ .../internal/converters/LongConverterTest.java | 111 ++++++++++++ .../internal/converters/ShortConverterTest.java | 111 ++++++++++++ .../java/org/apache/tamaya/Configuration.java | 15 ++ .../converters/BigDecimalConverter.java | 29 ++- .../converters/BigIntegerConverter.java | 60 ++++++- .../internal/converters/BooleanConverter.java | 3 +- .../core/internal/converters/ByteConverter.java | 12 +- .../internal/converters/DoubleConverter.java | 41 ++++- .../internal/converters/FloatConverter.java | 47 ++++- .../internal/converters/IntegerConverter.java | 12 +- .../internal/converters/LocalTimeConverter.java | 6 + .../core/internal/converters/LongConverter.java | 12 +- .../internal/converters/ShortConverter.java | 12 +- .../converters/BigDecimalConverterTest.java | 104 +++++++++++ .../internal/converters/ByteConverterTest.java | 26 +++ .../ConverterTestsPropertySource.java | 119 +++++++++++-- .../converters/DoubleConverterTest.java | 176 ++++++++++++++++++ .../internal/converters/FloatConverterTest.java | 178 +++++++++++++++++++ .../converters/IntegerConverterTest.java | 113 ++++++++++++ .../internal/converters/LongConverterTest.java | 113 ++++++++++++ .../internal/converters/ShortConverterTest.java | 113 ++++++++++++ 38 files changed, 2430 insertions(+), 138 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java ---------------------------------------------------------------------- diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java b/java7/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java index 3fa97b9..dccd2dd 100644 --- a/java7/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java +++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/PropertyConverterManager.java @@ -18,19 +18,11 @@ */ package org.apache.tamaya.core.internal; -import org.apache.tamaya.ConfigException; -import org.apache.tamaya.TypeLiteral; -import org.apache.tamaya.core.internal.converters.EnumConverter; -import org.apache.tamaya.PropertyConverter; -import org.apache.tamaya.spi.ServiceContextManager; - import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import java.security.AccessController; -import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -39,29 +31,27 @@ import java.util.Map; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.logging.Level; +import java.util.concurrent.locks.StampedLock; import java.util.logging.Logger; +import org.apache.tamaya.ConfigException; +import org.apache.tamaya.TypeLiteral; +import org.apache.tamaya.core.internal.converters.EnumConverter; +import org.apache.tamaya.PropertyConverter; +import org.apache.tamaya.spi.ServiceContextManager; + /** * Manager that deals with {@link org.apache.tamaya.PropertyConverter} instances. * This class is thread-safe. */ public class PropertyConverterManager { - /** - * The logger used. - */ + /** The logger used. */ private static final Logger LOG = Logger.getLogger(PropertyConverterManager.class.getName()); - /** - * The registered converters. - */ + /** The registered converters. */ private Map<TypeLiteral<?>, List<PropertyConverter<?>>> converters = new ConcurrentHashMap<>(); - /** - * The lock used. - */ - private ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); + /** The lock used. */ + private StampedLock lock = new StampedLock(); private static final String CHAR_NULL_ERROR = "Cannot convert null property"; - /** * Constructor. */ @@ -73,11 +63,11 @@ public class PropertyConverterManager { * Registers the default converters provided out of the box. */ protected void initConverters() { - for (PropertyConverter conv : ServiceContextManager.getServiceContext().getServices(PropertyConverter.class)) { + for(PropertyConverter conv: ServiceContextManager.getServiceContext().getServices(PropertyConverter.class)){ ParameterizedType type = ReflectionUtil.getParametrizedType(conv.getClass()); - if (type == null || type.getActualTypeArguments().length == 0) { + if(type==null || type.getActualTypeArguments().length==0){ LOG.warning("Failed to register PropertyConverter, no generic type information available: " + - conv.getClass().getName()); + conv.getClass().getName()); } else { Type targetType = type.getActualTypeArguments()[0]; register(TypeLiteral.of(targetType), conv); @@ -90,11 +80,10 @@ public class PropertyConverterManager { * * @param targetType the target type, not null. * @param converter the converter, not null. - * @param <T> the type. */ - public <T> void register(TypeLiteral<T> targetType, PropertyConverter<T> converter) { + public void register(TypeLiteral<?> targetType, PropertyConverter<?> converter) { Objects.requireNonNull(converter); - Lock writeLock = lock.writeLock(); + Lock writeLock = lock.asWriteLock(); try { writeLock.lock(); List converters = List.class.cast(this.converters.get(targetType)); @@ -115,16 +104,6 @@ public class PropertyConverterManager { * @param targetType the target type, not null. * @return true, if a converter for the given type is registered, or a default one can be created. */ - public boolean isTargetTypeSupported(Class<?> targetType) { - return isTargetTypeSupported(TypeLiteral.of(targetType)); - } - - /** - * Allows to evaluate if a given target type is supported. - * - * @param targetType the target type, not null. - * @return true, if a converter for the given type is registered, or a default one can be created. - */ public boolean isTargetTypeSupported(TypeLiteral<?> targetType) { return converters.containsKey(targetType) || createDefaultPropertyConverter(targetType) != null; @@ -139,7 +118,7 @@ public class PropertyConverterManager { * @see #createDefaultPropertyConverter(org.apache.tamaya.TypeLiteral) */ public Map<TypeLiteral<?>, List<PropertyConverter<?>>> getPropertyConverters() { - Lock readLock = lock.readLock(); + Lock readLock = lock.asReadLock(); try { readLock.lock(); return new HashMap<>(this.converters); @@ -159,7 +138,7 @@ public class PropertyConverterManager { * @see #createDefaultPropertyConverter(org.apache.tamaya.TypeLiteral) */ public <T> List<PropertyConverter<T>> getPropertyConverters(TypeLiteral<T> targetType) { - Lock readLock = lock.readLock(); + Lock readLock = lock.asReadLock(); List<PropertyConverter<T>> converters; try { readLock.lock(); @@ -174,6 +153,7 @@ public class PropertyConverterManager { if (defaultConverter != null) { register(targetType, defaultConverter); try { + readLock.lock(); converters = List.class.cast(this.converters.get(targetType)); } finally { readLock.unlock(); @@ -193,20 +173,48 @@ public class PropertyConverterManager { * @return a new converter, or null. */ protected <T> PropertyConverter<T> createDefaultPropertyConverter(final TypeLiteral<T> targetType) { - if (Enum.class.isAssignableFrom(targetType.getRawType())) { + if(Enum.class.isAssignableFrom(targetType.getRawType())){ return new EnumConverter<T>(targetType.getRawType()); } PropertyConverter<T> converter = null; final Method factoryMethod = getFactoryMethod(targetType.getRawType(), "of", "valueOf", "instanceOf", "getInstance", "from", "fromString", "parse"); if (factoryMethod != null) { - converter = new ViaMethodPropertyConverter<>(factoryMethod, targetType.getRawType()); + converter = new PropertyConverter<T>() { + @Override + public T convert(String value) { + try { + if (!Modifier.isStatic(factoryMethod.getModifiers())) { + throw new ConfigException(factoryMethod.toGenericString() + + " is not a static method. Only static " + + "methods can be used as factory methods."); + } + + factoryMethod.setAccessible(true); + + Object invoke = factoryMethod.invoke(null, value); + return targetType.getRawType().cast(invoke); + } catch (Exception e) { + throw new ConfigException("Failed to decode '" + value + "'", e); + } + } + }; } if (converter == null) { try { final Constructor<T> constr = targetType.getRawType().getDeclaredConstructor(String.class); - converter = new ViaConstructorPropertyConverter<>(constr); + converter = new PropertyConverter<T>() { + @Override + public T convert(String value) { + try { + constr.setAccessible(true); + return constr.newInstance(value); + } catch (Exception e) { + throw new ConfigException("Failed to decode '" + value + "'", e); + } + } + }; } catch (Exception e) { - LOG.log(Level.FINEST, "Failed to construct instance of type: " + targetType.getRawType().getName(), e); + LOG.finest("Failed to construct instance of type: " + targetType.getRawType().getName()+": " + e); } } return converter; @@ -226,53 +234,10 @@ public class PropertyConverterManager { m = type.getDeclaredMethod(name, String.class); return m; } catch (NoSuchMethodException | RuntimeException e) { - LOG.log(Level.FINEST, "No such factory method found on type: " + type.getName() + ", methodName: " + name, e); + LOG.finest("No such factory method found on type: " + type.getName()+", methodName: " + name); } } return null; } - private static class ViaMethodPropertyConverter<T> implements PropertyConverter<T> { - private final Method factoryMethod; - private final Class<T> targetType; - - public ViaMethodPropertyConverter(Method factoryMethod, Class<T> targetType) { - this.factoryMethod = factoryMethod; - this.targetType = targetType; - } - - public T convert(final String value) { - try { - AccessController.doPrivileged( - new PrivilegedAction() { - @Override - public Object run() { - factoryMethod.setAccessible(true); - return null; - } - }); - return targetType.cast(factoryMethod.invoke(value)); - } catch (IllegalAccessException | InvocationTargetException e) { - throw new ConfigException("Failed to decode '" + value + "'", e); - } - } - } - - private static class ViaConstructorPropertyConverter<T> implements PropertyConverter<T> { - private final Constructor<T> constr; - - public ViaConstructorPropertyConverter(Constructor<T> constr) { - this.constr = constr; - } - - @Override - public T convert(String value) { - try { - constr.setAccessible(true); - return constr.newInstance(value); - } catch (Exception e) { - throw new ConfigException("Failed to decode '" + value + "'", e); - } - } - } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java ---------------------------------------------------------------------- diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java index 87f3d14..4b20071 100644 --- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java +++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java @@ -21,17 +21,40 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.PropertyConverter; import java.math.BigDecimal; +import java.math.BigInteger; import java.util.Objects; +import java.util.logging.Logger; /** - * Converter, converting from String to BigDecimal. - * //X TODO not good enough as this is Locale dependent! + * Converter, converting from String to BigDecimal, the supported format is one of the following: + * <ul> + * <li>232573527352.76352753</li> + * <li>-23257352.735276352753</li> + * <li>-0xFFFFFF (integral numbers only)</li> + * <li>-0XFFFFAC (integral numbers only)</li> + * <li>0xFFFFFF (integral numbers only)</li> + * <li>0XFFFFAC (integral numbers only)</li> + * </ul> */ public class BigDecimalConverter implements PropertyConverter<BigDecimal>{ + /** The logger. */ + private static final Logger LOG = Logger.getLogger(BigDecimalConverter.class.getName()); + /** Converter to be used if the format is not directly supported by BigDecimal, e.g. for integral hex values. */ + private BigIntegerConverter integerConverter = new BigIntegerConverter(); @Override public BigDecimal convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - return new BigDecimal(trimmed); + try{ + return new BigDecimal(trimmed); + } catch(Exception e){ + LOG.finest("Parsing BigDecimal failed, trying BigInteger for: " + value); + BigInteger bigInt = integerConverter.convert(trimmed); + if(bigInt!=null){ + return new BigDecimal(bigInt); + } + LOG.finest("Failed to parse BigDecimal from: " + value); + return null; + } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java ---------------------------------------------------------------------- diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java index 4db9bcd..de95d8a 100644 --- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java +++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java @@ -22,17 +22,71 @@ import org.apache.tamaya.PropertyConverter; import java.math.BigInteger; import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; /** - * Converter, converting from String to BigInteger. - * //X TODO not good enough as this is Locale dependent! + * Converter, converting from String to BigInteger, the supported format is one of the following: + * <ul> + * <li>0xFFFFFF</li> + * <li>0XFFFFAC</li> + * <li>23257352735276352753</li> + * <li>-0xFFFFFF</li> + * <li>-0XFFFFAC</li> + * <li>-23257352735276352753</li> + * </ul> */ public class BigIntegerConverter implements PropertyConverter<BigInteger>{ + /** The logger. */ + private static final Logger LOG = Logger.getLogger(BigIntegerConverter.class.getName()); + /** Converter used to decode hex, octal values. */ + private ByteConverter byteConverter = new ByteConverter(); + @Override public BigInteger convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - return new BigInteger(trimmed); + if(trimmed.startsWith("0x") || trimmed.startsWith("0X")){ + LOG.finer("Parsing Hex value to BigInteger: " + value); + trimmed = trimmed.substring(2); + StringBuilder decimal = new StringBuilder(); + for(int offset = 0;offset < trimmed.length();offset+=2){ + if(offset==trimmed.length()-1){ + LOG.info("Invalid Hex-Byte-String: " + value); + return null; + } + byte val = byteConverter.convert("0x" + trimmed.substring(offset, offset + 2)); + if(val<10){ + decimal.append('0').append(val); + } else{ + decimal.append(val); + } + } + return new BigInteger(decimal.toString()); + } else if(trimmed.startsWith("-0x") || trimmed.startsWith("-0X")){ + LOG.finer("Parsing Hex value to BigInteger: " + value); + trimmed = trimmed.substring(3); + StringBuilder decimal = new StringBuilder(); + for(int offset = 0;offset < trimmed.length();offset+=2){ + if(offset==trimmed.length()-1){ + LOG.info("Invalid Hex-Byte-String: " + trimmed); + return null; + } + byte val = byteConverter.convert("0x" + trimmed.substring(offset, offset + 2)); + if(val<10){ + decimal.append('0').append(val); + } else{ + decimal.append(val); + } + } + return new BigInteger('-' + decimal.toString()); + } + try{ + return new BigInteger(trimmed); + } catch(Exception e){ + LOG.log(Level.FINEST, "Failed to parse BigInteger from: " + value, e); + return null; + } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java ---------------------------------------------------------------------- diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java index f07a8ee..cd48c97 100644 --- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java +++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java @@ -51,7 +51,6 @@ public class BooleanConverter implements PropertyConverter<Boolean> { default: LOG.warning("Unknown boolean value encountered: " + value); } - return null; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java ---------------------------------------------------------------------- diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java index 191e9e5..d280746 100644 --- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java +++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java @@ -19,6 +19,8 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.PropertyConverter; + +import java.util.Locale; import java.util.Objects; /** @@ -29,6 +31,15 @@ public class ByteConverter implements PropertyConverter<Byte>{ @Override public Byte convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - return Byte.decode(trimmed); + switch(trimmed.toUpperCase(Locale.ENGLISH)){ + case "MIN_VALUE": + case "MIN": + return Byte.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Byte.MAX_VALUE; + default: + return Byte.decode(trimmed); + } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java ---------------------------------------------------------------------- diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java index 1814cba..eb74278 100644 --- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java +++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/DoubleConverter.java @@ -20,17 +20,53 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.PropertyConverter; +import java.util.Locale; import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; /** - * Converter, converting from String to Double. + * Converter, converting from String to Double, using the Java number syntax: + * (-)?[0-9]*\.[0-9]*. In case of error the value given also is tried being parsed as integral number using + * {@link LongConverter}. */ public class DoubleConverter implements PropertyConverter<Double>{ + /** The logger. */ + private static final Logger LOG = Logger.getLogger(DoubleConverter.class.getName()); + /** The converter used, when floating point parse failed. */ + private LongConverter integerConverter = new LongConverter(); @Override public Double convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - //X TODO not good enough as this is Locale dependent! - return Double.valueOf(trimmed); + switch(trimmed.toUpperCase(Locale.ENGLISH)){ + case "POSITIVE_INFINITY": + return Double.POSITIVE_INFINITY; + case "NEGATIVE_INFINITY": + return Double.NEGATIVE_INFINITY; + case "NAN": + return Double.NaN; + case "MIN_VALUE": + case "MIN": + return Double.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Double.MAX_VALUE; + default: + try { + return Double.valueOf(trimmed); + } catch(Exception e){ + // OK perhaps we have an integral number that must be converted to the double type... + LOG.log(Level.FINER, "Parsing of double as floating number failed, trying parsing integral" + + " number instead...", e); + } + try{ + return integerConverter.convert(trimmed).doubleValue(); + } catch(Exception e){ + LOG.log(Level.INFO, "Unexpected error from LongConverter for " + trimmed, e); + return null; + } + } + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java ---------------------------------------------------------------------- diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java index 955197b..918c9a9 100644 --- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java +++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/FloatConverter.java @@ -20,17 +20,56 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.PropertyConverter; +import java.util.Locale; import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; /** - * Converter, converting from String to Float. + * Converter, converting from String to Float, using the Java number syntax: + * (-)?[0-9]*\.[0-9]*. In case of error the value given also is tried being parsed as integral number using + * {@link LongConverter}. */ -public class FloatConverter implements PropertyConverter<Float>{ +public class FloatConverter implements PropertyConverter<Float> { + /** + * The logger. + */ + private static final Logger LOG = Logger.getLogger(DoubleConverter.class.getName()); + /** + * The converter used, when floating point parse failed. + */ + private IntegerConverter integerConverter = new IntegerConverter(); @Override public Float convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - //X TODO not good enough as this is Locale dependent! - return Float.valueOf(trimmed); + switch(trimmed.toUpperCase(Locale.ENGLISH)){ + case "POSITIVE_INFINITY": + return Float.POSITIVE_INFINITY; + case "NEGATIVE_INFINITY": + return Float.NEGATIVE_INFINITY; + case "NAN": + return Float.NaN; + case "MIN_VALUE": + case "MIN": + return Float.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Float.MAX_VALUE; + default: + try { + return Float.valueOf(trimmed); + } catch(Exception e){ + // OK perhaps we have an integral number that must be converted to the double type... + LOG.log(Level.FINER, "Parsing of double as floating number failed, trying parsing integral" + + " number instead...", e); + } + try{ + return integerConverter.convert(trimmed).floatValue(); + } catch(Exception e){ + LOG.log(Level.INFO, "Unexpected error from LongConverter for " + trimmed, e); + return null; + } + } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java ---------------------------------------------------------------------- diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java index 923b132..94d3427 100644 --- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java +++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/IntegerConverter.java @@ -20,6 +20,7 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.PropertyConverter; +import java.util.Locale; import java.util.Objects; /** @@ -30,6 +31,16 @@ public class IntegerConverter implements PropertyConverter<Integer>{ @Override public Integer convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - return Integer.decode(trimmed); + switch(trimmed.toUpperCase(Locale.ENGLISH)){ + case "MIN_VALUE": + case "MIN": + return Integer.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Integer.MAX_VALUE; + default: + return Integer.decode(trimmed); + } + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java ---------------------------------------------------------------------- diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java index 67434d2..673076f 100644 --- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java +++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/LongConverter.java @@ -20,6 +20,7 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.PropertyConverter; +import java.util.Locale; import java.util.Objects; /** @@ -30,6 +31,16 @@ public class LongConverter implements PropertyConverter<Long>{ @Override public Long convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - return Long.decode(trimmed); + switch(trimmed.toUpperCase(Locale.ENGLISH)){ + case "MIN_VALUE": + case "MIN": + return Long.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Long.MAX_VALUE; + default: + return Long.decode(trimmed); + } + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java ---------------------------------------------------------------------- diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java index e41c1d6..f9ae732 100644 --- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java +++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/ShortConverter.java @@ -20,6 +20,7 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.PropertyConverter; +import java.util.Locale; import java.util.Objects; /** @@ -30,6 +31,15 @@ public class ShortConverter implements PropertyConverter<Short>{ @Override public Short convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - return Short.decode(trimmed); + switch(trimmed.toUpperCase(Locale.ENGLISH)){ + case "MIN_VALUE": + case "MIN": + return Short.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Short.MAX_VALUE; + default: + return Short.decode(trimmed); + } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/test/java/org/apache/tamaya/core/internal/PropertyConverterManagerTest.java ---------------------------------------------------------------------- diff --git a/java7/core/src/test/java/org/apache/tamaya/core/internal/PropertyConverterManagerTest.java b/java7/core/src/test/java/org/apache/tamaya/core/internal/PropertyConverterManagerTest.java new file mode 100644 index 0000000..d53d98b --- /dev/null +++ b/java7/core/src/test/java/org/apache/tamaya/core/internal/PropertyConverterManagerTest.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.core.internal; + + +import org.apache.tamaya.PropertyConverter; +import org.apache.tamaya.TypeLiteral; +import org.junit.Test; + +import java.util.List; + +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; + +public class PropertyConverterManagerTest { + @Test + public void customTypeWithFactoryMethodOfIsRecognizedAsSupported() { + PropertyConverterManager manager = new PropertyConverterManager(); + + assertThat(manager.isTargetTypeSupported(TypeLiteral.of(MyType.class)), + is(true)); + } + + @Test + public void factoryMethodOfIsUsedAsConverter() { + PropertyConverterManager manager = new PropertyConverterManager(); + + List<PropertyConverter<MyType>> converters = manager.getPropertyConverters(TypeLiteral.of(MyType.class)); + + assertThat(converters, hasSize(1)); + + PropertyConverter<MyType> converter = converters.get(0); + + Object result = converter.convert("IN"); + + assertThat(result, notNullValue()); + assertThat(result, instanceOf(MyType.class)); + assertThat(((MyType)result).getValue(), equalTo("IN")); + } + + public static class MyType { + private String typeValue; + + private MyType(String value) { + typeValue = value; + } + + public static MyType of(String source) { + return new MyType(source); + } + + public String getValue() { + return typeValue; + } + + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverterTest.java ---------------------------------------------------------------------- diff --git a/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverterTest.java b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverterTest.java new file mode 100644 index 0000000..9c71688 --- /dev/null +++ b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/BigDecimalConverterTest.java @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.core.internal.converters; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.junit.Test; + +import java.math.BigDecimal; + +import static org.junit.Assert.*; + +/** + * Tests the default converter for bytes. + */ +public class BigDecimalConverterTest { + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_BigDecimal_Decimal() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + BigDecimal valueRead = config.get("tests.converter.bd.decimal", BigDecimal.class); + assertTrue(valueRead != null); + assertEquals(valueRead, new BigDecimal(101)); + } + + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_BigDecimal_Hex() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + BigDecimal valueRead = config.get("tests.converter.bd.hex.lowerX", BigDecimal.class); + assertTrue(valueRead != null); + assertEquals(valueRead, new BigDecimal("47")); + valueRead = config.get("tests.converter.bd.hex.upperX", BigDecimal.class); + assertTrue(valueRead != null); + assertEquals(valueRead, new BigDecimal("63")); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_NotPresent() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + BigDecimal valueRead = config.get("tests.converter.bd.foo", BigDecimal.class); + assertFalse(valueRead != null); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_BigDecimal_BigValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + BigDecimal valueRead = config.get("tests.converter.bd.big", BigDecimal.class); + assertTrue(valueRead != null); + assertEquals(new BigDecimal("101666666666666662333337263723628763821638923628193612983618293628763"), + valueRead); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_BigDecimal_BigFloatValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + BigDecimal valueRead = config.get("tests.converter.bd.bigFloat", BigDecimal.class); + assertTrue(valueRead != null); + assertEquals(new BigDecimal("1016666666666666623333372637236287638216389293628763.1016666666666666623333372" + + "63723628763821638923628193612983618293628763"), valueRead); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java ---------------------------------------------------------------------- diff --git a/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java index 70f54c2..b0b6cb8 100644 --- a/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java +++ b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ByteConverterTest.java @@ -22,7 +22,10 @@ import org.apache.tamaya.Configuration; import org.apache.tamaya.ConfigurationProvider; import org.junit.Test; +import java.util.Optional; + import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; /** * Tests the default converter for bytes. @@ -52,4 +55,30 @@ public class ByteConverterTest { valueRead = config.get("tests.converter.byte.foo", Byte.class); assertNull(valueRead); } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Byte_MinValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Byte valueRead = config.get("tests.converter.byte.min", Byte.class); + assertTrue(valueRead!=null); + assertEquals(Byte.MIN_VALUE, valueRead.byteValue()); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Byte_MaxValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Byte valueRead = config.get("tests.converter.byte.max", Byte.class); + assertTrue(valueRead!=null); + assertEquals(Byte.MAX_VALUE, valueRead.byteValue()); + } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java ---------------------------------------------------------------------- diff --git a/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java index 918b97f..a2c61a8 100644 --- a/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java +++ b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ConverterTestsPropertySource.java @@ -33,7 +33,7 @@ public class ConverterTestsPropertySource implements PropertySource{ } @Override - public String getName() { + public String getName(){ return getClass().getName(); } @@ -49,6 +49,10 @@ public class ConverterTestsPropertySource implements PropertySource{ return "0x2F"; case "tests.converter.byte.hex.upperX": return "0X3F"; + case "tests.converter.byte.min": + return "min"; + case "tests.converter.byte.max": + return "MAX_Value"; // Boolean case "tests.converter.boolean.y1": return "y"; @@ -90,6 +94,7 @@ public class ConverterTestsPropertySource implements PropertySource{ return "f"; case "tests.converter.boolean.f2": return "F"; + // Character case "tests.converter.char.f": return "f"; case "tests.converter.char.d": @@ -102,6 +107,7 @@ public class ConverterTestsPropertySource implements PropertySource{ return " f "; case "tests.converter.char.f-numeric": return "101"; + // currency case "tests.converter.currency.code1": return "CHF"; case "tests.converter.currency.code2": @@ -128,11 +134,112 @@ public class ConverterTestsPropertySource implements PropertySource{ return "DE "; case "tests.converter.currency.code-locale4": return " DE "; + //double + case "tests.converter.double.decimal": + return "1.23456789"; + case "tests.converter.double.decimalNegative": + return "-1.23456789"; + case "tests.converter.double.integer": + return " 100"; + case "tests.converter.double.hex1": + return " 0XFF"; + case "tests.converter.double.hex2": + return "-0xFF "; + case "tests.converter.double.hex3": + return "#FF"; + case "tests.converter.double.octal": + return "0013"; + case "tests.converter.double.min": + return "MIN_Value"; + case "tests.converter.double.max": + return "max"; + case "tests.converter.double.nan": + return "NAN"; + case "tests.converter.double.pi": + return "positive_infinity"; + case "tests.converter.double.ni": + return "Negative_Infinity"; + //float + case "tests.converter.float.decimal": + return "1.23456789"; + case "tests.converter.float.decimalNegative": + return "-1.23456789"; + case "tests.converter.float.integer": + return " 100"; + case "tests.converter.float.hex1": + return " 0XFF"; + case "tests.converter.float.hex2": + return "-0xFF "; + case "tests.converter.float.hex3": + return "#FF"; + case "tests.converter.float.octal": + return "0013"; + case "tests.converter.float.min": + return "MIN_Value"; + case "tests.converter.float.max": + return "max"; + case "tests.converter.float.nan": + return "NAN"; + case "tests.converter.float.pi": + return "positive_infinity"; + case "tests.converter.float.ni": + return "Negative_Infinity"; + // Integer + case "tests.converter.integer.decimal": + return "101"; + case "tests.converter.integer.octal": + return "02"; + case "tests.converter.integer.hex.lowerX": + return "0x2F"; + case "tests.converter.integer.hex.upperX": + return "0X3F"; + case "tests.converter.integer.min": + return "min"; + case "tests.converter.integer.max": + return "MAX_Value"; + // Long + case "tests.converter.long.decimal": + return "101"; + case "tests.converter.long.octal": + return "02"; + case "tests.converter.long.hex.lowerX": + return "0x2F"; + case "tests.converter.long.hex.upperX": + return "0X3F"; + case "tests.converter.long.min": + return "min"; + case "tests.converter.long.max": + return "MAX_Value"; + // Short + case "tests.converter.short.decimal": + return "101"; + case "tests.converter.short.octal": + return "02"; + case "tests.converter.short.hex.lowerX": + return "0x2F"; + case "tests.converter.short.hex.upperX": + return "0X3F"; + case "tests.converter.short.min": + return "min"; + case "tests.converter.short.max": + return "MAX_Value"; + // BigDecimal + case "tests.converter.bd.decimal": + return "101"; + case "tests.converter.bd.float": + return "101.36438746"; + case "tests.converter.bd.big": + return "101666666666666662333337263723628763821638923628193612983618293628763"; + case "tests.converter.bd.bigFloat": + return "1016666666666666623333372637236287638216389293628763.101666666666666662333337263723628763821638923628193612983618293628763"; + case "tests.converter.bd.hex.lowerX": + return "0x2F"; + case "tests.converter.bd.hex.upperX": + return "0X3F"; } return null; } - @Override public Map<String, String> getProperties() { return Collections.emptyMap(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/DoubleConverterTest.java ---------------------------------------------------------------------- diff --git a/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/DoubleConverterTest.java b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/DoubleConverterTest.java new file mode 100644 index 0000000..9c585f8 --- /dev/null +++ b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/DoubleConverterTest.java @@ -0,0 +1,177 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.core.internal.converters; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.junit.Test; + +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Tests the default converter for bytes. + */ +public class DoubleConverterTest { + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Double_Decimal() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Double valueRead = config.get("tests.converter.double.decimal", Double.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.doubleValue(), 1.23456789, 0.0d); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Double_DecimalNegative() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Double valueRead = config.get("tests.converter.double.decimalNegative", Double.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.doubleValue(), -1.23456789, 0.0d); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Double_Integer() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Double valueRead = config.get("tests.converter.double.integer", Double.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.doubleValue(),100d, 0.0d); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Double_Hex1() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Double valueRead = config.get("tests.converter.double.hex1", Double.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.doubleValue(),255d, 0.0d); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Double_Hex2() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Double valueRead = config.get("tests.converter.double.hex2", Double.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.doubleValue(),-255d, 0.0d); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Double_Hex3() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Double valueRead = config.get("tests.converter.double.hex3", Double.class); + assertTrue(valueRead!=null); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Double_MinValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Double valueRead = config.get("tests.converter.double.min", Double.class); + assertTrue(valueRead!=null); + assertEquals(Double.MIN_VALUE, valueRead.doubleValue(),0.0d); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Double_MaxValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Double valueRead = config.get("tests.converter.double.max", Double.class); + assertTrue(valueRead!=null); + assertEquals(Double.MAX_VALUE, valueRead.doubleValue(),0.0d); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Double_NaNValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Double valueRead = config.get("tests.converter.double.nan", Double.class); + assertTrue(valueRead!=null); + assertEquals(Double.NaN, valueRead.doubleValue(),0.0d); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Double_PositiveInfinityValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Double valueRead = config.get("tests.converter.double.pi", Double.class); + assertTrue(valueRead!=null); + assertEquals(Double.POSITIVE_INFINITY, valueRead.doubleValue(),0.0d); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Double_NegativeInfinityValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Double valueRead = config.get("tests.converter.double.ni", Double.class); + assertTrue(valueRead!=null); + assertEquals(Double.NEGATIVE_INFINITY, valueRead.doubleValue(),0.0d); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/FloatConverterTest.java ---------------------------------------------------------------------- diff --git a/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/FloatConverterTest.java b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/FloatConverterTest.java new file mode 100644 index 0000000..b8ae8b5 --- /dev/null +++ b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/FloatConverterTest.java @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.core.internal.converters; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.junit.Test; + +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Tests the default converter for bytes. + */ +public class FloatConverterTest { + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Float_Decimal() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Float valueRead = config.get("tests.converter.float.decimal", Float.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.floatValue(), 1.23456789f, 0.0f); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Float_DecimalNegative() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Float valueRead = config.get("tests.converter.float.decimalNegative", Float.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.floatValue(), -1.23456789f, 0.0f); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Float_Integer() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Float valueRead = config.get("tests.converter.float.integer", Float.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.floatValue(),100f, 0.0f); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Float_Hex1() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Float valueRead = config.get("tests.converter.float.hex1", Float.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.floatValue(),255f, 0.0f); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Float_Hex2() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Float valueRead = config.get("tests.converter.float.hex2", Float.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.floatValue(),-255f, 0.0f); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Float_Hex3() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Float valueRead = config.get("tests.converter.float.hex3", Float.class); + assertTrue(valueRead!=null); + assertEquals(valueRead.floatValue(),255f, 0.0f); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Float_MinValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Float valueRead = config.get("tests.converter.float.min", Float.class); + assertTrue(valueRead!=null); + assertEquals(Float.MIN_VALUE, valueRead.floatValue(),0.0f); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Float_MaxValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Float valueRead = config.get("tests.converter.float.max", Float.class); + assertTrue(valueRead!=null); + assertEquals(Float.MAX_VALUE, valueRead.floatValue(),0.0f); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Float_NaNValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Float valueRead = config.get("tests.converter.float.nan", Float.class); + assertTrue(valueRead!=null); + assertEquals(Float.NaN, valueRead.floatValue(),0.0f); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Float_PositiveInfinityValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Float valueRead = config.get("tests.converter.float.pi", Float.class); + assertTrue(valueRead!=null); + assertEquals(Float.POSITIVE_INFINITY, valueRead.floatValue(),0.0f); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Float_NegativeInfinityValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Float valueRead = config.get("tests.converter.float.ni", Float.class); + assertTrue(valueRead!=null); + assertEquals(Float.NEGATIVE_INFINITY, valueRead.floatValue(),0.0f); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/IntegerConverterTest.java ---------------------------------------------------------------------- diff --git a/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/IntegerConverterTest.java b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/IntegerConverterTest.java new file mode 100644 index 0000000..03b0f12 --- /dev/null +++ b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/IntegerConverterTest.java @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.core.internal.converters; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the default converter for Integers. + */ +public class IntegerConverterTest { + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Integer_Decimal() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Integer valueRead = config.get("tests.converter.integer.decimal", Integer.class); + assertTrue(valueRead != null); + assertEquals(valueRead.intValue(), 101); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Integer_Octal() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Integer valueRead = config.get("tests.converter.integer.octal", Integer.class); + assertTrue(valueRead != null); + assertEquals(valueRead.intValue(), Integer.decode("02").intValue()); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Integer_Hex() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Integer valueRead = config.get("tests.converter.integer.hex.lowerX", Integer.class); + assertTrue(valueRead != null); + assertEquals(valueRead.intValue(), Integer.decode("0x2F").intValue()); + valueRead = config.get("tests.converter.integer.hex.upperX", Integer.class); + assertTrue(valueRead != null); + assertEquals(valueRead.intValue(), Integer.decode("0X3F").intValue()); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_NotPresent() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Integer valueRead = config.get("tests.converter.integer.foo", Integer.class); + assertFalse(valueRead != null); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Integer_MinValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Integer valueRead = config.get("tests.converter.integer.min", Integer.class); + assertTrue(valueRead != null); + assertEquals(Integer.MIN_VALUE, valueRead.intValue()); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Integer_MaxValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Integer valueRead = config.get("tests.converter.integer.max", Integer.class); + assertTrue(valueRead != null); + assertEquals(Integer.MAX_VALUE, valueRead.intValue()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/LongConverterTest.java ---------------------------------------------------------------------- diff --git a/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/LongConverterTest.java b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/LongConverterTest.java new file mode 100644 index 0000000..0df6b09 --- /dev/null +++ b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/LongConverterTest.java @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.core.internal.converters; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the default converter for Longs. + */ +public class LongConverterTest { + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Long_Decimal() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Long valueRead = config.get("tests.converter.long.decimal", Long.class); + assertTrue(valueRead != null); + assertEquals(valueRead.intValue(), 101); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Long_Octal() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Long valueRead = config.get("tests.converter.long.octal", Long.class); + assertTrue(valueRead != null); + assertEquals(valueRead.intValue(), Long.decode("02").intValue()); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Long_Hex() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Long valueRead = config.get("tests.converter.long.hex.lowerX", Long.class); + assertTrue(valueRead != null); + assertEquals(valueRead.intValue(), Long.decode("0x2F").intValue()); + valueRead = config.get("tests.converter.long.hex.upperX", Long.class); + assertTrue(valueRead != null); + assertEquals(valueRead.intValue(), Long.decode("0X3F").intValue()); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_NotPresent() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Long valueRead = config.get("tests.converter.long.foo", Long.class); + assertFalse(valueRead != null); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Long_MinValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Long valueRead = config.get("tests.converter.long.min", Long.class); + assertTrue(valueRead != null); + assertEquals(Long.MIN_VALUE, valueRead.longValue()); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Long_MaxValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Long valueRead = config.get("tests.converter.long.max", Long.class); + assertTrue(valueRead != null); + assertEquals(Long.MAX_VALUE, valueRead.longValue()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ShortConverterTest.java ---------------------------------------------------------------------- diff --git a/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ShortConverterTest.java b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ShortConverterTest.java new file mode 100644 index 0000000..193a92e --- /dev/null +++ b/java7/core/src/test/java/org/apache/tamaya/core/internal/converters/ShortConverterTest.java @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.core.internal.converters; + +import org.apache.tamaya.Configuration; +import org.apache.tamaya.ConfigurationProvider; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the default converter for Shorts. + */ +public class ShortConverterTest { + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Short_Decimal() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Short valueRead = config.get("tests.converter.short.decimal", Short.class); + assertTrue(valueRead != null); + assertEquals(valueRead.intValue(), 101); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Short_Octal() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Short valueRead = config.get("tests.converter.short.octal", Short.class); + assertTrue(valueRead != null); + assertEquals(valueRead.intValue(), Short.decode("02").intValue()); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Short_Hex() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Short valueRead = config.get("tests.converter.short.hex.lowerX", Short.class); + assertTrue(valueRead != null); + assertEquals(valueRead.intValue(), Short.decode("0x2F").intValue()); + valueRead = config.get("tests.converter.short.hex.upperX", Short.class); + assertTrue(valueRead != null); + assertEquals(valueRead.intValue(), Short.decode("0X3F").intValue()); + } + + /** + * Test conversion. The value are provided by + * {@link ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_NotPresent() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Short valueRead = config.get("tests.converter.short.foo", Short.class); + assertFalse(valueRead != null); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Short_MinValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Short valueRead = config.get("tests.converter.short.min", Short.class); + assertTrue(valueRead != null); + assertEquals(Short.MIN_VALUE, valueRead.intValue()); + } + + /** + * Test conversion. The value are provided by + * {@link org.apache.tamaya.core.internal.converters.ConverterTestsPropertySource}. + * @throws Exception + */ + @Test + public void testConvert_Short_MaxValue() throws Exception { + Configuration config = ConfigurationProvider.getConfiguration(); + Short valueRead = config.get("tests.converter.short.max", Short.class); + assertTrue(valueRead != null); + assertEquals(Short.MAX_VALUE, valueRead.intValue()); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java8/api/src/main/java/org/apache/tamaya/Configuration.java ---------------------------------------------------------------------- diff --git a/java8/api/src/main/java/org/apache/tamaya/Configuration.java b/java8/api/src/main/java/org/apache/tamaya/Configuration.java index 3b81775..da476ea 100644 --- a/java8/api/src/main/java/org/apache/tamaya/Configuration.java +++ b/java8/api/src/main/java/org/apache/tamaya/Configuration.java @@ -110,6 +110,21 @@ public interface Configuration { } /** + * Get the property keys as type T. This will implicitly require a corresponding {@link + * PropertyConverter} to be available that is capable current providing type T + * fromMap the given String keys. + * + * @param key the property's absolute, or relative path, e.g. @code + * a/b/c/d.myProperty}. + * @param type The target type required, not null. + * @return the property value, never null.. + * @throws ConfigException if the keys could not be converted to the required target type. + */ + default <T> Optional<T> getOptional(String key, TypeLiteral<T> type) { + return Optional.ofNullable(get(key, type)); + } + + /** * Get the property keys as type {@code Class<T>}. * <p> * If {@code Class<T>} is not one current http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java index 87f3d14..be1a57a 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigDecimalConverter.java @@ -21,17 +21,40 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.PropertyConverter; import java.math.BigDecimal; +import java.math.BigInteger; import java.util.Objects; +import java.util.logging.Logger; /** - * Converter, converting from String to BigDecimal. - * //X TODO not good enough as this is Locale dependent! + * Converter, converting from String to BigDecimal, the supported format is one of the following: + * <ul> + * <li>232573527352.76352753</li> + * <li>-23257352.735276352753</li> + * <li>-0xFFFFFF (integral numbers only)</li> + * <li>-0XFFFFAC (integral numbers only)</li> + * <li>0xFFFFFF (integral numbers only)</li> + * <li>0XFFFFAC (integral numbers only)</li> + * </ul> */ public class BigDecimalConverter implements PropertyConverter<BigDecimal>{ + /** The logger. */ + private static final Logger LOG = Logger.getLogger(BigDecimalConverter.class.getName()); + /** Converter to be used if the format is not directly supported by BigDecimal, e.g. for integral hex values. */ + private BigIntegerConverter integerConverter = new BigIntegerConverter(); @Override public BigDecimal convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - return new BigDecimal(trimmed); + try{ + return new BigDecimal(trimmed); + } catch(Exception e){ + LOG.finest(() -> "Parsing BigDecimal failed, trying BigInteger for: " + value); + BigInteger bigInt = integerConverter.convert(trimmed); + if(bigInt!=null){ + return new BigDecimal(bigInt); + } + LOG.finest(() -> "Failed to parse BigDecimal from: " + value); + return null; + } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java index 4db9bcd..078ee15 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BigIntegerConverter.java @@ -22,17 +22,71 @@ import org.apache.tamaya.PropertyConverter; import java.math.BigInteger; import java.util.Objects; +import java.util.logging.Level; +import java.util.logging.Logger; /** - * Converter, converting from String to BigInteger. - * //X TODO not good enough as this is Locale dependent! + * Converter, converting from String to BigInteger, the supported format is one of the following: + * <ul> + * <li>0xFFFFFF</li> + * <li>0XFFFFAC</li> + * <li>23257352735276352753</li> + * <li>-0xFFFFFF</li> + * <li>-0XFFFFAC</li> + * <li>-23257352735276352753</li> + * </ul> */ public class BigIntegerConverter implements PropertyConverter<BigInteger>{ + /** The logger. */ + private static final Logger LOG = Logger.getLogger(BigIntegerConverter.class.getName()); + /** Converter used to decode hex, octal values. */ + private ByteConverter byteConverter = new ByteConverter(); + @Override public BigInteger convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - return new BigInteger(trimmed); + if(trimmed.startsWith("0x") || trimmed.startsWith("0X")){ + LOG.finer(() -> "Parsing Hex value to BigInteger: " + value); + trimmed = trimmed.substring(2); + StringBuilder decimal = new StringBuilder(); + for(int offset = 0;offset < trimmed.length();offset+=2){ + if(offset==trimmed.length()-1){ + LOG.info(() -> "Invalid Hex-Byte-String: " + value); + return null; + } + byte val = byteConverter.convert("0x" + trimmed.substring(offset, offset + 2)); + if(val<10){ + decimal.append('0').append(val); + } else{ + decimal.append(val); + } + } + return new BigInteger(decimal.toString()); + } else if(trimmed.startsWith("-0x") || trimmed.startsWith("-0X")){ + LOG.finer(() -> "Parsing Hex value to BigInteger: " + value); + trimmed = trimmed.substring(3); + StringBuilder decimal = new StringBuilder(); + for(int offset = 0;offset < trimmed.length();offset+=2){ + if(offset==trimmed.length()-1){ + LOG.info("Invalid Hex-Byte-String: " + trimmed); + return null; + } + byte val = byteConverter.convert("0x" + trimmed.substring(offset, offset + 2)); + if(val<10){ + decimal.append('0').append(val); + } else{ + decimal.append(val); + } + } + return new BigInteger('-' + decimal.toString()); + } + try{ + return new BigInteger(trimmed); + } catch(Exception e){ + LOG.log(Level.FINEST, e, () -> "Failed to parse BigInteger from: " + value); + return null; + } } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java index 754b4b7..7ff8406 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/BooleanConverter.java @@ -52,8 +52,7 @@ public class BooleanConverter implements PropertyConverter<Boolean> { return Boolean.FALSE; default: LOG.warning("Unknown boolean value encountered: " + value); + return null; } - - return null; } } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/c5415ce4/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java ---------------------------------------------------------------------- diff --git a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java index 6e52ef1..d280746 100644 --- a/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java +++ b/java8/core/src/main/java/org/apache/tamaya/core/internal/converters/ByteConverter.java @@ -20,6 +20,7 @@ package org.apache.tamaya.core.internal.converters; import org.apache.tamaya.PropertyConverter; +import java.util.Locale; import java.util.Objects; /** @@ -30,6 +31,15 @@ public class ByteConverter implements PropertyConverter<Byte>{ @Override public Byte convert(String value) { String trimmed = Objects.requireNonNull(value).trim(); - return Byte.decode(trimmed); + switch(trimmed.toUpperCase(Locale.ENGLISH)){ + case "MIN_VALUE": + case "MIN": + return Byte.MIN_VALUE; + case "MAX_VALUE": + case "MAX": + return Byte.MAX_VALUE; + default: + return Byte.decode(trimmed); + } } }
