tamaya-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anat...@apache.org
Subject [4/6] incubator-tamaya git commit: TAMAYA-63: Improved/unified implementations for various converters. Aligned tests for Java 7/8.
Date Thu, 12 Feb 2015 21:32:36 GMT
TAMAYA-63: Improved/unified implementations for various 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/2d80d49c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/2d80d49c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/2d80d49c

Branch: refs/heads/master
Commit: 2d80d49cd7619aac59735eab00f51d34669dca0f
Parents: 1e871d8
Author: anatole <anatole@apache.org>
Authored: Thu Feb 12 22:25:37 2015 +0100
Committer: anatole <anatole@apache.org>
Committed: Thu Feb 12 22:32:24 2015 +0100

----------------------------------------------------------------------
 .../converters/BigIntegerConverter.java         |  8 ++---
 .../internal/converters/BooleanConverter.java   |  2 +-
 .../core/internal/converters/ByteConverter.java | 12 ++++++-
 .../core/internal/converters/CharConverter.java |  2 +-
 .../internal/converters/CurrencyConverter.java  |  8 ++---
 .../internal/converters/DoubleConverter.java    | 13 ++++---
 .../core/internal/converters/EnumConverter.java |  9 ++++-
 .../internal/converters/FloatConverter.java     | 16 ++++-----
 .../internal/converters/IntegerConverter.java   | 14 +++++++-
 .../core/internal/converters/LongConverter.java | 33 +++++++++++------
 .../internal/converters/NumberConverter.java    | 37 ++++++++++++++++++--
 .../internal/converters/ShortConverter.java     | 13 ++++++-
 12 files changed, 124 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2d80d49c/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 de95d8a..f32e793 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
