commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r922568 - /commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/
Date Sat, 13 Mar 2010 13:47:49 GMT
Author: niallp
Date: Sat Mar 13 13:47:49 2010
New Revision: 922568

URL: http://svn.apache.org/viewvc?rev=922568&view=rev
Log:
Re-apply Adrian's changes for commons

Modified:
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/AbstractConverter.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/AbstractLocalizedConverter.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/BooleanConverters.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/CollectionConverters.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConversionException.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/Converter.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConverterCreator.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConverterLoader.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/Converters.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/DateTimeConverters.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/GenericSingletonToList.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/GenericSingletonToSet.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/LocalizedConverter.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/MiscConverters.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/NetConverters.java
    commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/NumberConverters.java

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/AbstractConverter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/AbstractConverter.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/AbstractConverter.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/AbstractConverter.java Sat Mar 13 13:47:49 2010
@@ -16,9 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  *******************************************************************************/
-package org.ofbiz.base.conversion;
-
-import org.ofbiz.base.util.ObjectType;
+package org.apache.commons.convert;
 
 /** Abstract Converter class. This class handles converter registration
  * and it implements the <code>canConvert</code>, <code>getSourceClass</code>,
@@ -42,7 +40,7 @@ public abstract class AbstractConverter<
     }
 
     public boolean canConvert(Class<?> sourceClass, Class<?> targetClass) {
-        return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && ObjectType.instanceOf(targetClass, this.getTargetClass());
+        return Util.instanceOf(sourceClass, this.getSourceClass()) && Util.instanceOf(targetClass, this.getTargetClass());
     }
 
     public Class<? super S> getSourceClass() {

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/AbstractLocalizedConverter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/AbstractLocalizedConverter.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/AbstractLocalizedConverter.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/AbstractLocalizedConverter.java Sat Mar 13 13:47:49 2010
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  *******************************************************************************/
-package org.ofbiz.base.conversion;
+package org.apache.commons.convert;
 
 import java.util.Locale;
 import java.util.TimeZone;

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/BooleanConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/BooleanConverters.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/BooleanConverters.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/BooleanConverters.java Sat Mar 13 13:47:49 2010
@@ -16,13 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  *******************************************************************************/
-package org.ofbiz.base.conversion;
-
-import java.util.List;
-import java.util.Set;
-
-import javolution.util.FastList;
-import javolution.util.FastSet;
+package org.apache.commons.convert;
 
 /** Boolean Converter classes. */
 public class BooleanConverters implements ConverterLoader {

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/CollectionConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/CollectionConverters.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/CollectionConverters.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/CollectionConverters.java Sat Mar 13 13:47:49 2010
@@ -16,21 +16,16 @@
  * specific language governing permissions and limitations
  * under the License.
  *******************************************************************************/
-package org.ofbiz.base.conversion;
+package org.apache.commons.convert;
 
 import java.lang.reflect.Array;
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
-import javolution.util.FastList;
-import javolution.util.FastSet;
-
-import org.ofbiz.base.util.StringUtil;
-import org.ofbiz.base.util.UtilGenerics;
-import org.ofbiz.base.util.UtilValidate;
-
 /** Collection Converter classes. */
 public class CollectionConverters implements ConverterLoader {
     public static class ArrayCreator implements ConverterCreator, ConverterLoader {
@@ -48,7 +43,7 @@ public class CollectionConverters implem
             if (!(sourceClass.getComponentType() instanceof Object)) {
                 return null;
             }
-            return UtilGenerics.cast(new ArrayClassToList<S, T>(sourceClass, targetClass));
+            return Util.cast(new ArrayClassToList<S, T>(sourceClass, targetClass));
         }
     }
 
@@ -62,12 +57,12 @@ public class CollectionConverters implem
         }
 
         public T convert(S obj) throws ConversionException {
-            List<Object> list = FastList.newInstance();
+            List<Object> list = new ArrayList<Object>();
             int len = Array.getLength(obj);
             for (int i = 0; i < len; i++) {
                 list.add(Array.get(obj, i));
             }
-            return UtilGenerics.<T>cast(list);
+            return Util.<T>cast(list);
         }
     }
 
@@ -111,7 +106,7 @@ public class CollectionConverters implem
         }
 
         public List<Map<K, V>> convert(Map<K, V> obj) throws ConversionException {
-            List<Map<K, V>> tempList = FastList.newInstance();
+            List<Map<K, V>> tempList = new ArrayList<Map<K, V>>();
             tempList.add(obj);
             return tempList;
         }
@@ -123,7 +118,7 @@ public class CollectionConverters implem
         }
 
         public Set<Map<K, V>> convert(Map<K, V> obj) throws ConversionException {
-            Set<Map<K, V>> tempSet = FastSet.newInstance();
+            Set<Map<K, V>> tempSet = new HashSet<Map<K, V>>();
             tempSet.add(obj);
             return tempSet;
         }
@@ -139,47 +134,6 @@ public class CollectionConverters implem
         }
     }
 
-    public static class StringToList extends GenericSingletonToList<String> {
-        public StringToList() {
-            super(String.class);
-        }
-
-        public List<String> convert(String obj) throws ConversionException {
-            if (obj.startsWith("[") && obj.endsWith("]")) {
-                return StringUtil.toList(obj);
-            } else {
-                return super.convert(obj);
-            }
-        }
-    }
-
-    public static class StringToMap extends AbstractConverter<String, Map<String, String>> {
-        public StringToMap() {
-            super(String.class, Map.class);
-        }
-
-        public Map<String, String> convert(String obj) throws ConversionException {
-            if (obj.startsWith("{") && obj.endsWith("}")) {
-                return StringUtil.toMap(obj);
-            }
-            throw new ConversionException("Could not convert " + obj + " to Map: ");
-        }
-    }
-
-    public static class StringToSet extends GenericSingletonToSet<String> {
-        public StringToSet() {
-            super(String.class);
-        }
-
-        public Set<String> convert(String obj) throws ConversionException {
-            if (obj.startsWith("[") && obj.endsWith("]")) {
-                return StringUtil.toSet(obj);
-            } else {
-                return super.convert(obj);
-            }
-        }
-    }
-
     public void loadConverters() {
         Converters.loadContainedConverters(CollectionConverters.class);
     }

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConversionException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConversionException.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConversionException.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConversionException.java Sat Mar 13 13:47:49 2010
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  *******************************************************************************/
-package org.ofbiz.base.conversion;
+package org.apache.commons.convert;
 
 /** ConversionException class. */
 @SuppressWarnings("serial")

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/Converter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/Converter.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/Converter.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/Converter.java Sat Mar 13 13:47:49 2010
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  *******************************************************************************/
-package org.ofbiz.base.conversion;
+package org.apache.commons.convert;
 
 /** Converter interface. Classes implement this interface to convert one
  * Java object type to another.

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConverterCreator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConverterCreator.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConverterCreator.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConverterCreator.java Sat Mar 13 13:47:49 2010
@@ -16,14 +16,14 @@
  * specific language governing permissions and limitations
  * under the License.
  *******************************************************************************/
