Author: ggregory
Date: Fri May 30 16:41:35 2014
New Revision: 1598663
URL: http://svn.apache.org/r1598663
Log:
- Add type converters to cover missing primitive types: char, byte, short.
- Also add useful JRE types: Class, File, URI, URL, BigInteger, BigDecimal.
- TypeConverter.convert(String) now throws Exception because all call sites
catch Exception and this also avoids all implementors to be forced to add
try/catch blocks for convertions (See URL and URI converters for example.)
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverter.java
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverters.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/util/TypeConvertersTest.java
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverter.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverter.java?rev=1598663&r1=1598662&r2=1598663&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverter.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/util/TypeConverter.java
Fri May 30 16:41:35 2014
@@ -20,15 +20,19 @@ package org.apache.logging.log4j.core.co
/**
* Interface for doing automatic String conversion to a specific type.
*
- * @param <T> the type this converter converts Strings into.
+ * @param <T>
+ * Converts Strings into the given type {@code T}.
*/
public interface TypeConverter<T> {
- /**
- * Converts a String to a given type.
- *
- * @param s the String to convert. Cannot be {@code null}.
- * @return the converted object.
- */
- T convert(String s);
+ /**
+ * Converts a String to a given type.
+ *
+ * @param s
+ * the String to convert. Cannot be {@code null}.
+ * @return the converted object.
+ * @throws Exception
+ * thrown when a conversion error occurs
+ */
+ T convert(String s) throws Exception;
}
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=1598663&r1=1598662&r2=1598663&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:41:35 2014
@@ -18,6 +18,12 @@
package org.apache.logging.log4j.core.config.plugins.util;
import java.io.File;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -30,6 +36,7 @@ import org.apache.logging.log4j.core.lay
import org.apache.logging.log4j.core.net.Facility;
import org.apache.logging.log4j.core.net.Protocol;
import org.apache.logging.log4j.core.util.Assert;
+import org.apache.logging.log4j.core.util.Loader;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.EnglishEnums;
@@ -54,27 +61,44 @@ public final class TypeConverters {
* Constructs default TypeConverter registry. Used solely by singleton
instance.
*/
private TypeConverters() {
- registry.put(String.class, new StringConverter());
- registry.put(char[].class, new CharArrayConverter());
- registry.put(byte[].class, new ByteArrayConverter());
+ // Primitive wrappers
registry.put(Boolean.class, new BooleanConverter());
- registry.put(boolean.class, registry.get(Boolean.class));
+ 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(int.class, registry.get(Integer.class));
registry.put(Long.class, new LongConverter());
- registry.put(long.class, registry.get(Long.class));
- registry.put(Float.class, new FloatConverter());
- registry.put(float.class, registry.get(Float.class));
- registry.put(Double.class, new DoubleConverter());
+ 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());
- registry.put(Charset.class, new CharsetConverter());
+ // 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));
- registry.put(File.class, new FileConverter());
}
/**
@@ -189,6 +213,36 @@ public final class TypeConverters {
}
/**
+ * Parses Strings into Classes.
+ */
+ private static class ClassConverter implements TypeConverter<Class<?>> {
+ @Override
+ public Class<?> convert(final String s) throws ClassNotFoundException {
+ return Loader.loadClass(s);
+ }
+ }
+
+ /**
+ * Parses Strings into URIs.
+ */
+ private static class UriConverter implements TypeConverter<URI> {
+ @Override
+ public URI convert(final String s) throws URISyntaxException {
+ return new URI(s);
+ }
+ }
+
+ /**
+ * Parses Strings into URIs.
+ */
+ private static class UrlConverter implements TypeConverter<URL> {
+ @Override
+ public URL convert(final String s) throws MalformedURLException {
+ return new URL(s);
+ }
+ }
+
+ /**
* Parses strings into booleans.
*/
private static class BooleanConverter implements TypeConverter<Boolean> {
@@ -199,6 +253,29 @@ public final class TypeConverters {
}
/**
+ * Parses strings into bytes.
+ */
+ private static class ByteConverter implements TypeConverter<Byte> {
+ @Override
+ public Byte convert(final String s) {
+ return Byte.valueOf(s);
+ }
+ }
+
+ /**
+ * Parses strings into character.
+ */
+ private static class CharacterConverter implements
TypeConverter<Character> {
+ @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]);
+ }
+ }
+
+ /**
* Parses strings into integers.
*/
private static class IntegerConverter implements TypeConverter<Integer> {
@@ -209,6 +286,16 @@ public final class TypeConverters {
}
/**
+ * Parses strings into shorts.
+ */
+ private static class ShortConverter implements TypeConverter<Short> {
+ @Override
+ public Short convert(final String s) {
+ return Short.valueOf(s);
+ }
+ }
+
+ /**
* Parses strings into longs.
*/
private static class LongConverter implements TypeConverter<Long> {
@@ -259,6 +346,26 @@ public final class TypeConverters {
}
/**
+ * Parses a {@link String} into a {@link BigDecimal}.
+ */
+ private static class BigDecimalConverter implements
TypeConverter<BigDecimal> {
+ @Override
+ public BigDecimal convert(final String s) {
+ return new BigDecimal(s);
+ }
+ }
+
+ /**
+ * Parses a {@link String} into a {@link BigInteger}.
+ */
+ private static class BigIntegerConverter implements
TypeConverter<BigInteger> {
+ @Override
+ public BigInteger convert(final String s) {
+ return new BigInteger(s);
+ }
+ }
+
+ /**
* Parses strings into Log4j Levels. Returns {@code null} for invalid
level names.
*/
private static class LevelConverter implements TypeConverter<Level> {
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/util/TypeConvertersTest.java
URL:
http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/util/TypeConvertersTest.java?rev=1598663&r1=1598662&r2=1598663&view=diff
==============================================================================
---
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/util/TypeConvertersTest.java
(original)
+++
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/plugins/util/TypeConvertersTest.java
Fri May 30 16:41:35 2014
@@ -17,7 +17,14 @@
package org.apache.logging.log4j.core.config.plugins.util;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
import java.io.File;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.net.URI;
+import java.net.URL;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
@@ -30,20 +37,19 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
-import static org.junit.Assert.*;
-
/**
- * Unit tests for the various supported TypeConverter implementations.
+ * Tests {@link TypeConverters}.
*/
@RunWith(Parameterized.class)
public class TypeConvertersTest {
+ @SuppressWarnings("boxing")
@Parameterized.Parameters
- public static Collection<Object[]> data() {
+ public static Collection<Object[]> data() throws Exception {
return Arrays.asList(
- // value, expected, default, type
+ // Array format: value, expected, default, type
new Object[][]{
- // booleans
+ // boolean
{ "true", true, null, Boolean.class },
{ "false", false, null, Boolean.class },
{ "True", true, null, Boolean.class },
@@ -56,7 +62,14 @@ public class TypeConvertersTest {
{ "FALSE", false, "true", boolean.class },
{ null, false, "false", boolean.class },
{ "invalid", false, "false", boolean.class },
- // integers
+ // byte
+ { "42", (byte)42, null, Byte.class },
+ { "53", (byte)53, null, Byte.class },
+ // char
+ { "A", 'A', null, char.class },
+ { "b", 'b', null, char.class },
+ { "b0", null, null, char.class },
+ // integer
{ "42", 42, null, Integer.class },
{ "53", 53, null, Integer.class },
{ "-16", -16, null, Integer.class },
@@ -86,12 +99,11 @@ public class TypeConvertersTest {
{ "3000", 3000L, "0", long.class },
{ "-543210", -543210L, "0", long.class },
{ "22.7", -53L, "-53", long.class },
- // charsets
- { "UTF-8", Charsets.UTF_8, null, Charset.class },
- { "ASCII", Charset.forName("ASCII"), "UTF-8", Charset.class },
- { "Not a real charset", Charsets.UTF_8, "UTF-8", Charset.class
},
- { null, Charsets.UTF_8, "UTF-8", Charset.class },
- { null, null, null, Charset.class },
+ // short
+ { "42", (short)42, null, short.class },
+ { "53", (short)53, null, short.class },
+ { "-16", (short)-16, null, Short.class },
+ // Log4j
// levels
{ "ERROR", Level.ERROR, null, Level.class },
{ "WARN", Level.WARN, null, Level.class },
@@ -117,7 +129,30 @@ public class TypeConvertersTest {
// arrays
{ "123", "123".toCharArray(), null, char[].class },
{ "123", "123".getBytes(Charset.defaultCharset()), null,
byte[].class },
+ // JRE
+ // JRE Charset
+ { "UTF-8", Charsets.UTF_8, null, Charset.class },
+ { "ASCII", Charset.forName("ASCII"), "UTF-8", Charset.class },
+ { "Not a real charset", Charsets.UTF_8, "UTF-8", Charset.class
},
+ { null, Charsets.UTF_8, "UTF-8", Charset.class },
+ { null, null, null, Charset.class },
+ // JRE File
{ "c:/temp", new File("c:/temp"), null, File.class },
+ // JRE Class
+ { TypeConvertersTest.class.getName(),
TypeConvertersTest.class, null, Class.class },
+ { "\n", null, null, Class.class },
+ // JRE URL
+ { "http://locahost", new URL("http://locahost"), null,
URL.class },
+ { "\n", null, null, URL.class },
+ // JRE URI
+ { "http://locahost", new URI("http://locahost"), null,
URI.class },
+ { "\n", null, null, URI.class },
+ // JRE BigInteger
+ { "9223372036854775817000", new
BigInteger("9223372036854775817000"), null, BigInteger.class },
+ { "\n", null, null, BigInteger.class },
+ // JRE BigInteger
+ { "9223372036854775817000.99999", new
BigDecimal("9223372036854775817000.99999"), null, BigDecimal.class },
+ { "\n", null, null, BigDecimal.class },
}
);
}