@@ -47,12 +47,12 @@ public class BigIntegerConverter implements PropertyConverter<BigInteger>{
     public BigInteger convert(String value) {
         String trimmed = Objects.requireNonNull(value).trim();
         if(trimmed.startsWith("0x") || trimmed.startsWith("0X")){
-            LOG.finer("Parsing Hex value to BigInteger: " + value);
+            LOG.finest("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);
+                    LOG.finest("Invalid Hex-Byte-String: " + value);
                     return null;
                 }
                 byte val = byteConverter.convert("0x" + trimmed.substring(offset, offset
+ 2));
@@ -64,12 +64,12 @@ public class BigIntegerConverter implements PropertyConverter<BigInteger>{
             }
             return new BigInteger(decimal.toString());
         } else if(trimmed.startsWith("-0x") || trimmed.startsWith("-0X")){
-            LOG.finer("Parsing Hex value to BigInteger: " + value);
+            LOG.finest("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);
+                    LOG.finest("Invalid Hex-Byte-String: " + trimmed);
                     return null;
                 }
                 byte val = byteConverter.convert("0x" + trimmed.substring(offset, offset
+ 2));

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2d80d49c/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 cd48c97..0f28131 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
@@ -49,7 +49,7 @@ public class BooleanConverter implements PropertyConverter<Boolean>
{
             case "f":
                 return Boolean.FALSE;
             default:
-                LOG.warning("Unknown boolean value encountered: " + value);
+                LOG.finest("Unknown boolean value encountered: " + value);
         }
         return null;
     }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2d80d49c/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 7b695bf..d0de5e0 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
@@ -22,6 +22,8 @@ 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 Byte, the supported format is one of the following:
@@ -42,6 +44,8 @@ import java.util.Objects;
  */
 public class ByteConverter implements PropertyConverter<Byte>{
 
+    private Logger LOG = Logger.getLogger(getClass().getName());
+
     @Override
     public Byte convert(String value) {
         String trimmed = Objects.requireNonNull(value).trim();
@@ -53,7 +57,13 @@ public class ByteConverter implements PropertyConverter<Byte>{
             case "MAX":
                 return Byte.MAX_VALUE;
             default:
-                return Byte.decode(trimmed);
+                try{
+                    return Byte.decode(trimmed);
+                }
+                catch(Exception e){
+                    LOG.log(Level.FINEST, "Unparseable Byte: " + value);
+                    return null;
+                }
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2d80d49c/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java
----------------------------------------------------------------------
diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java
b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java
index 3e59cb8..96a5aab 100644
--- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java
+++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CharConverter.java
@@ -51,7 +51,7 @@ public class CharConverter implements PropertyConverter<Character>{
                 }
                 return trimmed.charAt(0);
             } catch (Exception e) {
-                LOG.warning("Invalid character format encountered: '" + value + "', valid
formats are 'a', 101 and a.");
+                LOG.finest("Invalid character format encountered: '" + value + "', valid
formats are 'a', 101 and a.");
                 return null;
             }
         }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2d80d49c/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java
----------------------------------------------------------------------
diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java
b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java
index cd4ab04..e0b9de8 100644
--- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java
+++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/CurrencyConverter.java
@@ -45,7 +45,7 @@ public class CurrencyConverter implements PropertyConverter<Currency>
{
         try {
             return Currency.getInstance(trimmed.toUpperCase(Locale.ENGLISH));
         } catch (Exception e) {
-            LOG.log(Level.INFO, "Not a valid textual currency code: " + trimmed + ", checking
for numeric...", e);
+            LOG.log(Level.FINEST, "Not a valid textual currency code: " + trimmed + ", checking
for numeric...", e);
         }
         try {
             // Check for numeric code
@@ -56,7 +56,7 @@ public class CurrencyConverter implements PropertyConverter<Currency>
{
                 }
             }
         } catch (Exception e) {
-            LOG.log(Level.INFO, "Not a valid numeric currency code: " + trimmed + ", checking
for locale...", e);
+            LOG.log(Level.FINEST, "Not a valid numeric currency code: " + trimmed + ", checking
for locale...", e);
         }
         try {
             // Check for numeric code
@@ -78,9 +78,9 @@ public class CurrencyConverter implements PropertyConverter<Currency>
{
             if (locale != null) {
                 return Currency.getInstance(locale);
             }
-            LOG.info("Not a valid currency: " + trimmed + ", giving up...");
+            LOG.finest("Not a valid currency: " + trimmed + ", giving up...");
         } catch (Exception e) {
-            LOG.log(Level.INFO, "Not a valid country locale for currency: " + trimmed + ",
giving up...", e);
+            LOG.log(Level.FINEST, "Not a valid country locale for currency: " + trimmed +
", giving up...", e);
         }
         return null;
     }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2d80d49c/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 27f2cbd..15f0c18 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
@@ -66,15 +66,14 @@ public class DoubleConverter implements PropertyConverter<Double>
{
                     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);
+                    LOG.finest("Parsing of double as floating number failed, trying parsing
integral" +
+                            " number/hex instead...");
                 }
-                try {
-                    return integerConverter.convert(trimmed).doubleValue();
-                } catch (Exception e) {
-                    LOG.log(Level.INFO, "Unexpected error from LongConverter for " + trimmed,
e);
-                    return null;
+                Long val = integerConverter.convert(trimmed);
+                if(val!=null){
+                    return val.doubleValue();
                 }
+                return null;
         }
 
     }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2d80d49c/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java
----------------------------------------------------------------------
diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java
b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java
index dbdabf6..07fd35e 100644
--- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java
+++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/EnumConverter.java
@@ -24,6 +24,7 @@ import org.apache.tamaya.PropertyConverter;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.Objects;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 
 /**
@@ -52,7 +53,13 @@ public class EnumConverter<T> implements PropertyConverter<T>
{
         try {
             return (T) factory.invoke(null, value);
         } catch (InvocationTargetException | IllegalAccessException e) {
-            throw new ConfigException("Invalid enum value '" + value + "' for " + enumType.getName(),
e);
+            LOG.log(Level.FINEST, "Invalid enum value '" + value + "' for " + enumType.getName(),
e);
         }
+        try {
+            return (T) factory.invoke(null, value);
+        } catch (InvocationTargetException | IllegalAccessException e) {
+            LOG.log(Level.FINEST, "Invalid enum value '" + value + "' for " + enumType.getName(),
e);
+        }
+        return null;
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2d80d49c/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 c895634..d6d84f7 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
@@ -39,7 +39,7 @@ public class FloatConverter implements PropertyConverter<Float> {
     /**
      * The logger.
      */
-    private static final Logger LOG = Logger.getLogger(DoubleConverter.class.getName());
+    private static final Logger LOG = Logger.getLogger(FloatConverter.class.getName());
     /**
      * The converter used, when floating point parse failed.
      */
@@ -66,15 +66,15 @@ public class FloatConverter implements PropertyConverter<Float>
{
                     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);
+                    LOG.finest("Parsing of float as floating number failed, trying parsing
integral" +
+                            " number/hex instead...");
                 }
-                try{
-                    return integerConverter.convert(trimmed).floatValue();
-                } catch(Exception e){
-                    LOG.log(Level.INFO, "Unexpected error from LongConverter for " + trimmed,
e);
-                    return null;
+                Integer val = integerConverter.convert(trimmed);
+                if(val!=null) {
+                    return val.floatValue();
                 }
+                LOG.finest("Unparseable float value: " + trimmed);
+                return null;
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2d80d49c/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 75bf838..c1dbb93 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
@@ -22,6 +22,7 @@ import org.apache.tamaya.PropertyConverter;
 
 import java.util.Locale;
 import java.util.Objects;
+import java.util.logging.Logger;
 
 /**
  * Converter, converting from String to Integer, the supported format is one of the following:
@@ -42,6 +43,11 @@ import java.util.Objects;
  */
 public class IntegerConverter implements PropertyConverter<Integer>{
 
+    /**
+     * The logger.
+     */
+    private static final Logger LOG = Logger.getLogger(IntegerConverter.class.getName());
+
     @Override
     public Integer convert(String value) {
         String trimmed = Objects.requireNonNull(value).trim();
@@ -53,7 +59,13 @@ public class IntegerConverter implements PropertyConverter<Integer>{
             case "MAX":
                 return Integer.MAX_VALUE;
             default:
-                return Integer.decode(trimmed);
+                try{
+                    return Integer.decode(trimmed);
+                }
+                catch(Exception e){
+                    LOG.finest("Unparseable Integer value: " + trimmed);
+                    return null;
+                }
         }
 
     }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2d80d49c/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 1578254..4bbee27 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
@@ -22,6 +22,8 @@ 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 Long, the supported format is one of the following:
@@ -42,19 +44,28 @@ import java.util.Objects;
  */
 public class LongConverter implements PropertyConverter<Long>{
 
+    private static final Logger LOGGER = Logger.getLogger(LongConverter.class.getName());
+
     @Override
     public Long convert(String value) {
-        String trimmed = Objects.requireNonNull(value).trim();
-        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);
-        }
+
+            String trimmed = Objects.requireNonNull(value).trim();
+            switch (trimmed.toUpperCase(Locale.ENGLISH)) {
+                case "MIN_VALUE":
+                case "MIN":
+                    return Long.MIN_VALUE;
+                case "MAX_VALUE":
+                case "MAX":
+                    return Long.MAX_VALUE;
+                default:
+                    try {
+                        return Long.decode(trimmed);
+                    }
+                    catch(Exception e){
+                        LOGGER.finest("Unable to parse Long value: " + value);
+                        return null;
+                    }
+            }
 
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2d80d49c/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java
----------------------------------------------------------------------
diff --git a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java
b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java
index 3fdc68d..bb5a348 100644
--- a/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java
+++ b/java7/core/src/main/java/org/apache/tamaya/core/internal/converters/NumberConverter.java
@@ -21,17 +21,48 @@ package org.apache.tamaya.core.internal.converters;
 import org.apache.tamaya.PropertyConverter;
 
 import java.math.BigDecimal;
+import java.util.Locale;
 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 Number. Valid inputs are:
+ * <pre>
+ *     POSITIVE_INFINITY -> Double.POSITIVE_INFINITY
+ *     NEGATIVE_INFINITY -> Double.NEGATIVE_INFINITY
+ *     NaN -> Double.NaN
+ *     0xFFDCD3D2 -> Long
+ *     1234566789.23642327352735273752 -> new BigDecimal(input)
+ * </pre>
  */
 public class NumberConverter implements PropertyConverter<Number>{
+    /** the logger. */
+    private static final Logger LOGGER = Logger.getLogger(NumberConverter.class.getName());
+    /** Converter used for trying to parse as an integral value. */
+    private LongConverter longConverter = new LongConverter();
 
     @Override
     public Number convert(String value) {
         String trimmed = Objects.requireNonNull(value).trim();
-        return new BigDecimal(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;
+            default:
+                Long lVal = longConverter.convert(trimmed);
+                if (lVal != null) {
+                    return lVal;
+                }
+                try{
+                    return new BigDecimal(trimmed);
+                }
+                catch(Exception e){
+                    LOGGER.finest("Unparseable Number: " + trimmed);
+                    return null;
+                }
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/2d80d49c/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 6495ccd..15223dc 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
@@ -22,6 +22,7 @@ import org.apache.tamaya.PropertyConverter;
 
 import java.util.Locale;
 import java.util.Objects;
+import java.util.logging.Logger;
 
 /**
  * Converter, converting from String to Short, the supported format is one of the following:
@@ -42,6 +43,9 @@ import java.util.Objects;
  */
 public class ShortConverter implements PropertyConverter<Short>{
 
+    /** the logger. */
+    private static final Logger LOG = Logger.getLogger(ShortConverter.class.getName());
+
     @Override
     public Short convert(String value) {
         String trimmed = Objects.requireNonNull(value).trim();
@@ -53,7 +57,14 @@ public class ShortConverter implements PropertyConverter<Short>{
             case "MAX":
                 return Short.MAX_VALUE;
             default:
-                return Short.decode(trimmed);
+                try{
+                    return Short.decode(trimmed);
+                }
+                catch(Exception e){
+                    LOG.finest("Unparseable Short: " + trimmed);
+                    return null;
+                }
         }
     }
+
 }


Mime
View raw message