-package org.ofbiz.base.conversion;
+package org.apache.commons.convert;
 
 /** ConverterCreator interface. Classes implement this interface to create a
  * converter that can convert one Java object type to another.
  */
 public interface ConverterCreator {
     /** Creates a Converter that can convert the <code>sourceClass</code> to
-     * the <code>targetClass</code>. Returns <code>null</code> if this creater
+     * the <code>targetClass</code>. Returns <code>null</code> if this creator
      * doesn't support the class pair.
      *
      * @param sourceClass The source <code>Class</code> to convert

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConverterLoader.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConverterLoader.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConverterLoader.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/ConverterLoader.java Sat Mar 13 13:47:49 2010
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  *******************************************************************************/
-package org.ofbiz.base.conversion;
+package org.apache.commons.convert;
 
 /** Converter loader interface. Applications implement this
  * interface to load their Java object converters.
@@ -27,7 +27,7 @@ public interface ConverterLoader {
      * conversion framework. If the converter extends one of the
      * converter abstract classes, then the converter will register
      * itself when an instance is created. Otherwise, call
-     * {@link org.ofbiz.base.conversion.Converters#registerConverter(Converter)}
+     * {@link org.apache.commons.convert.Converters#registerConverter(Converter)}
      * with the <code>Converter</code> instance.
      *
      */

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/Converters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/Converters.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/Converters.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/Converters.java Sat Mar 13 13:47:49 2010
@@ -16,44 +16,34 @@
  * specific language governing permissions and limitations
  * under the License.
  *******************************************************************************/
-package org.ofbiz.base.conversion;
+package org.apache.commons.convert;
 
 import java.lang.reflect.Modifier;
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.Iterator;
-import java.util.ServiceLoader;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
 
-import javolution.util.FastMap;
-import javolution.util.FastSet;
-
-import org.ofbiz.base.util.Debug;
-import org.ofbiz.base.util.ObjectType;
-import org.ofbiz.base.util.UtilGenerics;
+import javax.imageio.spi.ServiceRegistry;
 
 /** A <code>Converter</code> factory and repository. */
 public class Converters {
-    protected static final String module = Converters.class.getName();
     protected static final String DELIMITER = "->";
-    protected static final FastMap<String, Converter<?, ?>> converterMap = FastMap.newInstance();
-    protected static final FastSet<ConverterCreator> creators = FastSet.newInstance();
-    protected static final FastSet<String> noConversions = FastSet.newInstance();
-    /** Null converter used when the source and target java object
-     * types are the same. The <code>convert</code> method returns the
-     * source object.
-     *
-     */
-    public static final Converter<Object, Object> nullConverter = new NullConverter();
+    protected static final ConcurrentHashMap<String, Converter<?, ?>> converterMap = new ConcurrentHashMap<String, Converter<?, ?>>();
+    protected static final Set<ConverterCreator> creators = Collections.synchronizedSet(new HashSet<ConverterCreator>());
+    protected static final Set<String> noConversions = Collections.synchronizedSet(new HashSet<String>());
 
     static {
-        converterMap.setShared(true);
         registerCreator(new PassThruConverterCreator());
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
-        Iterator<ConverterLoader> converterLoaders = ServiceLoader.load(ConverterLoader.class, loader).iterator();
+        Iterator<ConverterLoader> converterLoaders = ServiceRegistry.lookupProviders(ConverterLoader.class, loader);
         while (converterLoaders.hasNext()) {
             try {
                 ConverterLoader converterLoader = converterLoaders.next();
                 converterLoader.loadConverters();
             } catch (Exception e) {
-                Debug.logError(e, module);
+                e.printStackTrace();
             }
         }
     }
@@ -68,7 +58,7 @@ public class Converters {
      * <p>This method is intended to be used when the source or
      * target <code>Object</code> types are unknown at compile time.
      * If the source and target <code>Object</code> types are known
-     * at compile time, then one of the "ready made" converters should be used.</p>
+     * at compile time, then the appropriate converter instance should be used.</p>
      *
      * @param sourceClass The object class to convert from
      * @param targetClass The object class to convert to
@@ -77,14 +67,11 @@ public class Converters {
      */
     public static <S, T> Converter<S, T> getConverter(Class<S> sourceClass, Class<T> targetClass) throws ClassNotFoundException {
         String key = sourceClass.getName().concat(DELIMITER).concat(targetClass.getName());
-        if (Debug.verboseOn()) {
-            Debug.logVerbose("Getting converter: " + key, module);
-        }
 OUTER:
         do {
             Converter<?, ?> result = converterMap.get(key);
             if (result != null) {
-                return UtilGenerics.cast(result);
+                return Util.cast(result);
             }
             if (noConversions.contains(key)) {
                 throw new ClassNotFoundException("No converter found for " + key);
@@ -102,12 +89,7 @@ OUTER:
                     continue OUTER;
                 }
             }
-            if (noConversions.add(key)) {
-                Debug.logWarning("*** No converter found, converting from " +
-                        sourceClass.getName() + " to " + targetClass.getName() +
-                        ". Please report this message to the developer community so " +
-                        "a suitable converter can be created. ***", module);
-            }
+            noConversions.add(key);
             throw new ClassNotFoundException("No converter found for " + key);
         } while (true);
     }
@@ -141,13 +123,13 @@ OUTER:
                     }
                 }
             } catch (Exception e) {
-                Debug.logError(e, module);
+                e.printStackTrace();
             }
         }
     }
 
     /** Registers a <code>ConverterCreater</code> instance to be used by the
-     * {@link org.ofbiz.base.conversion.Converters#getConverter(Class, Class)}
+     * {@link org.apache.commons.convert.Converters#getConverter(Class, Class)}
      * method, when a converter can't be found.
      *
      * @param <S> The source object type
@@ -159,7 +141,7 @@ OUTER:
     }
 
     /** Registers a <code>Converter</code> instance to be used by the
-     * {@link org.ofbiz.base.conversion.Converters#getConverter(Class, Class)}
+     * {@link org.apache.commons.convert.Converters#getConverter(Class, Class)}
      * method.
      *
      * @param <S> The source object type
@@ -184,44 +166,7 @@ OUTER:
             sb.append("<null>");
         }
         String key = sb.toString();
-        if (converterMap.putIfAbsent(key, converter) == null) {
-            if (Debug.verboseOn()) {
-                Debug.logVerbose("Registered converter " + converter.getClass().getName(), module);
-            }
-        }
-    }
-
-    /** Null converter used when the source and target java object
-     * types are the same. The <code>convert</code> method returns the
-     * source object.
-     *
-     */
-    protected static class NullConverter implements Converter<Object, Object> {
-        public NullConverter() {
-        }
-
-        public boolean canConvert(Class<?> sourceClass, Class<?> targetClass) {
-            if (sourceClass.getName().equals(targetClass.getName()) || "java.lang.Object".equals(targetClass.getName())) {
-                return true;
-            }
-            return ObjectType.instanceOf(sourceClass, targetClass);
-        }
-
-        public Object convert(Object obj) throws ConversionException {
-            return obj;
-        }
-
-        public Object convert(Class<? extends Object> targetClass, Object obj) throws ConversionException {
-            return obj;
-        }
-
-        public Class<?> getSourceClass() {
-            return Object.class;
-        }
-
-        public Class<?> getTargetClass() {
-            return Object.class;
-        }
+        converterMap.putIfAbsent(key, converter);
     }
 
     protected static class PassThruConverterCreator implements ConverterCreator{
@@ -229,7 +174,7 @@ OUTER:
         }
 
         public <S, T> Converter<S, T> createConverter(Class<S> sourceClass, Class<T> targetClass) {
-            if (sourceClass == targetClass || targetClass == Object.class || ObjectType.instanceOf(sourceClass, targetClass)) {
+            if (sourceClass == targetClass || targetClass == Object.class || Util.instanceOf(sourceClass, targetClass)) {
                 return new PassThruConverter<S, T>(sourceClass, targetClass);
             } else {
                 return null;

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/DateTimeConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/DateTimeConverters.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/DateTimeConverters.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/DateTimeConverters.java Sat Mar 13 13:47:49 2010
@@ -16,28 +16,95 @@
  * specific language governing permissions and limitations
  * under the License.
  *******************************************************************************/
-package org.ofbiz.base.conversion;
+package org.apache.commons.convert;
 
+import java.sql.Timestamp;
 import java.text.DateFormat;
-import java.text.NumberFormat;
 import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Date;
-import java.util.List;
 import java.util.Locale;
-import java.util.Set;
 import java.util.TimeZone;
 
-import javolution.util.FastList;
-import javolution.util.FastSet;
-
-import org.ofbiz.base.util.TimeDuration;
-import org.ofbiz.base.util.UtilDateTime;
-import org.ofbiz.base.util.UtilValidate;
-
-import com.ibm.icu.util.Calendar;
-
 /** Date/time Converter classes. */
 public class DateTimeConverters implements ConverterLoader {
+    /**
+     * JDBC escape format for java.sql.Date conversions.
+     */
+    public static final String DATE_FORMAT = "yyyy-MM-dd";
+
+    /**
+     * JDBC escape format for java.sql.Timestamp conversions.
+     */
+    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
+
+    /**
+     * JDBC escape format for java.sql.Time conversions.
+     */
+    public static final String TIME_FORMAT = "HH:mm:ss";
+
+    /**
+     * Returns an initialized DateFormat object.
+     *
+     * @param dateFormat
+     *            optional format string
+     * @param tz
+     * @param locale
+     *            can be null if dateFormat is not null
+     * @return DateFormat object
+     */
+    public static DateFormat toDateFormat(String dateFormat, TimeZone tz, Locale locale) {
+        DateFormat df = null;
+        if (Util.isEmpty(dateFormat)) {
+            df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
+        } else {
+            df = new SimpleDateFormat(dateFormat);
+        }
+        df.setTimeZone(tz);
+        return df;
+    }
+
+    /**
+     * Returns an initialized DateFormat object.
+     * @param dateTimeFormat optional format string
+     * @param tz
+     * @param locale can be null if dateTimeFormat is not null
+     * @return DateFormat object
+     */
+    public static DateFormat toDateTimeFormat(String dateTimeFormat, TimeZone tz, Locale locale) {
+        DateFormat df = null;
+        if (Util.isEmpty(dateTimeFormat)) {
+            df = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);
+        } else {
+            df = new SimpleDateFormat(dateTimeFormat);
+        }
+        df.setTimeZone(tz);
+        return df;
+    }
+
+    /**
+     * Returns an initialized DateFormat object.
+     * @param timeFormat optional format string
+     * @param tz
+     * @param locale can be null if timeFormat is not null
+     * @return DateFormat object
+     */
+    public static DateFormat toTimeFormat(String timeFormat, TimeZone tz, Locale locale) {
+        DateFormat df = null;
+        if (Util.isEmpty(timeFormat)) {
+            df = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
+        } else {
+            df = new SimpleDateFormat(timeFormat);
+        }
+        df.setTimeZone(tz);
+        return df;
+    }
+
+    public void loadConverters() {
+        Converters.loadContainedConverters(DateTimeConverters.class);
+    }
+
     public static class CalendarToLong extends AbstractConverter<Calendar, Long> {
         public CalendarToLong() {
             super(Calendar.class, Long.class);
@@ -54,36 +121,33 @@ public class DateTimeConverters implemen
         }
 
         public String convert(Calendar obj) throws ConversionException {
-            Locale locale = obj.getLocale(com.ibm.icu.util.ULocale.VALID_LOCALE).toLocale();
-            TimeZone timeZone = UtilDateTime.toTimeZone(obj.getTimeZone().getID());
-            DateFormat df = UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, locale);
-            return df.format(obj);
+            return obj.toString();
         }
     }
 
-    public static class DateToLong extends AbstractConverter<java.util.Date, Long> {
+    public static class DateToLong extends AbstractConverter<Date, Long> {
         public DateToLong() {
-            super(java.util.Date.class, Long.class);
+            super(Date.class, Long.class);
         }
 
-        public Long convert(java.util.Date obj) throws ConversionException {
+        public Long convert(Date obj) throws ConversionException {
              return obj.getTime();
         }
     }
 
-    public static class DateToSqlDate extends AbstractConverter<java.util.Date, java.sql.Date> {
+    public static class DateToSqlDate extends AbstractConverter<Date, java.sql.Date> {
         public DateToSqlDate() {
-            super(java.util.Date.class, java.sql.Date.class);
+            super(Date.class, java.sql.Date.class);
         }
 
-        public java.sql.Date convert(java.util.Date obj) throws ConversionException {
+        public java.sql.Date convert(Date obj) throws ConversionException {
             return new java.sql.Date(obj.getTime());
         }
     }
 
-    public static class DateToString extends GenericLocalizedConverter<java.util.Date, String> {
+    public static class DateToString extends GenericLocalizedConverter<Date, String> {
         public DateToString() {
-            super(java.util.Date.class, String.class);
+            super(Date.class, String.class);
         }
 
         @Override
@@ -91,89 +155,22 @@ public class DateTimeConverters implemen
             return obj.toString();
         }
 
-        public String convert(java.util.Date obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
-            DateFormat df = null;
-            if (UtilValidate.isEmpty(formatString)) {
-                df = UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, locale);
-            } else {
-                df = UtilDateTime.toDateTimeFormat(formatString, timeZone, locale);
-            }
+        public String convert(Date obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+            DateFormat df = toDateTimeFormat(formatString, timeZone, locale);
             return df.format(obj);
         }
     }
 
-    public static class DateToTimestamp extends AbstractConverter<java.util.Date, java.sql.Timestamp> {
+    public static class DateToTimestamp extends AbstractConverter<Date, java.sql.Timestamp> {
         public DateToTimestamp() {
-            super(java.util.Date.class, java.sql.Timestamp.class);
+            super(Date.class, java.sql.Timestamp.class);
         }
 
-        public java.sql.Timestamp convert(java.util.Date obj) throws ConversionException {
+        public java.sql.Timestamp convert(Date obj) throws ConversionException {
             return new java.sql.Timestamp(obj.getTime());
         }
     }
 
-    public static class DurationToBigDecimal extends AbstractConverter<TimeDuration, java.math.BigDecimal> {
-        public DurationToBigDecimal() {
-            super(TimeDuration.class, java.math.BigDecimal.class);
-        }
-
-        public java.math.BigDecimal convert(TimeDuration obj) throws ConversionException {
-             return new java.math.BigDecimal(TimeDuration.toLong(obj));
-        }
-    }
-
-    public static class DurationToDouble extends AbstractConverter<TimeDuration, Double> {
-        public DurationToDouble() {
-            super(TimeDuration.class, Double.class);
-        }
-
-        public Double convert(TimeDuration obj) throws ConversionException {
-             return Double.valueOf(TimeDuration.toLong(obj));
-        }
-    }
-
-    public static class DurationToFloat extends AbstractConverter<TimeDuration, Float> {
-        public DurationToFloat() {
-            super(TimeDuration.class, Float.class);
-        }
-
-        public Float convert(TimeDuration obj) throws ConversionException {
-             return Float.valueOf(TimeDuration.toLong(obj));
-        }
-    }
-
-    public static class DurationToLong extends AbstractConverter<TimeDuration, Long> {
-        public DurationToLong() {
-            super(TimeDuration.class, Long.class);
-        }
-
-        public Long convert(TimeDuration obj) throws ConversionException {
-             return TimeDuration.toLong(obj);
-        }
-    }
-
-    public static class DurationToList extends GenericSingletonToList<TimeDuration> {
-        public DurationToList() {
-            super(TimeDuration.class);
-        }
-    }
-
-    public static class DurationToSet extends GenericSingletonToSet<TimeDuration> {
-        public DurationToSet() {
-            super(TimeDuration.class);
-        }
-    }
-
-    public static class DurationToString extends AbstractConverter<TimeDuration, String> {
-        public DurationToString() {
-            super(TimeDuration.class, String.class);
-        }
-
-        public String convert(TimeDuration obj) throws ConversionException {
-             return obj.toString();
-        }
-    }
-
     public static abstract class GenericLocalizedConverter<S, T> extends AbstractLocalizedConverter<S, T> {
         protected GenericLocalizedConverter(Class<S> sourceClass, Class<T> targetClass) {
             super(sourceClass, targetClass);
@@ -194,11 +191,15 @@ public class DateTimeConverters implemen
         }
 
         public Calendar convert(Long obj) throws ConversionException {
-            return convert(obj, Locale.getDefault(), TimeZone.getDefault());
+            Calendar cal = Calendar.getInstance();
+            cal.setTimeInMillis(obj);
+            return cal;
         }
 
         public Calendar convert(Long obj, Locale locale, TimeZone timeZone) throws ConversionException {
-            return UtilDateTime.toCalendar(new java.util.Date(obj.longValue()), timeZone, locale);
+            Calendar cal = Calendar.getInstance(timeZone, locale);
+            cal.setTimeInMillis(obj);
+            return cal;
         }
 
         public Calendar convert(Long obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
@@ -206,23 +207,13 @@ public class DateTimeConverters implemen
         }
     }
 
-    public static class NumberToDate extends AbstractConverter<Number, java.util.Date> {
+    public static class NumberToDate extends AbstractConverter<Number, Date> {
         public NumberToDate() {
-            super(Number.class, java.util.Date.class);
+            super(Number.class, Date.class);
         }
 
-        public java.util.Date convert(Number obj) throws ConversionException {
-             return new java.util.Date(obj.longValue());
-        }
-    }
-
-    public static class NumberToDuration extends AbstractConverter<Number, TimeDuration> {
-        public NumberToDuration() {
-            super(Number.class, TimeDuration.class);
-        }
-
-        public TimeDuration convert(Number obj) throws ConversionException {
-             return TimeDuration.fromNumber(obj);
+        public Date convert(Number obj) throws ConversionException {
+             return new Date(obj.longValue());
         }
     }
 
@@ -256,13 +247,13 @@ public class DateTimeConverters implemen
         }
     }
 
-    public static class SqlDateToDate extends AbstractConverter<java.sql.Date, java.util.Date> {
+    public static class SqlDateToDate extends AbstractConverter<java.sql.Date, Date> {
         public SqlDateToDate() {
-            super(java.sql.Date.class, java.util.Date.class);
+            super(java.sql.Date.class, Date.class);
         }
 
-        public java.util.Date convert(java.sql.Date obj) throws ConversionException {
-            return new java.util.Date(obj.getTime());
+        public Date convert(java.sql.Date obj) throws ConversionException {
+            return new Date(obj.getTime());
         }
     }
 
@@ -289,12 +280,7 @@ public class DateTimeConverters implemen
         }
 
         public String convert(java.sql.Date obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
-            DateFormat df = null;
-            if (UtilValidate.isEmpty(formatString)) {
-                df = UtilDateTime.toDateFormat(UtilDateTime.DATE_FORMAT, timeZone, locale);
-            } else {
-                df = UtilDateTime.toDateFormat(formatString, timeZone, locale);
-            }
+            DateFormat df = toDateFormat(formatString, timeZone, locale);
             return df.format(obj);
         }
     }
@@ -309,22 +295,6 @@ public class DateTimeConverters implemen
        }
     }
 
-    public static class SqlTimeToSqlDate extends AbstractConverter<java.sql.Time, java.sql.Date> {
-        public SqlTimeToSqlDate() {
-            super(java.sql.Time.class, java.sql.Date.class);
-        }
-
-        public java.sql.Date convert(java.sql.Time obj) throws ConversionException {
-            throw new ConversionException("Conversion from Time to Date not supported");
-        }
-    }
-
-    public static class SqlTimeToList extends GenericSingletonToList<java.sql.Time> {
-        public SqlTimeToList() {
-            super(java.sql.Time.class);
-        }
-    }
-
     public static class SqlDateToTimestamp extends AbstractConverter<java.sql.Date, java.sql.Timestamp> {
         public SqlDateToTimestamp() {
             super(java.sql.Date.class, java.sql.Timestamp.class);
@@ -335,29 +305,44 @@ public class DateTimeConverters implemen
        }
     }
 
+    public static class SqlTimeToList extends GenericSingletonToList<java.sql.Time> {
+        public SqlTimeToList() {
+            super(java.sql.Time.class);
+        }
+    }
+
     public static class SqlTimeToSet extends GenericSingletonToSet<java.sql.Time> {
         public SqlTimeToSet() {
             super(java.sql.Time.class);
         }
     }
 
+    public static class SqlTimeToSqlDate extends AbstractConverter<java.sql.Time, java.sql.Date> {
+        public SqlTimeToSqlDate() {
+            super(java.sql.Time.class, java.sql.Date.class);
+        }
+
+        public java.sql.Date convert(java.sql.Time obj) throws ConversionException {
+            throw new ConversionException("Conversion from Time to Date not supported");
+        }
+    }
+
     public static class SqlTimeToString extends GenericLocalizedConverter<java.sql.Time, String> {
         public SqlTimeToString() {
             super(java.sql.Time.class, String.class);
         }
 
         public String convert(java.sql.Time obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
-            DateFormat df = null;
-            if (UtilValidate.isEmpty(formatString)) {
-                df = UtilDateTime.toTimeFormat(UtilDateTime.TIME_FORMAT, timeZone, locale);
-            } else {
-                df = UtilDateTime.toTimeFormat(formatString, timeZone, locale);
-            }
+            DateFormat df = toTimeFormat(formatString, timeZone, locale);
             return df.format(obj);
         }
     }
 
     public static class StringToCalendar extends AbstractLocalizedConverter<String, Calendar> {
+        public StringToCalendar() {
+            super(String.class, Calendar.class);
+        }
+
         public Calendar convert(String obj) throws ConversionException {
             return convert(obj, Locale.getDefault(), TimeZone.getDefault(), null);
         }
@@ -366,101 +351,53 @@ public class DateTimeConverters implemen
             return convert(obj, Locale.getDefault(), TimeZone.getDefault(), null);
         }
 
-        public StringToCalendar() {
-            super(String.class, Calendar.class);
-        }
-
         public Calendar convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
-            String trimStr = obj.trim();
-            if (trimStr.length() == 0) {
-                return null;
-            }
-            DateFormat df = null;
-            if (UtilValidate.isEmpty(formatString)) {
-                df = UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, locale);
-            } else {
-                df = UtilDateTime.toDateTimeFormat(formatString, timeZone, locale);
-            }
+            DateFormat df = toDateTimeFormat(formatString, timeZone, locale);
             try {
-                java.util.Date date = df.parse(trimStr);
-                return UtilDateTime.toCalendar(date, timeZone, locale);
+                Date date = df.parse(obj);
+                Calendar cal = Calendar.getInstance(timeZone, locale);
+                cal.setTimeInMillis(date.getTime());
+                return cal;
             } catch (ParseException e) {
                 throw new ConversionException(e);
             }
         }
     }
 
-    public static class StringToDate extends GenericLocalizedConverter<String, java.util.Date> {
+    public static class StringToDate extends GenericLocalizedConverter<String, Date> {
         public StringToDate() {
-            super(String.class, java.util.Date.class);
+            super(String.class, Date.class);
         }
 
-        public java.util.Date convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
-            String trimStr = obj.trim();
-            if (trimStr.length() == 0) {
-                return null;
-            }
-            DateFormat df = null;
-            if (UtilValidate.isEmpty(formatString)) {
-                df = UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, locale);
-            } else {
-                df = UtilDateTime.toDateTimeFormat(formatString, timeZone, locale);
-            }
+        public Date convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+            DateFormat df = toDateTimeFormat(formatString, timeZone, locale);
             try {
-                return df.parse(trimStr);
+                return df.parse(obj);
             } catch (ParseException e) {
                 throw new ConversionException(e);
             }
         }
     }
 
-    public static class StringToDuration extends AbstractLocalizedConverter<String, TimeDuration> {
-        public StringToDuration() {
-            super(String.class, TimeDuration.class);
-        }
-
-        public TimeDuration convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
-            return convert(obj, locale, timeZone);
-        }
-
-        public TimeDuration convert(String obj, Locale locale, TimeZone timeZone) throws ConversionException {
-            if (!obj.contains(":")) {
-                // Encoded duration
-                try {
-                    NumberFormat nf = NumberFormat.getNumberInstance(locale);
-                    nf.setMaximumFractionDigits(0);
-                    Number number = nf.parse(obj);
-                    return TimeDuration.fromNumber(number);
-                } catch (ParseException e) {
-                    throw new ConversionException(e);
-                }
-            }
-            return convert(obj);
-        }
-
-        public TimeDuration convert(String obj) throws ConversionException {
-            return TimeDuration.parseDuration(obj);
-        }
-    }
-
     public static class StringToSqlDate extends GenericLocalizedConverter<String, java.sql.Date> {
         public StringToSqlDate() {
             super(String.class, java.sql.Date.class);
         }
 
-        public java.sql.Date convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
-            String trimStr = obj.trim();
-            if (trimStr.length() == 0) {
-                return null;
-            }
-            DateFormat df = null;
-            if (UtilValidate.isEmpty(formatString)) {
-                df = UtilDateTime.toDateFormat(UtilDateTime.DATE_FORMAT, timeZone, locale);
-            } else {
-                df = UtilDateTime.toDateFormat(formatString, timeZone, locale);
+        @Override
+        public java.sql.Date convert(String obj) throws ConversionException {
+            DateFormat df = new SimpleDateFormat(DATE_FORMAT);
+            try {
+                return new java.sql.Date(df.parse(obj).getTime());
+            } catch (ParseException e) {
+                throw new ConversionException(e);
             }
+        }
+
+        public java.sql.Date convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+            DateFormat df = toDateFormat(formatString, timeZone, locale);
             try {
-                return new java.sql.Date(df.parse(trimStr).getTime());
+                return new java.sql.Date(df.parse(obj).getTime());
             } catch (ParseException e) {
                 throw new ConversionException(e);
             }
@@ -472,19 +409,20 @@ public class DateTimeConverters implemen
             super(String.class, java.sql.Time.class);
         }
 
-        public java.sql.Time convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
-            String trimStr = obj.trim();
-            if (trimStr.length() == 0) {
-                return null;
-            }
-            DateFormat df = null;
-            if (UtilValidate.isEmpty(formatString)) {
-                df = UtilDateTime.toTimeFormat(UtilDateTime.TIME_FORMAT, timeZone, locale);
-            } else {
-                df = UtilDateTime.toTimeFormat(formatString, timeZone, locale);
+        @Override
+        public java.sql.Time convert(String obj) throws ConversionException {
+            DateFormat df = new SimpleDateFormat(TIME_FORMAT);
+            try {
+                return new java.sql.Time(df.parse(obj).getTime());
+            } catch (ParseException e) {
+                throw new ConversionException(e);
             }
+        }
+
+        public java.sql.Time convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+            DateFormat df = toTimeFormat(formatString, timeZone, locale);
             try {
-                return new java.sql.Time(df.parse(trimStr).getTime());
+                return new java.sql.Time(df.parse(obj).getTime());
             } catch (ParseException e) {
                 throw new ConversionException(e);
             }
@@ -496,36 +434,20 @@ public class DateTimeConverters implemen
             super(String.class, java.sql.Timestamp.class);
         }
 
-        public java.sql.Timestamp convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
-            String str = obj.trim();
-            if (str.length() == 0) {
-                return null;
-            }
-            DateFormat df = null;
-            if (UtilValidate.isEmpty(formatString)) {
-                // These hacks are a bad idea, but they are included
-                // for backward compatibility.
-                if (str.length() > 0 && !str.contains(":")) {
-                    str = str + " 00:00:00.00";
-                }
-                // hack to mimic Timestamp.valueOf() method
-                if (str.length() > 0 && !str.contains(".")) {
-                    str = str + ".0";
-                } else {
-                    // DateFormat has a funny way of parsing milliseconds:
-                    // 00:00:00.2 parses to 00:00:00.002
-                    // so we'll add zeros to the end to get 00:00:00.200
-                    String[] timeSplit = str.split("[.]");
-                    if (timeSplit.length > 1 && timeSplit[1].length() < 3) {
-                        str = str + "000".substring(timeSplit[1].length());
-                    }
-                }
-                df = UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, locale);
-            } else {
-                df = UtilDateTime.toDateTimeFormat(formatString, timeZone, locale);
+        @Override
+        public Timestamp convert(String obj) throws ConversionException {
+            DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT);
+            try {
+                return new java.sql.Timestamp(df.parse(obj).getTime());
+            } catch (ParseException e) {
+                throw new ConversionException(e);
             }
+        }
+
+        public java.sql.Timestamp convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+            DateFormat df = toDateTimeFormat(formatString, timeZone, locale);
             try {
-                return new java.sql.Timestamp(df.parse(str).getTime());
+                return new java.sql.Timestamp(df.parse(obj).getTime());
             } catch (ParseException e) {
                 throw new ConversionException(e);
             }
@@ -538,31 +460,38 @@ public class DateTimeConverters implemen
         }
 
         public TimeZone convert(String obj) throws ConversionException {
-            TimeZone tz = UtilDateTime.toTimeZone(obj);
-            if (tz != null) {
-                return tz;
-            } else {
-                throw new ConversionException("Could not convert " + obj + " to TimeZone: ");
-            }
+            return TimeZone.getTimeZone(obj);
         }
     }
 
