commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adri...@apache.org
Subject svn commit: r931041 - in /commons/sandbox/convert/trunk/src: main/java/org/apache/commons/convert/ test/java/org/apache/commons/convert/
Date Tue, 06 Apr 2010 07:44:35 GMT
Author: adrianc
Date: Tue Apr  6 07:44:35 2010
New Revision: 931041

URL: http://svn.apache.org/viewvc?rev=931041&view=rev
Log:
Added new converter - GenericToStringConverter - to eliminate a lot of redundant converters.

Added:
    commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/GenericToStringConverter.java
  (with props)
Modified:
    commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/BooleanConverters.java
    commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/CollectionConverters.java
    commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java
    commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/MiscConverters.java
    commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NetConverters.java
    commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestBooleanConverters.java

Modified: commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/BooleanConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/BooleanConverters.java?rev=931041&r1=931040&r2=931041&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/BooleanConverters.java
(original)
+++ commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/BooleanConverters.java
Tue Apr  6 07:44:35 2010
@@ -42,16 +42,6 @@ public class BooleanConverters implement
         }
     }
 
-    public static class BooleanToString extends AbstractConverter<Boolean, String>
{
-        public BooleanToString() {
-            super(Boolean.class, String.class);
-        }
-
-        public String convert(Boolean obj) throws ConversionException {
-            return obj.toString();
-        }
-    }
-
     public static class IntegerToBoolean extends AbstractConverter<Integer, Boolean>
{
         public IntegerToBoolean() {
             super(Integer.class, Boolean.class);
@@ -74,5 +64,6 @@ public class BooleanConverters implement
 
     public void loadConverters() {
         Converters.loadContainedConverters(BooleanConverters.class);
+        Converters.registerConverter(new GenericToStringConverter<Boolean>(Boolean.class));
     }
 }

Modified: commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/CollectionConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/CollectionConverters.java?rev=931041&r1=931040&r2=931041&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/CollectionConverters.java
(original)
+++ commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/CollectionConverters.java
Tue Apr  6 07:44:35 2010
@@ -21,6 +21,7 @@ package org.apache.commons.convert;
 import java.lang.reflect.Array;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -90,16 +91,6 @@ public class CollectionConverters implem
         }
     }
 
-    public static class ListToString<T> extends AbstractConverter<List<T>,
String> {
-        public ListToString() {
-            super(List.class, String.class);
-        }
-
-        public String convert(List<T> obj) throws ConversionException {
-            return obj.toString();
-        }
-    }
-
     public static class MapToList<K, V> extends AbstractConverter<Map<K, V>,
List<Map<K, V>>> {
         public MapToList() {
             super(Map.class, List.class);
@@ -124,17 +115,9 @@ public class CollectionConverters implem
         }
     }
 
-    public static class MapToString<K, V> extends AbstractConverter<Map<K, V>,
String> {
-        public MapToString() {
-            super(Map.class, String.class);
-        }
-
-        public String convert(Map<K, V> obj) throws ConversionException {
-            return obj.toString();
-        }
-    }
-
+    @SuppressWarnings("unchecked")
     public void loadConverters() {
         Converters.loadContainedConverters(CollectionConverters.class);
+        Converters.registerConverter(new GenericToStringConverter<Collection>(Collection.class));
     }
 }

Modified: commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java?rev=931041&r1=931040&r2=931041&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java
(original)
+++ commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java
Tue Apr  6 07:44:35 2010
@@ -90,6 +90,7 @@ public class DateTimeConverters implemen
 
     public void loadConverters() {
         Converters.loadContainedConverters(DateTimeConverters.class);
+        Converters.registerConverter(new GenericToStringConverter<Calendar>(Calendar.class));
     }
 
     public static class CalendarToLong extends AbstractConverter<Calendar, Long> {
@@ -102,16 +103,6 @@ public class DateTimeConverters implemen
         }
     }
 
-    public static class CalendarToString extends AbstractConverter<Calendar, String>
{
-        public CalendarToString() {
-            super(Calendar.class, String.class);
-        }
-
-        public String convert(Calendar obj) throws ConversionException {
-            return obj.toString();
-        }
-    }
-
     public static class DateToLong extends AbstractConverter<Date, Long> {
         public DateToLong() {
             super(Date.class, Long.class);

Added: commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/GenericToStringConverter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/GenericToStringConverter.java?rev=931041&view=auto
==============================================================================
--- commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/GenericToStringConverter.java
(added)
+++ commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/GenericToStringConverter.java
Tue Apr  6 07:44:35 2010
@@ -0,0 +1,33 @@
+/*******************************************************************************
+ * 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.commons.convert;
+
+/** Generic object-to-string converter class. */
+public class GenericToStringConverter<S> extends AbstractConverter<S, String>
{
+
+    public GenericToStringConverter(Class<S> sourceClass) {
+        super(sourceClass, String.class);
+    }
+
+    @Override
+    public String convert(S obj) throws ConversionException {
+        return obj.toString();
+    }
+
+}

Propchange: commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/GenericToStringConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/GenericToStringConverter.java
------------------------------------------------------------------------------
    svn:keywords = Date Rev Author URL Id

Propchange: commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/GenericToStringConverter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/MiscConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/MiscConverters.java?rev=931041&r1=931040&r2=931041&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/MiscConverters.java
(original)
+++ commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/MiscConverters.java
Tue Apr  6 07:44:35 2010
@@ -196,16 +196,6 @@ public class MiscConverters implements C
         }
     }
 
