You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2014/05/30 18:47:59 UTC

svn commit: r1598666 - /logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverters.java

Author: ggregory
Date: Fri May 30 16:47:59 2014
New Revision: 1598666

URL: http://svn.apache.org/r1598666
Log:
Sort members.

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverters.java

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverters.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverters.java?rev=1598666&r1=1598665&r2=1598666&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverters.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverters.java Fri May 30 16:47:59 2014
@@ -48,147 +48,66 @@ public final class TypeConverters {
 
     // TODO: this could probably be combined with the usual plugin architecture instead
 
-    private static final Logger LOGGER = StatusLogger.getLogger();
-
-    private final Map<Class<?>, TypeConverter<?>> registry =
-        new ConcurrentHashMap<Class<?>, TypeConverter<?>>();
-
-    private static final class Holder {
-        private static final TypeConverters INSTANCE = new TypeConverters();
-    }
-
     /**
-     * Constructs default TypeConverter registry. Used solely by singleton instance.
+     * Parses a {@link String} into a {@link BigDecimal}.
      */
-    private TypeConverters() {
-    	// Primitive wrappers
-        registry.put(Boolean.class, new BooleanConverter());
-        registry.put(Byte.class, new ByteConverter());
-        registry.put(Character.class, new CharacterConverter());
-        registry.put(Double.class, new DoubleConverter());
-        registry.put(Float.class, new FloatConverter());
-        registry.put(Integer.class, new IntegerConverter());
-        registry.put(Long.class, new LongConverter());
-        registry.put(Short.class, new ShortConverter());
-    	// Primitives
-        registry.put(boolean.class, registry.get(Boolean.class));
-        registry.put(byte.class, new ByteConverter());
-        registry.put(char[].class, new CharArrayConverter());
-        registry.put(double.class, registry.get(Double.class));
-        registry.put(float.class, registry.get(Float.class));
-        registry.put(int.class, registry.get(Integer.class));
-        registry.put(long.class, registry.get(Long.class));
-        registry.put(short.class, registry.get(Short.class));
-    	// Primitive arrays
-        registry.put(byte[].class, new ByteArrayConverter());
-        registry.put(char.class, new CharacterConverter());
-        // Numbers
-        registry.put(BigInteger.class, new BigIntegerConverter());
-        registry.put(BigDecimal.class, new BigDecimalConverter());
-        // JRE
-        registry.put(String.class, new StringConverter());
-        registry.put(Charset.class, new CharsetConverter());        
-        registry.put(File.class, new FileConverter());
-        registry.put(URL.class, new UrlConverter());
-        registry.put(URI.class, new UriConverter());
-        registry.put(Class.class, new ClassConverter());
-        registry.put(Pattern.class, new PatternConverter());
-        // Log4J 
-        registry.put(Level.class, new LevelConverter());
-        registry.put(Filter.Result.class, new EnumConverter<Filter.Result>(Filter.Result.class));
-        registry.put(Facility.class, new EnumConverter<Facility>(Facility.class));
-        registry.put(Protocol.class, new EnumConverter<Protocol>(Protocol.class));
-        registry.put(HtmlLayout.FontSize.class, new EnumConverter<HtmlLayout.FontSize>(HtmlLayout.FontSize.class));
+    private static class BigDecimalConverter implements TypeConverter<BigDecimal> {
+        @Override
+        public BigDecimal convert(final String s) {
+            return new BigDecimal(s);
+        }
     }
 
     /**
-     * Locates a TypeConverter for a specified class.
-     *
-     * @param clazz the class to get a TypeConverter for
-     * @return the associated TypeConverter for that class or {@code null} if none could be found
+     * Parses a {@link String} into a {@link BigInteger}.
      */
-    public static TypeConverter<?> findTypeConverter(final Class<?> clazz) {
-        // TODO: what to do if there's no converter?
-        // supplementary idea: automatically add type converters for enums using EnglishEnums
-        // Idea 1: use reflection to see if the class has a static "valueOf" method and use that
-        // Idea 2: reflect on class's declared methods to see if any methods look suitable (probably too complex)
-        return Holder.INSTANCE.registry.get(clazz);
+    private static class BigIntegerConverter implements TypeConverter<BigInteger> {
+        @Override
+        public BigInteger convert(final String s) {
+            return new BigInteger(s);
+        }
     }
 
     /**
-     * Registers a TypeConverter for a specified class. This will overwrite any existing TypeConverter that may be
-     * registered for the class.
-     *
-     * @param clazz the class to register the TypeConverter for
-     * @param converter the TypeConverter to register
+     * Converts a {@link String} into a {@link Boolean}.
      */
-    public static void registerTypeConverter(final Class<?> clazz, final TypeConverter<?> converter) {
-        Holder.INSTANCE.registry.put(clazz, converter);
+    private static class BooleanConverter implements TypeConverter<Boolean> {
+        @Override
+        public Boolean convert(final String s) {
+            return Boolean.valueOf(s);
+        }
     }
 
     /**
-     * Converts a String to a given class if a TypeConverter is available for that class. Falls back to the provided
-     * default value if the conversion is unsuccessful. However, if the default value is <em>also</em> invalid, then
-     * {@code null} is returned (along with a nasty status log message).
-     *
-     * @param s     the string to convert
-     * @param clazz the class to try to convert the string to
-     * @param defaultValue the fallback object to use if the conversion is unsuccessful
-     * @return the converted object which may be {@code null} if the string is invalid for the given type
-     * @throws NullPointerException if {@code clazz} is {@code null}
-     * @throws IllegalArgumentException if no TypeConverter exists for the given class
+     * Converts a {@link String} into a {@code byte[]}.
      */
-    public static Object convert(final String s, final Class<?> clazz, final Object defaultValue) {
-        final TypeConverter<?> converter = findTypeConverter(
-            Assert.requireNonNull(clazz, "No class specified to convert to."));
-        if (converter == null) {
-            throw new IllegalArgumentException("No type converter found for class: " + clazz.getName());
-        }
-        if (s == null) {
-            LOGGER.debug("Null string given to convert. Using default [{}].", defaultValue);
-            return parseDefaultValue(converter, defaultValue);
-        }
-        try {
-            return converter.convert(s);
-        } catch (final Exception e) {
-            LOGGER.warn("Error while converting string [{}] to type [{}]. Using default value [{}].", s, clazz,
-                defaultValue, e);
-            return parseDefaultValue(converter, defaultValue);
-        }
-    }
-
-    private static Object parseDefaultValue(final TypeConverter<?> converter, final Object defaultValue) {
-        if (defaultValue == null) {
-            return null;
-        }
-        if (!(defaultValue instanceof String)) {
-            return defaultValue;
-        }
-        try {
-            return converter.convert((String) defaultValue);
-        } catch (final Exception e) {
-            LOGGER.debug("Can't parse default value [{}] for type [{}].", defaultValue, converter.getClass(), e);
-            return null;
+    private static class ByteArrayConverter implements TypeConverter<byte[]> {
+        @Override
+        public byte[] convert(final String s) {
+            return s.getBytes(Charset.defaultCharset());
         }
     }
 
     /**
-     * Returns the given {@link String}, no conversion takes place.
+     * Converts a {@link String} into a {@link Byte}.
      */
-    private static class StringConverter implements TypeConverter<String> {
+    private static class ByteConverter implements TypeConverter<Byte> {
         @Override
-        public String convert(final String s) {
-            return s;
+        public Byte convert(final String s) {
+            return Byte.valueOf(s);
         }
     }
 
     /**
-     * Converts a {@link String} into a {@link File}.
+     * Converts a {@link String} into a {@link Character}.
      */
-    private static class FileConverter implements TypeConverter<File> {
+    private static class CharacterConverter implements TypeConverter<Character> {
         @Override
-        public File convert(final String s) {
-            return new File(s);
+        public Character convert(final String s) {
+            if (s.length() != 1) {
+                throw new IllegalArgumentException("Character string must be of length 1: " + s);
+            }
+            return Character.valueOf(s.toCharArray()[0]);
         }
     }
 
@@ -203,12 +122,12 @@ public final class TypeConverters {
     }
 
     /**
-     * Converts a {@link String} into a {@code byte[]}.
+     * Converts a {@link String} into a {@link Charset}.
      */
-    private static class ByteArrayConverter implements TypeConverter<byte[]> {
+    private static class CharsetConverter implements TypeConverter<Charset> {
         @Override
-        public byte[] convert(final String s) {
-            return s.getBytes(Charset.defaultCharset());
+        public Charset convert(final String s) {
+            return Charset.forName(s);
         }
     }
 
@@ -223,58 +142,57 @@ public final class TypeConverters {
     }
 
     /**
-     * Converts a {@link String} into a {@link URI}.
+     * Converts a {@link String} into a {@link Double}.
      */
-    private static class UriConverter implements TypeConverter<URI> {
+    private static class DoubleConverter implements TypeConverter<Double> {
         @Override
-        public URI convert(final String s) throws URISyntaxException {
-            return new URI(s);
+        public Double convert(final String s) {
+            return Double.valueOf(s);
         }
     }
 
     /**
-     * Converts a {@link String} into a {@link URL}.
+     * Converts a {@link String} into a {@link Enum}. Returns {@code null} for invalid enum names.
+     *
+     * @param <E> the enum class to parse.
      */
-    private static class UrlConverter implements TypeConverter<URL> {
-        @Override
-        public URL convert(final String s) throws MalformedURLException {
-            return new URL(s);
+    private static class EnumConverter<E extends Enum<E>> implements TypeConverter<E> {
+        private final Class<E> clazz;
+
+        private EnumConverter(final Class<E> clazz) {
+            this.clazz = clazz;
         }
-    }
 
-    /**
-     * Converts a {@link String} into a {@link Boolean}.
-     */
-    private static class BooleanConverter implements TypeConverter<Boolean> {
         @Override
-        public Boolean convert(final String s) {
-            return Boolean.valueOf(s);
+        public E convert(final String s) {
+            return EnglishEnums.valueOf(clazz, s);
         }
     }
 
     /**
-     * Converts a {@link String} into a {@link Byte}.
+     * Converts a {@link String} into a {@link File}.
      */
-    private static class ByteConverter implements TypeConverter<Byte> {
+    private static class FileConverter implements TypeConverter<File> {
         @Override
-        public Byte convert(final String s) {
-            return Byte.valueOf(s);
+        public File convert(final String s) {
+            return new File(s);
         }
     }
 
     /**
-     * Converts a {@link String} into a {@link Character}.
+     * Converts a {@link String} into a {@link Float}.
      */
-    private static class CharacterConverter implements TypeConverter<Character> {
+    private static class FloatConverter implements TypeConverter<Float> {
         @Override
-        public Character convert(final String s) {
-            if (s.length() != 1) {
-                throw new IllegalArgumentException("Character string must be of length 1: " + s);
-            }
-            return Character.valueOf(s.toCharArray()[0]);
+        public Float convert(final String s) {
+            return Float.valueOf(s);
         }
     }
 
+    private static final class Holder {
+        private static final TypeConverters INSTANCE = new TypeConverters();
+    }
+
     /**
      * Converts a {@link String} into a {@link Integer}.
      */
@@ -286,12 +204,12 @@ public final class TypeConverters {
     }
 
     /**
-     * Converts a {@link String} into a {@link Short}.
+     * Converts a {@link String} into a Log4j {@link Level}. Returns {@code null} for invalid level names.
      */
-    private static class ShortConverter implements TypeConverter<Short> {
+    private static class LevelConverter implements TypeConverter<Level> {
         @Override
-        public Short convert(final String s) {
-            return Short.valueOf(s);
+        public Level convert(final String s) {
+            return Level.valueOf(s);
         }
     }
 
@@ -306,90 +224,172 @@ public final class TypeConverters {
     }
 
     /**
-     * Converts a {@link String} into a {@link Float}.
+     * Converts a {@link String} into a {@link Pattern}.
      */
-    private static class FloatConverter implements TypeConverter<Float> {
+    private static class PatternConverter implements TypeConverter<Pattern> {
         @Override
-        public Float convert(final String s) {
-            return Float.valueOf(s);
+        public Pattern convert(final String s) {
+            return Pattern.compile(s);
         }
     }
 
     /**
-     * Converts a {@link String} into a {@link Double}.
+     * Converts a {@link String} into a {@link Short}.
      */
-    private static class DoubleConverter implements TypeConverter<Double> {
+    private static class ShortConverter implements TypeConverter<Short> {
         @Override
-        public Double convert(final String s) {
-            return Double.valueOf(s);
+        public Short convert(final String s) {
+            return Short.valueOf(s);
         }
     }
 
     /**
-     * Converts a {@link String} into a {@link Pattern}.
+     * Returns the given {@link String}, no conversion takes place.
      */
-    private static class PatternConverter implements TypeConverter<Pattern> {
+    private static class StringConverter implements TypeConverter<String> {
         @Override
-        public Pattern convert(final String s) {
-            return Pattern.compile(s);
+        public String convert(final String s) {
+            return s;
         }
     }
 
     /**
-     * Converts a {@link String} into a {@link Charset}.
+     * Converts a {@link String} into a {@link URI}.
      */
-    private static class CharsetConverter implements TypeConverter<Charset> {
+    private static class UriConverter implements TypeConverter<URI> {
         @Override
-        public Charset convert(final String s) {
-            return Charset.forName(s);
+        public URI convert(final String s) throws URISyntaxException {
+            return new URI(s);
         }
     }
 
     /**
-     * Parses a {@link String} into a {@link BigDecimal}.
+     * Converts a {@link String} into a {@link URL}.
      */
-    private static class BigDecimalConverter implements TypeConverter<BigDecimal> {
+    private static class UrlConverter implements TypeConverter<URL> {
         @Override
-        public BigDecimal convert(final String s) {
-            return new BigDecimal(s);
+        public URL convert(final String s) throws MalformedURLException {
+            return new URL(s);
         }
     }
 
     /**
-     * Parses a {@link String} into a {@link BigInteger}.
+     * Converts a String to a given class if a TypeConverter is available for that class. Falls back to the provided
+     * default value if the conversion is unsuccessful. However, if the default value is <em>also</em> invalid, then
+     * {@code null} is returned (along with a nasty status log message).
+     *
+     * @param s     the string to convert
+     * @param clazz the class to try to convert the string to
+     * @param defaultValue the fallback object to use if the conversion is unsuccessful
+     * @return the converted object which may be {@code null} if the string is invalid for the given type
+     * @throws NullPointerException if {@code clazz} is {@code null}
+     * @throws IllegalArgumentException if no TypeConverter exists for the given class
      */
-    private static class BigIntegerConverter implements TypeConverter<BigInteger> {
-        @Override
-        public BigInteger convert(final String s) {
-            return new BigInteger(s);
+    public static Object convert(final String s, final Class<?> clazz, final Object defaultValue) {
+        final TypeConverter<?> converter = findTypeConverter(
+            Assert.requireNonNull(clazz, "No class specified to convert to."));
+        if (converter == null) {
+            throw new IllegalArgumentException("No type converter found for class: " + clazz.getName());
+        }
+        if (s == null) {
+            LOGGER.debug("Null string given to convert. Using default [{}].", defaultValue);
+            return parseDefaultValue(converter, defaultValue);
+        }
+        try {
+            return converter.convert(s);
+        } catch (final Exception e) {
+            LOGGER.warn("Error while converting string [{}] to type [{}]. Using default value [{}].", s, clazz,
+                defaultValue, e);
+            return parseDefaultValue(converter, defaultValue);
         }
     }
 
     /**
-     * Converts a {@link String} into a Log4j {@link Level}. Returns {@code null} for invalid level names.
+     * Locates a TypeConverter for a specified class.
+     *
+     * @param clazz the class to get a TypeConverter for
+     * @return the associated TypeConverter for that class or {@code null} if none could be found
      */
-    private static class LevelConverter implements TypeConverter<Level> {
-        @Override
-        public Level convert(final String s) {
-            return Level.valueOf(s);
+    public static TypeConverter<?> findTypeConverter(final Class<?> clazz) {
+        // TODO: what to do if there's no converter?
+        // supplementary idea: automatically add type converters for enums using EnglishEnums
+        // Idea 1: use reflection to see if the class has a static "valueOf" method and use that
+        // Idea 2: reflect on class's declared methods to see if any methods look suitable (probably too complex)
+        return Holder.INSTANCE.registry.get(clazz);
+    }
+
+    private static Object parseDefaultValue(final TypeConverter<?> converter, final Object defaultValue) {
+        if (defaultValue == null) {
+            return null;
+        }
+        if (!(defaultValue instanceof String)) {
+            return defaultValue;
+        }
+        try {
+            return converter.convert((String) defaultValue);
+        } catch (final Exception e) {
+            LOGGER.debug("Can't parse default value [{}] for type [{}].", defaultValue, converter.getClass(), e);
+            return null;
         }
     }
 
     /**
-     * Converts a {@link String} into a {@link Enum}. Returns {@code null} for invalid enum names.
+     * Registers a TypeConverter for a specified class. This will overwrite any existing TypeConverter that may be
+     * registered for the class.
      *
-     * @param <E> the enum class to parse.
+     * @param clazz the class to register the TypeConverter for
+     * @param converter the TypeConverter to register
      */
-    private static class EnumConverter<E extends Enum<E>> implements TypeConverter<E> {
-        private final Class<E> clazz;
+    public static void registerTypeConverter(final Class<?> clazz, final TypeConverter<?> converter) {
+        Holder.INSTANCE.registry.put(clazz, converter);
+    }
 
-        private EnumConverter(final Class<E> clazz) {
-            this.clazz = clazz;
-        }
+    private static final Logger LOGGER = StatusLogger.getLogger();
 
-        @Override
-        public E convert(final String s) {
-            return EnglishEnums.valueOf(clazz, s);
-        }
+    private final Map<Class<?>, TypeConverter<?>> registry =
+        new ConcurrentHashMap<Class<?>, TypeConverter<?>>();
+
+    /**
+     * Constructs default TypeConverter registry. Used solely by singleton instance.
+     */
+    private TypeConverters() {
+    	// Primitive wrappers
+        registry.put(Boolean.class, new BooleanConverter());
+        registry.put(Byte.class, new ByteConverter());
+        registry.put(Character.class, new CharacterConverter());
+        registry.put(Double.class, new DoubleConverter());
+        registry.put(Float.class, new FloatConverter());
+        registry.put(Integer.class, new IntegerConverter());
+        registry.put(Long.class, new LongConverter());
+        registry.put(Short.class, new ShortConverter());
+    	// Primitives
+        registry.put(boolean.class, registry.get(Boolean.class));
+        registry.put(byte.class, new ByteConverter());
+        registry.put(char[].class, new CharArrayConverter());
+        registry.put(double.class, registry.get(Double.class));
+        registry.put(float.class, registry.get(Float.class));
+        registry.put(int.class, registry.get(Integer.class));
+        registry.put(long.class, registry.get(Long.class));
+        registry.put(short.class, registry.get(Short.class));
+    	// Primitive arrays
+        registry.put(byte[].class, new ByteArrayConverter());
+        registry.put(char.class, new CharacterConverter());
+        // Numbers
+        registry.put(BigInteger.class, new BigIntegerConverter());
+        registry.put(BigDecimal.class, new BigDecimalConverter());
+        // JRE
+        registry.put(String.class, new StringConverter());
+        registry.put(Charset.class, new CharsetConverter());        
+        registry.put(File.class, new FileConverter());
+        registry.put(URL.class, new UrlConverter());
+        registry.put(URI.class, new UriConverter());
+        registry.put(Class.class, new ClassConverter());
+        registry.put(Pattern.class, new PatternConverter());
+        // Log4J 
+        registry.put(Level.class, new LevelConverter());
+        registry.put(Filter.Result.class, new EnumConverter<Filter.Result>(Filter.Result.class));
+        registry.put(Facility.class, new EnumConverter<Facility>(Facility.class));
+        registry.put(Protocol.class, new EnumConverter<Protocol>(Protocol.class));
+        registry.put(HtmlLayout.FontSize.class, new EnumConverter<HtmlLayout.FontSize>(HtmlLayout.FontSize.class));
     }
 }