-    public static class TimestampToDate extends AbstractConverter<java.sql.Timestamp, java.util.Date> {
+    public static class TimestampToDate extends AbstractConverter<java.sql.Timestamp, Date> {
         public TimestampToDate() {
-            super(java.sql.Timestamp.class, java.util.Date.class);
+            super(java.sql.Timestamp.class, Date.class);
         }
 
-        public java.util.Date convert(java.sql.Timestamp obj) throws ConversionException {
-            return new java.sql.Timestamp(obj.getTime());
+        public Date convert(java.sql.Timestamp obj) throws ConversionException {
+            return new Timestamp(obj.getTime());
         }
     }
 
-    public static class TimestampToSqlDate extends AbstractConverter<java.sql.Timestamp, java.sql.Date> {
+    public static class TimestampToList extends GenericSingletonToList<Timestamp> {
+        public TimestampToList() {
+            super(Timestamp.class);
+        }
+    }
+
+    public static class TimestampToSet extends GenericSingletonToSet<Timestamp> {
+        public TimestampToSet() {
+            super(Timestamp.class);
+        }
+    }
+
+    public static class TimestampToSqlDate extends AbstractConverter<Timestamp, java.sql.Date> {
         public TimestampToSqlDate() {
-            super(java.sql.Timestamp.class, java.sql.Date.class);
+            super(Timestamp.class, java.sql.Date.class);
         }
 
-        public java.sql.Date convert(java.sql.Timestamp obj) throws ConversionException {
+        public java.sql.Date convert(Timestamp obj) throws ConversionException {
             return new java.sql.Date(obj.getTime());
         }
     }
@@ -576,20 +505,4 @@ public class DateTimeConverters implemen
             return obj.getID();
         }
     }
-
-    public static class TimestampToList extends GenericSingletonToList<java.sql.Timestamp> {
-        public TimestampToList() {
-            super(java.sql.Timestamp.class);
-        }
-    }
-
-    public static class TimestampToSet extends GenericSingletonToSet<java.sql.Timestamp> {
-        public TimestampToSet() {
-            super(java.sql.Timestamp.class);
-        }
-    }
-
-    public void loadConverters() {
-        Converters.loadContainedConverters(DateTimeConverters.class);
-    }
 }

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/GenericSingletonToList.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/GenericSingletonToList.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/GenericSingletonToList.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/GenericSingletonToList.java Sat Mar 13 13:47:49 2010
@@ -1,35 +1,34 @@
-/*******************************************************************************
- * 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.ofbiz.base.conversion;
-
-import java.util.List;
-
-import javolution.util.FastList;
-
-public class GenericSingletonToList<T> extends AbstractConverter<T, List<T>> {
-    public GenericSingletonToList(Class<T> sourceClass) {
-        super(sourceClass, List.class);
-    }
-
-    public List<T> convert(T obj) throws ConversionException {
-        List<T> tempList = FastList.newInstance();
-        tempList.add(obj);
-        return tempList;
-    }
-}
+/*******************************************************************************
+ * 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;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class GenericSingletonToList<T> extends AbstractConverter<T, List<T>> {
+    public GenericSingletonToList(Class<T> sourceClass) {
+        super(sourceClass, List.class);
+    }
+
+    public List<T> convert(T obj) throws ConversionException {
+        List<T> tempList = new ArrayList<T>();
+        tempList.add(obj);
+        return tempList;
+    }
+}

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/GenericSingletonToSet.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/GenericSingletonToSet.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/GenericSingletonToSet.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/GenericSingletonToSet.java Sat Mar 13 13:47:49 2010
@@ -1,35 +1,34 @@
-/*******************************************************************************
- * 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.ofbiz.base.conversion;
-
-import java.util.Set;
-
-import javolution.util.FastSet;
-
-public class GenericSingletonToSet<T> extends AbstractConverter<T, Set<T>> {
-    public GenericSingletonToSet(Class<T> sourceClass) {
-        super(sourceClass, Set.class);
-    }
-
-    public Set<T> convert(T obj) throws ConversionException {
-        Set<T> tempSet = FastSet.newInstance();
-        tempSet.add(obj);
-        return tempSet;
-    }
-}
+/*******************************************************************************
+ * 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;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public class GenericSingletonToSet<T> extends AbstractConverter<T, Set<T>> {
+    public GenericSingletonToSet(Class<T> sourceClass) {
+        super(sourceClass, Set.class);
+    }
+
+    public Set<T> convert(T obj) throws ConversionException {
+        Set<T> tempSet = new HashSet<T>();
+        tempSet.add(obj);
+        return tempSet;
+    }
+}

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/LocalizedConverter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/LocalizedConverter.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/LocalizedConverter.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/LocalizedConverter.java Sat Mar 13 13:47:49 2010
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  *******************************************************************************/
-package org.ofbiz.base.conversion;
+package org.apache.commons.convert;
 
 import java.util.Locale;
 import java.util.TimeZone;

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/MiscConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/MiscConverters.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/MiscConverters.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/MiscConverters.java Sat Mar 13 13:47:49 2010
@@ -16,9 +16,10 @@
  * specific language governing permissions and limitations
  * under the License.
  *******************************************************************************/
-package org.ofbiz.base.conversion;
+package org.apache.commons.convert;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.Reader;
 import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
@@ -30,14 +31,9 @@ import java.util.Locale;
 import java.util.UUID;
 import java.util.regex.Pattern;
 
-import org.ofbiz.base.util.UtilGenerics;
-import org.ofbiz.base.util.UtilMisc;
-
 /** Miscellaneous Converter classes. */
 public class MiscConverters implements ConverterLoader {
 
-    public static final int CHAR_BUFFER_SIZE = 4096;
-
     public static class BlobToBlob extends AbstractConverter<Blob, Blob> {
         public BlobToBlob() {
             super(Blob.class, Blob.class);
@@ -58,11 +54,28 @@ public class MiscConverters implements C
         }
 
         public byte[] convert(Blob obj) throws ConversionException {
+            InputStream inStream = null;
             try {
-                return obj.getBytes(1, (int) obj.length());
+                inStream = obj.getBinaryStream();
+                int blobLength = (int) obj.length();
+                byte[] byteBuffer = new byte[blobLength];
+                int offset = 0;
+                int bytesRead = inStream.read(byteBuffer, offset, blobLength);
+                while (bytesRead > 0) {
+                    offset += bytesRead;
+                    bytesRead = inStream.read(byteBuffer, offset, blobLength);
+                }
+                return byteBuffer;
             } catch (Exception e) {
                 throw new ConversionException(e);
             }
+            finally {
+                if (inStream != null) {
+                    try {
+                        inStream.close();
+                    } catch (IOException e) {}
+                }
+            }
         }
     }
 
@@ -114,15 +127,18 @@ public class MiscConverters implements C
         }
 
         public String convert(Clob obj) throws ConversionException {
-            StringBuilder strBuf = new StringBuilder();
-            char[] inCharBuffer = new char[CHAR_BUFFER_SIZE];
-            int charsRead = 0;
             Reader clobReader = null;
             try {
-                clobReader =  obj.getCharacterStream();
-                while ((charsRead = clobReader.read(inCharBuffer, 0, CHAR_BUFFER_SIZE)) > 0) {
-                    strBuf.append(inCharBuffer, 0, charsRead);
+                clobReader = obj.getCharacterStream();
+                int clobLength = (int) obj.length();
+                char[] charBuffer = new char[clobLength];
+                int offset = 0;
+                int charsRead = clobReader.read(charBuffer, offset, clobLength);
+                while (charsRead > 0) {
+                    offset += charsRead;
+                    charsRead = clobReader.read(charBuffer, offset, clobLength);
                 }
+                return new String(charBuffer);
             } catch (Exception e) {
                 throw new ConversionException(e);
             }
@@ -133,7 +149,6 @@ public class MiscConverters implements C
                     } catch (IOException e) {}
                 }
             }