-    public static class LocaleToString extends AbstractConverter<Locale, String> {
-        public LocaleToString() {
-            super(Locale.class, String.class);
-        }
-
-        public String convert(Locale obj) throws ConversionException {
-             return obj.toString();
-        }
-    }
-
     public static class StringToClob extends AbstractConverter<String, Clob> {
         public StringToClob() {
             super(String.class, Clob.class);
@@ -290,16 +280,6 @@ public class MiscConverters implements C
         }
     }
 
-    public static class UUIDToString extends AbstractConverter<UUID, String> {
-        public UUIDToString() {
-            super(UUID.class, String.class);
-        }
-
-        public String convert(UUID obj) throws ConversionException {
-            return obj.toString();
-        }
-    }
-
     public static class StringToUUID extends AbstractConverter<String, UUID> {
         public StringToUUID() {
             super(String.class, UUID.class);
@@ -310,16 +290,6 @@ public class MiscConverters implements C
         }
     }
 
-    public static class RegexPatternToString extends AbstractConverter<Pattern, String>
{
-        public RegexPatternToString() {
-            super(Pattern.class, String.class);
-        }
-
-        public String convert(Pattern obj) throws ConversionException {
-            return obj.toString();
-        }
-    }
-
     public static class StringToRegexPattern extends AbstractConverter<String, Pattern>
{
         public StringToRegexPattern() {
             super(String.class, Pattern.class);
@@ -338,5 +308,8 @@ public class MiscConverters implements C
 
     public void loadConverters() {
         Converters.loadContainedConverters(MiscConverters.class);
+        Converters.registerConverter(new GenericToStringConverter<Locale>(Locale.class));
+        Converters.registerConverter(new GenericToStringConverter<UUID>(UUID.class));
+        Converters.registerConverter(new GenericToStringConverter<Pattern>(Pattern.class));
     }
 }

Modified: commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NetConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NetConverters.java?rev=931041&r1=931040&r2=931041&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NetConverters.java
(original)
+++ commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/NetConverters.java
Tue Apr  6 07:44:35 2010
@@ -68,16 +68,6 @@ public class NetConverters implements Co
         }
     }
 
-    public static class URIToString extends AbstractConverter<URI, String> {
-        public URIToString() {
-            super(URI.class, String.class);
-        }
-
-        public String convert(URI obj) throws ConversionException {
-            return obj.toString();
-        }
-    }
-
     public static class StringToURL extends AbstractConverter<String, URL> {
         public StringToURL() {
             super(String.class, URL.class);
@@ -92,16 +82,6 @@ public class NetConverters implements Co
         }
     }
 
-    public static class URLToString extends AbstractConverter<URL, String> {
-        public URLToString() {
-            super(URL.class, String.class);
-        }
-
-        public String convert(URL obj) throws ConversionException {
-            return obj.toString();
-        }
-    }
-
     public static class URIToURL extends AbstractConverter<URI, URL> {
         public URIToURL() {
             super(URI.class, URL.class);
@@ -132,5 +112,7 @@ public class NetConverters implements Co
 
     public void loadConverters() {
         Converters.loadContainedConverters(NetConverters.class);
+        Converters.registerConverter(new GenericToStringConverter<URI>(URI.class));
+        Converters.registerConverter(new GenericToStringConverter<URL>(URL.class));
     }
 }

Modified: commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestBooleanConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestBooleanConverters.java?rev=931041&r1=931040&r2=931041&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestBooleanConverters.java
(original)
+++ commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestBooleanConverters.java
Tue Apr  6 07:44:35 2010
@@ -60,7 +60,7 @@ public class TestBooleanConverters exten
         ConverterLoader loader = new BooleanConverters();
         loader.loadConverters();
         assertFromBoolean("BooleanToInteger", new BooleanConverters.BooleanToInteger(), 1,
0);
-        assertFromBoolean("BooleanToString", new BooleanConverters.BooleanToString(), "true",
"false");
+        assertFromBoolean("BooleanToString", new GenericToStringConverter<Boolean>(Boolean.class),
"true", "false");
         assertToBoolean("IntegerToBoolean", new BooleanConverters.IntegerToBoolean(), 1,
0);
         assertToBoolean("StringToBoolean", new BooleanConverters.StringToBoolean(), "true",
"false");
         assertToCollection("BooleanToList", new BooleanConverters.BooleanToList(), List.class);



Mime
View raw message