You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2009/11/08 01:43:17 UTC
svn commit: r833784 -
/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/
Author: adrianc
Date: Sun Nov 8 00:43:17 2009
New Revision: 833784
URL: http://svn.apache.org/viewvc?rev=833784&view=rev
Log:
The new Java object type conversion framework. It isn't complete, but there is enough there to grasp the general idea.
Added:
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractConverter.java (with props)
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractLocalizedConverter.java (with props)
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/BooleanConverters.java (with props)
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/ConversionException.java (with props)
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converter.java (with props)
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java (with props)
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java (with props)
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/LocalizedConverter.java (with props)
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java (with props)
ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/NumberConverters.java (with props)
Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractConverter.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractConverter.java?rev=833784&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractConverter.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractConverter.java Sun Nov 8 00:43:17 2009
@@ -0,0 +1,36 @@
+/*******************************************************************************
+ * 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 org.ofbiz.base.util.ObjectType;
+
+/** Abstract Converter class. This class handles converter registration
+ * and it implements the <code>canConvert</code> method.
+ */
+public abstract class AbstractConverter<S, T> implements Converter<S, T> {
+
+ public AbstractConverter() {
+ Converters.registerConverter(this);
+ }
+
+ public boolean canConvert(Class<?> sourceClass, Class<?> targetClass) {
+ return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && ObjectType.instanceOf(targetClass, this.getTargetClass());
+ }
+
+}
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractConverter.java
------------------------------------------------------------------------------
svn:keywords = "Date Rev Author URL Id"
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractConverter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractLocalizedConverter.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractLocalizedConverter.java?rev=833784&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractLocalizedConverter.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractLocalizedConverter.java Sun Nov 8 00:43:17 2009
@@ -0,0 +1,36 @@
+/*******************************************************************************
+ * 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 org.ofbiz.base.util.ObjectType;
+
+/** Abstract LocalizedConverter class. This class handles converter registration
+ * and it implements the <code>canConvert</code> method.
+ */
+public abstract class AbstractLocalizedConverter<S, T> implements LocalizedConverter<S, T> {
+
+ public AbstractLocalizedConverter() {
+ Converters.registerConverter(this);
+ }
+
+ public boolean canConvert(Class<?> sourceClass, Class<?> targetClass) {
+ return ObjectType.instanceOf(sourceClass, this.getSourceClass()) && ObjectType.instanceOf(targetClass, this.getTargetClass());
+ }
+
+}
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractLocalizedConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractLocalizedConverter.java
------------------------------------------------------------------------------
svn:keywords = "Date Rev Author URL Id"
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/AbstractLocalizedConverter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/BooleanConverters.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/BooleanConverters.java?rev=833784&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/BooleanConverters.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/BooleanConverters.java Sun Nov 8 00:43:17 2009
@@ -0,0 +1,87 @@
+/*******************************************************************************
+ * 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;
+
+/** Boolean Converter classes. */
+public class BooleanConverters {
+
+ public static class BooleanToInteger extends AbstractConverter<Boolean, Integer> {
+
+ public Integer convert(Boolean obj) throws ConversionException {
+ return obj.booleanValue() ? Integer.valueOf(1) : Integer.valueOf(0);
+ }
+
+ public Class<Boolean> getSourceClass() {
+ return Boolean.class;
+ }
+
+ public Class<Integer> getTargetClass() {
+ return Integer.class;
+ }
+
+ }
+
+ public static class BooleanToString extends AbstractConverter<Boolean, String> {
+
+ public String convert(Boolean obj) throws ConversionException {
+ return obj.booleanValue() ? "true" : "false";
+ }
+
+ public Class<Boolean> getSourceClass() {
+ return Boolean.class;
+ }
+
+ public Class<String> getTargetClass() {
+ return String.class;
+ }
+
+ }
+
+ public static class IntegerToBoolean extends AbstractConverter<Integer, Boolean> {
+
+ public Boolean convert(Integer obj) throws ConversionException {
+ return obj.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
+ }
+
+ public Class<Integer> getSourceClass() {
+ return Integer.class;
+ }
+
+ public Class<Boolean> getTargetClass() {
+ return Boolean.class;
+ }
+
+ }
+
+ public static class StringToBoolean extends AbstractConverter<String, Boolean> {
+
+ public Boolean convert(String obj) throws ConversionException {
+ return "TRUE".equals(obj.toUpperCase());
+ }
+
+ public Class<String> getSourceClass() {
+ return String.class;
+ }
+
+ public Class<Boolean> getTargetClass() {
+ return Boolean.class;
+ }
+
+ }
+}
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/BooleanConverters.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/BooleanConverters.java
------------------------------------------------------------------------------
svn:keywords = "Date Rev Author URL Id"
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/BooleanConverters.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/ConversionException.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/ConversionException.java?rev=833784&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/ConversionException.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/ConversionException.java Sun Nov 8 00:43:17 2009
@@ -0,0 +1,37 @@
+/*******************************************************************************
+ * 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;
+
+/** ConversionException class. */
+@SuppressWarnings("serial")
+public class ConversionException extends Exception {
+
+ public ConversionException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public ConversionException(String message) {
+ super(message);
+ }
+
+ public ConversionException(Throwable cause) {
+ super(cause);
+ }
+
+}
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/ConversionException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/ConversionException.java
------------------------------------------------------------------------------
svn:keywords = "Date Rev Author URL Id"
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/ConversionException.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converter.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converter.java?rev=833784&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converter.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converter.java Sun Nov 8 00:43:17 2009
@@ -0,0 +1,60 @@
+/*******************************************************************************
+ * 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;
+
+/** Converter interface.
+ *
+ * @param <S> The source object type
+ * @param <T> The target object type
+ */
+public interface Converter<S, T> {
+
+ /** Returns <code>true</code> if this object can convert
+ * <code>sourceClass</code> to <code>targetClass</code>.
+ * <p>Implementations can accomodate class hierarchy ranges
+ * by converting super classes or interfaces.</p>
+ *
+ * @param sourceClass The source <code>Class</code>
+ * @param targetClass The target <code>Class</code>
+ * @return <code>true</code> if this object can convert
+ * <code>sourceClass</code> to <code>targetClass</code>.
+ */
+ public boolean canConvert(Class<?> sourceClass, Class<?> targetClass);
+
+ /** Converts <code>obj</code> to <code>T</code>.
+ *
+ * @param obj The source <code>Object</code> to convert
+ * @return The converted <code>Object</code>
+ * @throws ConversionException
+ */
+ public T convert(S obj) throws ConversionException;
+
+ /** Returns the source <code>Class</code> for this converter.
+ *
+ * @return The source <code>Class</code> for this converter
+ */
+ public Class<?> getSourceClass();
+
+ /** Returns the target <code>Class</code> for this converter.
+ *
+ * @return The target <code>Class</code> for this converter
+ */
+ public Class<?> getTargetClass();
+
+}
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converter.java
------------------------------------------------------------------------------
svn:keywords = "Date Rev Author URL Id"
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java?rev=833784&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java Sun Nov 8 00:43:17 2009
@@ -0,0 +1,233 @@
+/*******************************************************************************
+ * 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.math.BigDecimal;
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.TimeZone;
+
+import javolution.util.FastMap;
+import javolution.util.FastSet;
+
+import org.ofbiz.base.conversion.BooleanConverters.*;
+import org.ofbiz.base.conversion.NumberConverters.*;
+import org.ofbiz.base.conversion.DateTimeConverters.*;
+import org.ofbiz.base.conversion.MiscConverters.*;
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.base.util.ObjectType;
+import org.ofbiz.base.util.TimeDuration;
+
+import com.ibm.icu.util.Calendar;
+
+/** 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 Map<String, Converter<?, ?>> converterMap = FastMap.newInstance();
+ protected static final Set<String> noConversions = FastSet.newInstance();
+ protected static final Converter<Object, Object> nullConverter = new NullConverter();
+
+ public static final Converter<BigDecimal, Double> BigDecimalToDouble = new BigDecimalToDouble();
+ public static final Converter<BigDecimal, Float> BigDecimalToFloat = new BigDecimalToFloat();
+ public static final Converter<BigDecimal, Integer> BigDecimalToInteger = new BigDecimalToInteger();
+ public static final Converter<BigDecimal, List<BigDecimal>> BigDecimalToList = new BigDecimalToList();
+ public static final Converter<BigDecimal, Long> BigDecimalToLong = new BigDecimalToLong();
+ public static final Converter<BigDecimal, Set<BigDecimal>> BigDecimalToSet = new BigDecimalToSet();
+ public static final LocalizedConverter<BigDecimal, String> BigDecimalToString = new BigDecimalToString();
+ public static final Converter<Boolean, Integer> BooleanToInteger = new BooleanToInteger();
+ public static final Converter<Boolean, String> BooleanToString = new BooleanToString();
+ public static final Converter<Calendar, Long> CalendarToLong = new CalendarToLong();
+ public static final Converter<Calendar, String> CalendarToString = new CalendarToString();
+ public static final Converter<java.util.Date, Long> DateToLong = new DateToLong();
+ public static final LocalizedConverter<java.util.Date, String> DateToString = new DateToString();
+ public static final Converter<Double, BigDecimal> DoubleToBigDecimal = new DoubleToBigDecimal();
+ public static final Converter<Double, Float> DoubleToFloat = new DoubleToFloat();
+ public static final Converter<Double, Integer> DoubleToInteger = new DoubleToInteger();
+ public static final Converter<Double, List<Double>> DoubleToList = new DoubleToList();
+ public static final Converter<Double, Long> DoubleToLong = new DoubleToLong();
+ public static final Converter<Double, Set<Double>> DoubleToSet = new DoubleToSet();
+ public static final LocalizedConverter<Double, String> DoubleToString = new DoubleToString();
+ public static final Converter<TimeDuration, String> DurationToString = new DurationToString();
+ public static final Converter<Float, BigDecimal> FloatToBigDecimal = new FloatToBigDecimal();
+ public static final Converter<Float, Double> FloatToDouble = new FloatToDouble();
+ public static final Converter<Float, Integer> FloatToInteger = new FloatToInteger();
+ public static final Converter<Float, List<Float>> FloatToList = new FloatToList();
+ public static final Converter<Float, Long> FloatToLong = new FloatToLong();
+ public static final Converter<Float, Set<Float>> FloatToSet = new FloatToSet();
+ public static final LocalizedConverter<Float, String> FloatToString = new FloatToString();
+ public static final Converter<Integer, BigDecimal> IntegerToBigDecimal = new IntegerToBigDecimal();
+ public static final Converter<Integer, Boolean> IntegerToBoolean = new IntegerToBoolean();
+ public static final Converter<Integer, Double> IntegerToDouble = new IntegerToDouble();
+ public static final Converter<Integer, Float> IntegerToFloat = new IntegerToFloat();
+ public static final Converter<Integer, List<Integer>> IntegerToList = new IntegerToList();
+ public static final Converter<Integer, Long> IntegerToLong = new IntegerToLong();
+ public static final Converter<Integer, Set<Integer>> IntegerToSet = new IntegerToSet();
+ public static final LocalizedConverter<Integer, String> IntegerToString = new IntegerToString();
+ public static final Converter<Locale, String> LocaleToString = new LocaleToString();
+ public static final Converter<Long, BigDecimal> LongToBigDecimal = new LongToBigDecimal();
+ public static final Converter<Long, Calendar> LongToCalendar = new LongToCalendar();
+ public static final Converter<Long, Double> LongToDouble = new LongToDouble();
+ public static final Converter<Long, Float> LongToFloat = new LongToFloat();
+ public static final Converter<Long, Integer> LongToInteger = new LongToInteger();
+ public static final Converter<Long, List<Long>> LongToList = new LongToList();
+ public static final Converter<Long, Set<Long>> LongToSet = new LongToSet();
+ public static final LocalizedConverter<Long, String> LongToString = new LongToString();
+ public static final Converter<Number, java.util.Date> NumberToDate = new NumberToDate();
+ public static final Converter<Number, TimeDuration> NumberToDuration = new NumberToDuration();
+ public static final Converter<Number, java.sql.Date> NumberToSqlDate = new NumberToSqlDate();
+ public static final Converter<Number, java.sql.Time> NumberToSqlTime = new NumberToSqlTime();
+ public static final Converter<Number, java.sql.Timestamp> NumberToTimestamp = new NumberToTimestamp();
+ public static final LocalizedConverter<java.sql.Date, String> SqlDateToString = new SqlDateToString();
+ public static final LocalizedConverter<java.sql.Time, String> SqlTimeToString = new SqlTimeToString();
+ public static final LocalizedConverter<String, BigDecimal> StringToBigDecimal = new StringToBigDecimal();
+ public static final Converter<String, Boolean> StringToBoolean = new StringToBoolean();
+ public static final LocalizedConverter<String, Calendar> StringToCalendar = new StringToCalendar();
+ public static final LocalizedConverter<String, java.util.Date> StringToDate = new StringToDate();
+ public static final LocalizedConverter<String, Double> StringToDouble = new StringToDouble();
+ public static final Converter<String, TimeDuration> StringToDuration = new StringToDuration();
+ public static final LocalizedConverter<String, Float> StringToFloat = new StringToFloat();
+ public static final LocalizedConverter<String, Integer> StringToInteger = new StringToInteger();
+ public static final Converter<String, Locale> StringToLocale = new StringToLocale();
+ public static final LocalizedConverter<String, Long> StringToLong = new StringToLong();
+ public static final LocalizedConverter<String, java.sql.Date> StringToSqlDate = new StringToSqlDate();
+ public static final LocalizedConverter<String, java.sql.Time> StringToSqlTime = new StringToSqlTime();
+ public static final LocalizedConverter<String, java.sql.Timestamp> StringToTimestamp = new StringToTimestamp();
+ public static final Converter<String, TimeZone> StringToTimeZone = new StringToTimeZone();
+ public static final Converter<TimeZone, String> TimeZoneToString = new TimeZoneToString();
+
+ /** Returns an appropriate <code>Converter</code> instance for
+ * <code>sourceClass</code> and <code>targetClass</code>. If no matching
+ * <code>Converter</code> is found, the method throws
+ * <code>ClassNotFoundException</code>.
+ *
+ * <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>
+ *
+ * @param sourceClass The object class to convert from
+ * @param targetClass The object class to convert to
+ * @return A matching <code>Converter</code> instance
+ * @throws ClassNotFoundException
+ */
+ @SuppressWarnings("unchecked")
+ 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());
+ Converter<?, ?> result = converterMap.get(key);
+ if (result == null) {
+ if (!noConversions.contains(key)) {
+ synchronized (converterMap) {
+ Collection<Converter<?, ?>> values = converterMap.values();
+ for (Converter<?, ?> value : values) {
+ if (value.canConvert(sourceClass, targetClass)) {
+ converterMap.put(key, value);
+ return (Converter<S, T>) value;
+ }
+ }
+ 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 developed. ***", module);
+ }
+ }
+ throw new ClassNotFoundException("No converter found for " + key);
+ }
+ return (Converter<S, T>) result;
+ }
+
+ /** Registers a <code>Converter</code> instance to be used by the
+ * {@link org.ofbiz.base.conversion.Converters#getConverter(Class, Class)}
+ * method.
+ *
+ * @param <S> The source object type
+ * @param <T> The target object type
+ * @param converter The <code>Converter</code> instance to register
+ */
+ public static <S, T> void registerConverter(Converter<S, T> converter) {
+ String key = converter.getSourceClass().getName().concat(DELIMITER).concat(converter.getTargetClass().getName());
+ synchronized (converterMap) {
+ converterMap.put(key, converter);
+ }
+ if (Debug.verboseOn()) {
+ Debug.logVerbose("Registered converter " + converter.getClass().getName(), module);
+ }
+ }
+
+ protected static class NullConverter implements Converter<Object, Object> {
+
+ public NullConverter() {
+ Converters.registerConverter(this);
+ }
+
+ 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 Class<?> getSourceClass() {
+ return Object.class;
+ }
+
+ public Class<?> getTargetClass() {
+ return Object.class;
+ }
+
+ }
+
+/*
+ public static String mapSize() {
+ return "Map size = " + converterMap.size();
+ }
+
+ public static String runPerformanceTest() {
+ @SuppressWarnings("unused")
+ Object obj = null;
+ try {
+ Converter<BigDecimal, Integer> converter = Converters.getConverter(BigDecimal.class, Integer.class);
+ obj = converter.convert(BigDecimal.ONE);
+ } catch (Exception e) {}
+ System.gc();
+ long newStart = System.currentTimeMillis();
+ try {
+ for (int i = 0; i < 1000000; i++) {
+ Converter<BigDecimal, Integer> converter = Converters.getConverter(BigDecimal.class, Integer.class);
+ obj = converter.convert(BigDecimal.ONE);
+ }
+ } catch (Exception e) {
+ Debug.logError(e, module);
+ }
+ long newStop = System.currentTimeMillis();
+ Debug.logInfo("Elasped time = " + (newStop - newStart), module);
+ return "Elasped time = " + (newStop - newStart);
+ }
+
+*/
+}
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java
------------------------------------------------------------------------------
svn:keywords = "Date Rev Author URL Id"
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/Converters.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java?rev=833784&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java Sun Nov 8 00:43:17 2009
@@ -0,0 +1,475 @@
+/*******************************************************************************
+ * 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.text.DateFormat;
+import java.text.ParseException;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import org.ofbiz.base.util.TimeDuration;
+import org.ofbiz.base.util.UtilDateTime;
+
+import com.ibm.icu.util.Calendar;
+
+/** Date/time Converter classes. */
+public class DateTimeConverters {
+
+ public static class CalendarToLong extends AbstractConverter<Calendar, Long> {
+
+ public Long convert(Calendar obj) throws ConversionException {
+ return obj.getTimeInMillis();
+ }
+
+ public Class<Calendar> getSourceClass() {
+ return Calendar.class;
+ }
+
+ public Class<Long> getTargetClass() {
+ return Long.class;
+ }
+
+ }
+
+ public static class CalendarToString extends AbstractConverter<Calendar, String> {
+
+ 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);
+ }
+
+ public Class<Calendar> getSourceClass() {
+ return Calendar.class;
+ }
+
+ public Class<String> getTargetClass() {
+ return String.class;
+ }
+
+ }
+
+ public static class DateToLong extends AbstractConverter<java.util.Date, Long> {
+
+ public Long convert(java.util.Date obj) throws ConversionException {
+ return obj.getTime();
+ }
+
+ public Class<java.util.Date> getSourceClass() {
+ return java.util.Date.class;
+ }
+
+ public Class<Long> getTargetClass() {
+ return Long.class;
+ }
+
+ }
+
+ public static class DateToString extends GenericLocalizedConverter<java.util.Date, String> {
+
+ public String convert(java.util.Date obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+ DateFormat df = null;
+ if (formatString == null || formatString.length() == 0) {
+ df = UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, locale);
+ } else {
+ df = UtilDateTime.toDateTimeFormat(formatString, timeZone, locale);
+ }
+ return df.format(obj);
+ }
+
+ public Class<java.util.Date> getSourceClass() {
+ return java.util.Date.class;
+ }
+
+ public Class<String> getTargetClass() {
+ return String.class;
+ }
+
+ }
+
+ public static class DurationToString extends AbstractConverter<TimeDuration, String> {
+
+ public String convert(TimeDuration obj) throws ConversionException {
+ return obj.toString();
+ }
+
+ public Class<TimeDuration> getSourceClass() {
+ return TimeDuration.class;
+ }
+
+ public Class<String> getTargetClass() {
+ return String.class;
+ }
+
+ }
+
+ public static abstract class GenericLocalizedConverter<S, T> extends AbstractLocalizedConverter<S, T> {
+
+ public T convert(S obj) throws ConversionException {
+ return convert(obj, Locale.getDefault(), TimeZone.getDefault(), null);
+ }
+
+ public T convert(S obj, Locale locale, TimeZone timeZone) throws ConversionException {
+ return convert(obj, locale, timeZone, null);
+ }
+
+ }
+
+ public static class LongToCalendar extends AbstractLocalizedConverter<Long, Calendar> {
+
+ public Calendar convert(Long obj) throws ConversionException {
+ return convert(obj, Locale.getDefault(), TimeZone.getDefault());
+ }
+
+ public Calendar convert(Long obj, Locale locale, TimeZone timeZone) throws ConversionException {
+ return UtilDateTime.toCalendar(new java.util.Date(obj.longValue()), timeZone, locale);
+ }
+
+ public Calendar convert(Long obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+ return convert(obj, Locale.getDefault(), TimeZone.getDefault());
+ }
+
+ public Class<Long> getSourceClass() {
+ return Long.class;
+ }
+
+ public Class<Calendar> getTargetClass() {
+ return Calendar.class;
+ }
+
+ }
+
+ public static class NumberToDate extends AbstractConverter<Number, java.util.Date> {
+
+ public java.util.Date convert(Number obj) throws ConversionException {
+ return new java.util.Date(obj.longValue());
+ }
+
+ public Class<Number> getSourceClass() {
+ return Number.class;
+ }
+
+ public Class<java.util.Date> getTargetClass() {
+ return java.util.Date.class;
+ }
+
+ }
+
+ public static class NumberToDuration extends AbstractConverter<Number, TimeDuration> {
+
+ public TimeDuration convert(Number obj) throws ConversionException {
+ return TimeDuration.fromNumber(obj);
+ }
+
+ public Class<Number> getSourceClass() {
+ return Number.class;
+ }
+
+ public Class<TimeDuration> getTargetClass() {
+ return TimeDuration.class;
+ }
+
+ }
+
+ public static class NumberToSqlDate extends AbstractConverter<Number, java.sql.Date> {
+
+ public java.sql.Date convert(Number obj) throws ConversionException {
+ return new java.sql.Date(obj.longValue());
+ }
+
+ public Class<Number> getSourceClass() {
+ return Number.class;
+ }
+
+ public Class<java.sql.Date> getTargetClass() {
+ return java.sql.Date.class;
+ }
+
+ }
+
+ public static class NumberToSqlTime extends AbstractConverter<Number, java.sql.Time> {
+
+ public java.sql.Time convert(Number obj) throws ConversionException {
+ return new java.sql.Time(obj.longValue());
+ }
+
+ public Class<Number> getSourceClass() {
+ return Number.class;
+ }
+
+ public Class<java.sql.Time> getTargetClass() {
+ return java.sql.Time.class;
+ }
+
+ }
+
+ public static class NumberToTimestamp extends AbstractConverter<Number, java.sql.Timestamp> {
+
+ public java.sql.Timestamp convert(Number obj) throws ConversionException {
+ return new java.sql.Timestamp(obj.longValue());
+ }
+
+ public Class<Number> getSourceClass() {
+ return Number.class;
+ }
+
+ public Class<java.sql.Timestamp> getTargetClass() {
+ return java.sql.Timestamp.class;
+ }
+
+ }
+
+ public static class SqlDateToString extends GenericLocalizedConverter<java.sql.Date, String> {
+
+ public String convert(java.sql.Date obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+ DateFormat df = null;
+ if (formatString == null || formatString.length() == 0) {
+ df = UtilDateTime.toDateFormat(UtilDateTime.DATE_FORMAT, timeZone, locale);
+ } else {
+ df = UtilDateTime.toDateFormat(formatString, timeZone, locale);
+ }
+ return df.format(obj);
+ }
+
+ public Class<java.sql.Date> getSourceClass() {
+ return java.sql.Date.class;
+ }
+
+ public Class<String> getTargetClass() {
+ return String.class;
+ }
+
+ }
+
+ public static class SqlTimeToString extends GenericLocalizedConverter<java.sql.Time, String> {
+
+ public String convert(java.sql.Time obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+ DateFormat df = null;
+ if (formatString == null || formatString.length() == 0) {
+ df = UtilDateTime.toTimeFormat(UtilDateTime.TIME_FORMAT, timeZone, locale);
+ } else {
+ df = UtilDateTime.toTimeFormat(formatString, timeZone, locale);
+ }
+ return df.format(obj);
+ }
+
+ public Class<java.sql.Time> getSourceClass() {
+ return java.sql.Time.class;
+ }
+
+ public Class<String> getTargetClass() {
+ return String.class;
+ }
+
+ }
+
+ public static class StringToCalendar extends AbstractLocalizedConverter<String, Calendar> {
+
+ public Calendar convert(String obj) throws ConversionException {
+ return convert(obj, Locale.getDefault(), TimeZone.getDefault(), null);
+ }
+
+ public Calendar convert(String obj, Locale locale, TimeZone timeZone) throws ConversionException {
+ return convert(obj, Locale.getDefault(), TimeZone.getDefault(), null);
+ }
+
+ public Calendar convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+ DateFormat df = null;
+ if (formatString == null || formatString.length() == 0) {
+ df = UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, locale);
+ } else {
+ df = UtilDateTime.toDateTimeFormat(formatString, timeZone, locale);
+ }
+ try {
+ java.util.Date date = df.parse(obj);
+ return UtilDateTime.toCalendar(date, timeZone, locale);
+ } catch (ParseException e) {
+ throw new ConversionException(e);
+ }
+ }
+
+ public Class<String> getSourceClass() {
+ return String.class;
+ }
+
+ public Class<Calendar> getTargetClass() {
+ return Calendar.class;
+ }
+
+ }
+
+ public static class StringToDate extends GenericLocalizedConverter<String, java.util.Date> {
+
+ public java.util.Date convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+ DateFormat df = null;
+ if (formatString == null || formatString.length() == 0) {
+ df = UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, locale);
+ } else {
+ df = UtilDateTime.toDateTimeFormat(formatString, timeZone, locale);
+ }
+ try {
+ return df.parse(obj);
+ } catch (ParseException e) {
+ throw new ConversionException(e);
+ }
+ }
+
+ public Class<String> getSourceClass() {
+ return String.class;
+ }
+
+ public Class<java.util.Date> getTargetClass() {
+ return java.util.Date.class;
+ }
+
+ }
+
+ public static class StringToDuration extends AbstractConverter<String, TimeDuration> {
+
+ public TimeDuration convert(String obj) throws ConversionException {
+ return TimeDuration.parseDuration(obj);
+ }
+
+ public Class<String> getSourceClass() {
+ return String.class;
+ }
+
+ public Class<TimeDuration> getTargetClass() {
+ return TimeDuration.class;
+ }
+
+ }
+
+ public static class StringToSqlDate extends GenericLocalizedConverter<String, java.sql.Date> {
+
+ public java.sql.Date convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+ DateFormat df = null;
+ if (formatString == null || formatString.length() == 0) {
+ df = UtilDateTime.toDateFormat(UtilDateTime.DATE_FORMAT, timeZone, locale);
+ } else {
+ df = UtilDateTime.toDateFormat(formatString, timeZone, locale);
+ }
+ try {
+ return new java.sql.Date(df.parse(obj).getTime());
+ } catch (ParseException e) {
+ throw new ConversionException(e);
+ }
+ }
+
+ public Class<String> getSourceClass() {
+ return String.class;
+ }
+
+ public Class<java.sql.Date> getTargetClass() {
+ return java.sql.Date.class;
+ }
+
+ }
+
+ public static class StringToSqlTime extends GenericLocalizedConverter<String, java.sql.Time> {
+
+ public java.sql.Time convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+ DateFormat df = null;
+ if (formatString == null || formatString.length() == 0) {
+ df = UtilDateTime.toTimeFormat(UtilDateTime.TIME_FORMAT, timeZone, locale);
+ } else {
+ df = UtilDateTime.toTimeFormat(formatString, timeZone, locale);
+ }
+ try {
+ return new java.sql.Time(df.parse(obj).getTime());
+ } catch (ParseException e) {
+ throw new ConversionException(e);
+ }
+ }
+
+ public Class<String> getSourceClass() {
+ return String.class;
+ }
+
+ public Class<java.sql.Time> getTargetClass() {
+ return java.sql.Time.class;
+ }
+
+ }
+
+ public static class StringToTimestamp extends GenericLocalizedConverter<String, java.sql.Timestamp> {
+
+ public java.sql.Timestamp convert(String obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+ DateFormat df = null;
+ if (formatString == null || formatString.length() == 0) {
+ df = UtilDateTime.toDateTimeFormat(UtilDateTime.DATE_TIME_FORMAT, timeZone, locale);
+ } else {
+ df = UtilDateTime.toDateTimeFormat(formatString, timeZone, locale);
+ }
+ try {
+ return new java.sql.Timestamp(df.parse(obj).getTime());
+ } catch (ParseException e) {
+ throw new ConversionException(e);
+ }
+ }
+
+ public Class<String> getSourceClass() {
+ return String.class;
+ }
+
+ public Class<java.sql.Timestamp> getTargetClass() {
+ return java.sql.Timestamp.class;
+ }
+
+ }
+
+ public static class StringToTimeZone extends AbstractConverter<String, TimeZone> {
+
+ 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: ");
+ }
+ }
+
+ public Class<String> getSourceClass() {
+ return String.class;
+ }
+
+ public Class<TimeZone> getTargetClass() {
+ return TimeZone.class;
+ }
+
+ }
+
+ public static class TimeZoneToString extends AbstractConverter<TimeZone, String> {
+
+ public String convert(TimeZone obj) throws ConversionException {
+ return obj.getID();
+ }
+
+ public Class<TimeZone> getSourceClass() {
+ return TimeZone.class;
+ }
+
+ public Class<String> getTargetClass() {
+ return String.class;
+ }
+
+ }
+
+}
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java
------------------------------------------------------------------------------
svn:keywords = "Date Rev Author URL Id"
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/DateTimeConverters.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/LocalizedConverter.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/LocalizedConverter.java?rev=833784&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/LocalizedConverter.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/LocalizedConverter.java Sun Nov 8 00:43:17 2009
@@ -0,0 +1,48 @@
+/*******************************************************************************
+ * 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.Locale;
+import java.util.TimeZone;
+
+/** Localized converter interface. */
+public interface LocalizedConverter<S, T> extends Converter<S, T> {
+
+ /** Converts <code>obj</code> to <code>T</code>.
+ *
+ * @param obj The source <code>Object</code> to convert
+ * @param locale The locale used for conversion - must not be <code>null</code>
+ * @param timeZone The time zone used for conversion - must not be <code>null</code>
+ * @return The converted <code>Object</code>
+ * @throws ConversionException
+ */
+ public T convert(S obj, Locale locale, TimeZone timeZone) throws ConversionException;
+
+ /** Converts <code>obj</code> to <code>T</code>.
+ *
+ * @param obj The source <code>Object</code> to convert
+ * @param locale The locale used for conversion - must not be <code>null</code>
+ * @param timeZone The time zone used for conversion - must not be <code>null</code>
+ * @param formatString Optional formatting string
+ * @return The converted <code>Object</code>
+ * @throws ConversionException
+ */
+ public T convert(S obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException;
+
+}
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/LocalizedConverter.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/LocalizedConverter.java
------------------------------------------------------------------------------
svn:keywords = "Date Rev Author URL Id"
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/LocalizedConverter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java?rev=833784&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java Sun Nov 8 00:43:17 2009
@@ -0,0 +1,65 @@
+/*******************************************************************************
+ * 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.Locale;
+
+import org.ofbiz.base.util.UtilMisc;
+
+/** Miscellaneous Converter classes. */
+public class MiscConverters {
+
+ public static class LocaleToString extends AbstractConverter<Locale, String> {
+
+ public String convert(Locale obj) throws ConversionException {
+ return obj.toString();
+ }
+
+ public Class<Locale> getSourceClass() {
+ return Locale.class;
+ }
+
+ public Class<String> getTargetClass() {
+ return String.class;
+ }
+
+ }
+
+ public static class StringToLocale extends AbstractConverter<String, Locale> {
+
+ 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: ");
+ }
+ }
+
+ public Class<String> getSourceClass() {
+ return String.class;
+ }
+
+ public Class<Locale> getTargetClass() {
+ return Locale.class;
+ }
+
+ }
+
+}
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
------------------------------------------------------------------------------
svn:keywords = "Date Rev Author URL Id"
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/MiscConverters.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/NumberConverters.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/NumberConverters.java?rev=833784&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/NumberConverters.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/NumberConverters.java Sun Nov 8 00:43:17 2009
@@ -0,0 +1,725 @@
+/*******************************************************************************
+ * 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.math.BigDecimal;
+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 javolution.util.FastList;
+import javolution.util.FastSet;
+
+/** Number Converter classes. */
+public class NumberConverters {
+
+ public static abstract class AbstractToNumberConverter<S, T> extends AbstractUsesLocaleConverter<S, T> {
+
+ protected Number fromString(String str, Locale locale) throws ConversionException {
+ NumberFormat nf = NumberFormat.getNumberInstance(locale);
+ try {
+ return nf.parse(str);
+ } catch (ParseException e) {
+ throw new ConversionException(e);
+ }
+ }
+
+ }
+
+ public static abstract class AbstractUsesLocaleConverter<S, T> extends AbstractLocalizedConverter<S, T> {
+
+ public T convert(S obj) throws ConversionException {
+ return convert(obj, Locale.getDefault(), null);
+ }
+
+ public T convert(S obj, Locale locale, TimeZone timeZone, String formatString) throws ConversionException {
+ return convert(obj, Locale.getDefault(), null);
+ }
+
+
+ }
+
+ public static class BigDecimalToDouble extends AbstractConverter<BigDecimal, Double> {
+
+ public Double convert(BigDecimal obj) throws ConversionException {
+ return Double.valueOf(obj.doubleValue());
+ }
+
+ public Class<BigDecimal> getSourceClass() {
+ return BigDecimal.class;
+ }
+
+ public Class<Double> getTargetClass() {
+ return Double.class;
+ }
+
+ }
+
+ public static class BigDecimalToFloat extends AbstractConverter<BigDecimal, Float> {
+
+ public Float convert(BigDecimal obj) throws ConversionException {
+ return Float.valueOf(obj.floatValue());
+ }
+
+ public Class<BigDecimal> getSourceClass() {
+ return BigDecimal.class;
+ }
+
+ public Class<Float> getTargetClass() {
+ return Float.class;
+ }
+
+ }
+
+ public static class BigDecimalToInteger extends AbstractConverter<BigDecimal, Integer> {
+
+ public Integer convert(BigDecimal obj) throws ConversionException {
+ return Integer.valueOf(obj.intValue());
+ }
+
+ public Class<BigDecimal> getSourceClass() {
+ return BigDecimal.class;
+ }
+
+ public Class<Integer> getTargetClass() {
+ return Integer.class;
+ }
+
+ }
+
+ public static class BigDecimalToList extends AbstractConverter<BigDecimal, List<BigDecimal>> {
+
+ public List<BigDecimal> convert(BigDecimal obj) throws ConversionException {
+ List<BigDecimal> tempList = FastList.newInstance();
+ tempList.add(obj);
+ return tempList;
+ }
+
+ public Class<BigDecimal> getSourceClass() {
+ return BigDecimal.class;
+ }
+
+ public Class<?> getTargetClass() {
+ return List.class;
+ }
+
+ }
+
+ public static class BigDecimalToLong extends AbstractConverter<BigDecimal, Long> {
+
+ public Long convert(BigDecimal obj) throws ConversionException {
+ return Long.valueOf(obj.longValue());
+ }
+
+ public Class<BigDecimal> getSourceClass() {
+ return BigDecimal.class;
+ }
+
+ public Class<Long> getTargetClass() {
+ return Long.class;
+ }
+
+ }
+
+ public static class BigDecimalToSet extends AbstractConverter<BigDecimal, Set<BigDecimal>> {
+
+ public Set<BigDecimal> convert(BigDecimal obj) throws ConversionException {
+ Set<BigDecimal> tempSet = FastSet.newInstance();
+ tempSet.add(obj);
+ return tempSet;
+ }
+
+ public Class<BigDecimal> getSourceClass() {
+ return BigDecimal.class;
+ }
+
+ public Class<?> getTargetClass() {
+ return Set.class;
+ }
+
+ }
+
+ public static class BigDecimalToString extends AbstractUsesLocaleConverter<BigDecimal, String> {
+
+ public String convert(BigDecimal obj, Locale locale, TimeZone timeZone) throws ConversionException {
+ NumberFormat nf = NumberFormat.getNumberInstance(locale);
+ return nf.format(obj.doubleValue());
+ }
+
+ public Class<BigDecimal> getSourceClass() {
+ return BigDecimal.class;
+ }
+
+ public Class<String> getTargetClass() {
+ return String.class;
+ }
+
+ }
+
+ public static class DoubleToBigDecimal extends AbstractConverter<Double, BigDecimal> {
+
+ public BigDecimal convert(Double obj) throws ConversionException {
+ return BigDecimal.valueOf(obj.doubleValue());
+ }
+
+ public Class<Double> getSourceClass() {
+ return Double.class;
+ }
+
+ public Class<BigDecimal> getTargetClass() {
+ return BigDecimal.class;
+ }
+
+ }
+
+ public static class DoubleToFloat extends AbstractConverter<Double, Float> {
+
+ public Float convert(Double obj) throws ConversionException {
+ return Float.valueOf(obj.floatValue());
+ }
+
+ public Class<Double> getSourceClass() {
+ return Double.class;
+ }
+
+ public Class<Float> getTargetClass() {
+ return Float.class;
+ }
+
+ }
+
+ public static class DoubleToInteger extends AbstractConverter<Double, Integer> {
+
+ public Integer convert(Double obj) throws ConversionException {
+ return Integer.valueOf(obj.intValue());
+ }
+
+ public Class<Double> getSourceClass() {
+ return Double.class;
+ }
+
+ public Class<Integer> getTargetClass() {
+ return Integer.class;
+ }
+
+ }
+
+ public static class DoubleToList extends AbstractConverter<Double, List<Double>> {
+
+ public List<Double> convert(Double obj) throws ConversionException {
+ List<Double> tempList = FastList.newInstance();
+ tempList.add(obj);
+ return tempList;
+ }
+
+ public Class<Double> getSourceClass() {
+ return Double.class;
+ }
+
+ public Class<?> getTargetClass() {
+ return List.class;
+ }
+
+ }
+
+ public static class DoubleToLong extends AbstractConverter<Double, Long> {
+
+ public Long convert(Double obj) throws ConversionException {
+ return Long.valueOf(obj.longValue());
+ }
+
+ public Class<Double> getSourceClass() {
+ return Double.class;
+ }
+
+ public Class<Long> getTargetClass() {
+ return Long.class;
+ }
+
+ }
+
+ public static class DoubleToSet extends AbstractConverter<Double, Set<Double>> {
+
+ public Set<Double> convert(Double obj) throws ConversionException {
+ Set<Double> tempSet = FastSet.newInstance();
+ tempSet.add(obj);
+ return tempSet;
+ }
+
+ public Class<Double> getSourceClass() {
+ return Double.class;
+ }
+
+ public Class<?> getTargetClass() {
+ return Set.class;
+ }
+
+ }
+
+ public static class DoubleToString extends AbstractUsesLocaleConverter<Double, String> {
+
+ public String convert(Double obj, Locale locale, TimeZone timeZone) throws ConversionException {
+ NumberFormat nf = NumberFormat.getNumberInstance(locale);
+ return nf.format(obj.doubleValue());
+ }
+
+ public Class<Double> getSourceClass() {
+ return Double.class;
+ }
+
+ public Class<String> getTargetClass() {
+ return String.class;
+ }
+
+ }
+
+ public static class FloatToBigDecimal extends AbstractConverter<Float, BigDecimal> {
+
+ public BigDecimal convert(Float obj) throws ConversionException {
+ return BigDecimal.valueOf(obj.doubleValue());
+ }
+
+ public Class<Float> getSourceClass() {
+ return Float.class;
+ }
+
+ public Class<BigDecimal> getTargetClass() {
+ return BigDecimal.class;
+ }
+
+ }
+
+ public static class FloatToDouble extends AbstractConverter<Float, Double> {
+
+ public Double convert(Float obj) throws ConversionException {
+ return Double.valueOf(obj.doubleValue());
+ }
+
+ public Class<Float> getSourceClass() {
+ return Float.class;
+ }
+
+ public Class<Double> getTargetClass() {
+ return Double.class;
+ }
+
+ }
+
+ public static class FloatToInteger extends AbstractConverter<Float, Integer> {
+
+ public Integer convert(Float obj) throws ConversionException {
+ return Integer.valueOf(obj.intValue());
+ }
+
+ public Class<Float> getSourceClass() {
+ return Float.class;
+ }
+
+ public Class<Integer> getTargetClass() {
+ return Integer.class;
+ }
+
+ }
+
+ public static class FloatToList extends AbstractConverter<Float, List<Float>> {
+
+ public List<Float> convert(Float obj) throws ConversionException {
+ List<Float> tempList = FastList.newInstance();
+ tempList.add(obj);
+ return tempList;
+ }
+
+ public Class<Float> getSourceClass() {
+ return Float.class;
+ }
+
+ public Class<?> getTargetClass() {
+ return List.class;
+ }
+
+ }
+
+ public static class FloatToLong extends AbstractConverter<Float, Long> {
+
+ public Long convert(Float obj) throws ConversionException {
+ return Long.valueOf(obj.longValue());
+ }
+
+ public Class<Float> getSourceClass() {
+ return Float.class;
+ }
+
+ public Class<Long> getTargetClass() {
+ return Long.class;
+ }
+
+ }
+
+ public static class FloatToSet extends AbstractConverter<Float, Set<Float>> {
+
+ public Set<Float> convert(Float obj) throws ConversionException {
+ Set<Float> tempSet = FastSet.newInstance();
+ tempSet.add(obj);
+ return tempSet;
+ }
+
+ public Class<Float> getSourceClass() {
+ return Float.class;
+ }
+
+ public Class<?> getTargetClass() {
+ return Set.class;
+ }
+
+ }
+
+ public static class FloatToString extends AbstractUsesLocaleConverter<Float, String> {
+
+ public String convert(Float obj, Locale locale, TimeZone timeZone) throws ConversionException {
+ NumberFormat nf = NumberFormat.getNumberInstance(locale);
+ return nf.format(obj.floatValue());
+ }
+
+ public Class<Float> getSourceClass() {
+ return Float.class;
+ }
+
+ public Class<String> getTargetClass() {
+ return String.class;
+ }
+
+ }
+
+ public static class IntegerToBigDecimal extends AbstractConverter<Integer, BigDecimal> {
+
+ public BigDecimal convert(Integer obj) throws ConversionException {
+ return BigDecimal.valueOf(obj.doubleValue());
+ }
+
+ public Class<Integer> getSourceClass() {
+ return Integer.class;
+ }
+
+ public Class<BigDecimal> getTargetClass() {
+ return BigDecimal.class;
+ }
+
+ }
+
+ public static class IntegerToDouble extends AbstractConverter<Integer, Double> {
+
+ public Double convert(Integer obj) throws ConversionException {
+ return Double.valueOf(obj.doubleValue());
+ }
+
+ public Class<Integer> getSourceClass() {
+ return Integer.class;
+ }
+
+ public Class<Double> getTargetClass() {
+ return Double.class;
+ }
+
+ }
+
+ public static class IntegerToFloat extends AbstractConverter<Integer, Float> {
+
+ public Float convert(Integer obj) throws ConversionException {
+ return Float.valueOf(obj.floatValue());
+ }
+
+ public Class<Integer> getSourceClass() {
+ return Integer.class;
+ }
+
+ public Class<Float> getTargetClass() {
+ return Float.class;
+ }
+
+ }
+
+ public static class IntegerToList extends AbstractConverter<Integer, List<Integer>> {
+
+ public List<Integer> convert(Integer obj) throws ConversionException {
+ List<Integer> tempList = FastList.newInstance();
+ tempList.add(obj);
+ return tempList;
+ }
+
+ public Class<Integer> getSourceClass() {
+ return Integer.class;
+ }
+
+ public Class<?> getTargetClass() {
+ return List.class;
+ }
+
+ }
+
+ public static class IntegerToLong extends AbstractConverter<Integer, Long> {
+
+ public Long convert(Integer obj) throws ConversionException {
+ return Long.valueOf(obj.longValue());
+ }
+
+ public Class<Integer> getSourceClass() {
+ return Integer.class;
+ }
+
+ public Class<Long> getTargetClass() {
+ return Long.class;
+ }
+
+ }
+
+ public static class IntegerToSet extends AbstractConverter<Integer, Set<Integer>> {
+
+ public Set<Integer> convert(Integer obj) throws ConversionException {
+ Set<Integer> tempSet = FastSet.newInstance();
+ tempSet.add(obj);
+ return tempSet;
+ }
+
+ public Class<Integer> getSourceClass() {
+ return Integer.class;
+ }
+
+ public Class<?> getTargetClass() {
+ return Set.class;
+ }
+
+ }
+
+ public static class IntegerToString extends AbstractUsesLocaleConverter<Integer, String> {
+
+ public String convert(Integer obj, Locale locale, TimeZone timeZone) throws ConversionException {
+ NumberFormat nf = NumberFormat.getNumberInstance(locale);
+ return nf.format(obj.intValue());
+ }
+
+ public Class<Integer> getSourceClass() {
+ return Integer.class;
+ }
+
+ public Class<String> getTargetClass() {
+ return String.class;
+ }
+
+ }
+
+ public static class LongToBigDecimal extends AbstractConverter<Long, BigDecimal> {
+
+ public BigDecimal convert(Long obj) throws ConversionException {
+ return BigDecimal.valueOf(obj.doubleValue());
+ }
+
+ public Class<Long> getSourceClass() {
+ return Long.class;
+ }
+
+ public Class<BigDecimal> getTargetClass() {
+ return BigDecimal.class;
+ }
+
+ }
+
+ public static class LongToDouble extends AbstractConverter<Long, Double> {
+
+ public Double convert(Long obj) throws ConversionException {
+ return Double.valueOf(obj.doubleValue());
+ }
+
+ public Class<Long> getSourceClass() {
+ return Long.class;
+ }
+
+ public Class<Double> getTargetClass() {
+ return Double.class;
+ }
+
+ }
+
+ public static class LongToFloat extends AbstractConverter<Long, Float> {
+
+ public Float convert(Long obj) throws ConversionException {
+ return Float.valueOf(obj.floatValue());
+ }
+
+ public Class<Long> getSourceClass() {
+ return Long.class;
+ }
+
+ public Class<Float> getTargetClass() {
+ return Float.class;
+ }
+
+ }
+
+ public static class LongToInteger extends AbstractConverter<Long, Integer> {
+
+ public Integer convert(Long obj) throws ConversionException {
+ return Integer.valueOf(obj.intValue());
+ }
+
+ public Class<Long> getSourceClass() {
+ return Long.class;
+ }
+
+ public Class<Integer> getTargetClass() {
+ return Integer.class;
+ }
+
+ }
+
+ public static class LongToList extends AbstractConverter<Long, List<Long>> {
+
+ public List<Long> convert(Long obj) throws ConversionException {
+ List<Long> tempList = FastList.newInstance();
+ tempList.add(obj);
+ return tempList;
+ }
+
+ public Class<Long> getSourceClass() {
+ return Long.class;
+ }
+
+ public Class<?> getTargetClass() {
+ return List.class;
+ }
+
+ }
+
+ public static class LongToSet extends AbstractConverter<Long, Set<Long>> {
+
+ public Set<Long> convert(Long obj) throws ConversionException {
+ Set<Long> tempSet = FastSet.newInstance();
+ tempSet.add(obj);
+ return tempSet;
+ }
+
+ public Class<Long> getSourceClass() {
+ return Long.class;
+ }
+
+ public Class<?> getTargetClass() {
+ return Set.class;
+ }
+
+ }
+
+ public static class LongToString extends AbstractUsesLocaleConverter<Long, String> {
+
+ public String convert(Long obj, Locale locale, TimeZone timeZone) throws ConversionException {
+ NumberFormat nf = NumberFormat.getNumberInstance(locale);
+ return nf.format(obj.longValue());
+ }
+
+ public Class<Long> getSourceClass() {
+ return Long.class;
+ }
+
+ public Class<String> getTargetClass() {
+ return String.class;
+ }
+
+ }
+
+ public static class StringToBigDecimal extends AbstractToNumberConverter<String, BigDecimal> {
+
+ public BigDecimal convert(String obj, Locale locale, TimeZone timeZone) throws ConversionException {
+ return BigDecimal.valueOf(this.fromString(obj, locale).doubleValue());
+ }
+
+ public Class<String> getSourceClass() {
+ return String.class;
+ }
+
+ public Class<BigDecimal> getTargetClass() {
+ return BigDecimal.class;
+ }
+
+ }
+
+ public static class StringToDouble extends AbstractToNumberConverter<String, Double> {
+
+ public Double convert(String obj, Locale locale, TimeZone timeZone) throws ConversionException {
+ return this.fromString(obj, locale).doubleValue();
+ }
+
+ public Class<String> getSourceClass() {
+ return String.class;
+ }
+
+ public Class<Double> getTargetClass() {
+ return Double.class;
+ }
+
+ }
+
+ public static class StringToFloat extends AbstractToNumberConverter<String, Float> {
+
+ public Float convert(String obj, Locale locale, TimeZone timeZone) throws ConversionException {
+ return this.fromString(obj, locale).floatValue();
+ }
+
+ public Class<String> getSourceClass() {
+ return String.class;
+ }
+
+ public Class<Float> getTargetClass() {
+ return Float.class;
+ }
+
+ }
+
+ public static class StringToInteger extends AbstractToNumberConverter<String, Integer> {
+
+ public Integer convert(String obj, Locale locale, TimeZone timeZone) throws ConversionException {
+ return this.fromString(obj, locale).intValue();
+ }
+
+ public Class<String> getSourceClass() {
+ return String.class;
+ }
+
+ public Class<Integer> getTargetClass() {
+ return Integer.class;
+ }
+
+ }
+
+ public static class StringToLong extends AbstractToNumberConverter<String, Long> {
+
+ public Long convert(String obj, Locale locale, TimeZone timeZone) throws ConversionException {
+ return this.fromString(obj, locale).longValue();
+ }
+
+ public Class<String> getSourceClass() {
+ return String.class;
+ }
+
+ public Class<Long> getTargetClass() {
+ return Long.class;
+ }
+
+ }
+}
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/NumberConverters.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/NumberConverters.java
------------------------------------------------------------------------------
svn:keywords = "Date Rev Author URL Id"
Propchange: ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/NumberConverters.java
------------------------------------------------------------------------------
svn:mime-type = text/plain