-            return strBuf.toString();
         }
     }
 
@@ -166,7 +181,7 @@ public class MiscConverters implements C
 
         public <S, T> Converter<S, T> createConverter(Class<S> sourceClass, Class<T> targetClass) {
             if (String.class == sourceClass && Enum.class.isAssignableFrom(targetClass)) {
-                return UtilGenerics.cast(new StringToEnum());
+                return Util.cast(new StringToEnum());
             } else {
                 return null;
             }
@@ -191,7 +206,7 @@ public class MiscConverters implements C
         }
 
         public E convert(Class<? extends E> targetClass, String obj) throws ConversionException {
-            return Enum.valueOf(UtilGenerics.<Class<E>>cast(targetClass), obj);
+            return Enum.valueOf(Util.<Class<E>>cast(targetClass), obj);
         }
 
         public Class<? super Enum> getTargetClass() {
@@ -229,12 +244,7 @@ public class MiscConverters implements C
         }
 
         public Locale convert(String obj) throws ConversionException {
-            Locale loc = UtilMisc.parseLocale(obj);
-            if (loc != null) {
-                return loc;
-            } else {
-                throw new ConversionException("Could not convert " + obj + " to Locale: ");
-            }
+            return new Locale(obj);
         }
     }
 

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/NetConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/NetConverters.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/NetConverters.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/NetConverters.java Sat Mar 13 13:47:49 2010
@@ -1,137 +1,137 @@
-/*******************************************************************************
- * 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.ofbiz.base.conversion;
-
-import java.io.IOException;
-import java.net.InetAddress;
-import java.net.MalformedURLException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URL;
-
-/** Net Converter classes. */
-public class NetConverters implements ConverterLoader {
-    public static final String module = NetConverters.class.getName();
-
-    public static class StringToInetAddress extends AbstractConverter<String, InetAddress> {
-        public StringToInetAddress() {
-            super(String.class, InetAddress.class);
-        }
-
-        public InetAddress convert(String obj) throws ConversionException {
-            try {
-                return InetAddress.getByName(obj);
-            } catch (IOException e) {
-                throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
-            }
-        }
-    }
-
-    public static class InetAddressToString extends AbstractConverter<InetAddress, String> {
-        public InetAddressToString() {
-            super(InetAddress.class, String.class);
-        }
-
-        public String convert(InetAddress obj) throws ConversionException {
-            String hostName = obj.getHostName();
-            if (hostName != null) return hostName;
-            return obj.getHostAddress();
-        }
-    }
-
-    public static class StringToURI extends AbstractConverter<String, URI> {
-        public StringToURI() {
-            super(String.class, URI.class);
-        }
-
-        public URI convert(String obj) throws ConversionException {
-            try {
-                return new URI(obj);
-            } catch (URISyntaxException e) {
-                throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
-            }
-        }
-    }
-
-    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);
-        }
-
-        public URL convert(String obj) throws ConversionException {
-            try {
-                return new URL(obj);
-            } catch (MalformedURLException e) {
-                throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
-            }
-        }
-    }
-
-    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);
-        }
-
-        public URL convert(URI obj) throws ConversionException {
-            try {
-                return obj.toURL();
-            } catch (MalformedURLException e) {
-                throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
-            }
-        }
-    }
-
-    public static class URLToURI extends AbstractConverter<URL, URI> {
-        public URLToURI() {
-            super(URL.class, URI.class);
-        }
-
-        public URI convert(URL obj) throws ConversionException {
-            try {
-                return obj.toURI();
-            } catch (URISyntaxException e) {
-                throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
-            }
-        }
-    }
-
-    public void loadConverters() {
-        Converters.loadContainedConverters(NetConverters.class);
-    }
-}
+/*******************************************************************************
+ * 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;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+/** Net Converter classes. */
+public class NetConverters implements ConverterLoader {
+    public static final String module = NetConverters.class.getName();
+
+    public static class StringToInetAddress extends AbstractConverter<String, InetAddress> {
+        public StringToInetAddress() {
+            super(String.class, InetAddress.class);
+        }
+
+        public InetAddress convert(String obj) throws ConversionException {
+            try {
+                return InetAddress.getByName(obj);
+            } catch (IOException e) {
+                throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
+            }
+        }
+    }
+
+    public static class InetAddressToString extends AbstractConverter<InetAddress, String> {
+        public InetAddressToString() {
+            super(InetAddress.class, String.class);
+        }
+
+        public String convert(InetAddress obj) throws ConversionException {
+            String hostName = obj.getHostName();
+            if (hostName != null) return hostName;
+            return obj.getHostAddress();
+        }
+    }
+
+    public static class StringToURI extends AbstractConverter<String, URI> {
+        public StringToURI() {
+            super(String.class, URI.class);
+        }
+
+        public URI convert(String obj) throws ConversionException {
+            try {
+                return new URI(obj);
+            } catch (URISyntaxException e) {
+                throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
+            }
+        }
+    }
+
+    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);
+        }
+
+        public URL convert(String obj) throws ConversionException {
+            try {
+                return new URL(obj);
+            } catch (MalformedURLException e) {
+                throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
+            }
+        }
+    }
+
+    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);
+        }
+
+        public URL convert(URI obj) throws ConversionException {
+            try {
+                return obj.toURL();
+            } catch (MalformedURLException e) {
+                throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
+            }
+        }
+    }
+
+    public static class URLToURI extends AbstractConverter<URL, URI> {
+        public URLToURI() {
+            super(URL.class, URI.class);
+        }
+
+        public URI convert(URL obj) throws ConversionException {
+            try {
+                return obj.toURI();
+            } catch (URISyntaxException e) {
+                throw (ConversionException) new ConversionException(e.getMessage()).initCause(e);
+            }
+        }
+    }
+
+    public void loadConverters() {
+        Converters.loadContainedConverters(NetConverters.class);
+    }
+}

Modified: commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/NumberConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/NumberConverters.java?rev=922568&r1=922567&r2=922568&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/NumberConverters.java (original)
+++ commons/sandbox/convert/trunk/src/java/org/apache/commons/convert/NumberConverters.java Sat Mar 13 13:47:49 2010
@@ -16,22 +16,15 @@
  * specific language governing permissions and limitations
  * under the License.
  *******************************************************************************/
-package org.ofbiz.base.conversion;
+package org.apache.commons.convert;
 
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.text.NumberFormat;
 import java.text.ParseException;
-import java.util.List;
 import java.util.Locale;
-import java.util.Set;
 import java.util.TimeZone;
 
-import org.ofbiz.base.util.StringUtil;
-
-import javolution.util.FastList;
-import javolution.util.FastSet;
-
 /** Number Converter classes. */
 public class NumberConverters implements ConverterLoader {
 
@@ -50,11 +43,7 @@ public class NumberConverters implements
         }
 
         public N convert(String obj, Locale locale, TimeZone timeZone) throws ConversionException {
-            String trimStr = StringUtil.removeSpaces(obj);
-            if (trimStr.length() == 0) {
-                return null;
-            }
-            return convert(fromString(trimStr, locale));
+            return convert(fromString(obj, locale));
         }
 
         protected abstract N convert(Number number) throws ConversionException;



Mime
View raw message