Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* Copyright 2016 Guy Davenport
*
* Licensed 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 uno.informatics.common;
import static uno.informatics.data.DataType.BIG_DECIMAL;
import static uno.informatics.data.DataType.BIG_INTEGER;
import static uno.informatics.data.DataType.BOOLEAN;
import static uno.informatics.data.DataType.DATE;
import static uno.informatics.data.DataType.DOUBLE;
import static uno.informatics.data.DataType.FLOAT;
import static uno.informatics.data.DataType.INTEGER;
import static uno.informatics.data.DataType.LONG;
import static uno.informatics.data.DataType.SHORT;
import static uno.informatics.data.DataType.STRING;
import static uno.informatics.data.DataTypeConstants.BIG_DECIMAL_ID;
import static uno.informatics.data.DataTypeConstants.BIG_INTEGER_ID;
import static uno.informatics.data.DataTypeConstants.BOOLEAN_ID;
import static uno.informatics.data.DataTypeConstants.DATE_ID;
import static uno.informatics.data.DataTypeConstants.DEFAULT_TYPE_IDS;
import static uno.informatics.data.DataTypeConstants.DOUBLE_ID;
import static uno.informatics.data.DataTypeConstants.FLOAT_ID;
import static uno.informatics.data.DataTypeConstants.INT_ID;
import static uno.informatics.data.DataTypeConstants.LONG_ID;
import static uno.informatics.data.DataTypeConstants.SHORT_ID;
import static uno.informatics.data.DataTypeConstants.STRING_ID;
import static uno.informatics.data.DataTypeConstants.UNKNOWN_ID;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class ConversionUtilities
{
private static final String TRUE = "true";
private static final String FALSE = "false";
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMddHHmmssZ");
@SuppressWarnings("unused")
private static String DEFAULT_DELIMITER = "&" ;
public static final String convertToString(String value)
{
return value;
}
/**
* Converts a string to an object to an integer, double, boolean or string. Will try to convert in following order
* Integer, Double and Boolean, or return the original String
*
* @param string the string to be converted
* @return a object representation of the string
*/
public static final Object convertToObject(String string)
{
try
{
return convertToObject(string, DEFAULT_TYPE_IDS);
}
catch (ConversionException e)
{
return string ;
}
}
/**
* Converts a string to an object given a set of possible types to convert to. Will try to convert in following order
* Short, Integer, Long, Float, Double, BigInteger, BigDecmal and Boolean, Date and String. An exception is throw if
* the string can not be converted to one of the possible types.
*
* @param string the string to be converted
* @param types possible types to which to convert
* @return a object representation of the string
* @throws ConversionException if the string can not be converted to one of the possible types
*/
public static final Object convertToObject(String string, int types) throws ConversionException
{
Object value = null ;
if (string != null)
{
if (value == null && (types & SHORT_ID) != 0)
value = convertToShortInternal(string) ;
if (value == null && (types & INT_ID) != 0)
value = convertToIntegerInternal(string) ;
if (value == null && (types & LONG_ID) != 0)
value = convertToLongInternal(string) ;
if (value == null && (types & FLOAT_ID) != 0)
value = convertToFloatInternal(string) ;
if (value == null && (types & DOUBLE_ID) != 0)
value = convertToDoubleInternal(string) ;
if (value == null && (types & BIG_INTEGER_ID) != 0)
value = convertToBigIntegerInternal(string) ;
if (value == null && (types & BIG_DECIMAL_ID) != 0)
value = convertToBigDecimalInternal(string) ;
if (value == null && (types & BOOLEAN_ID) != 0)
value = convertToBooleanInternal(string) ;
if (value == null && (types & DATE_ID) != 0)
value = convertToDateInternal(string) ;
if (value == null && (types & STRING_ID) != 0)
value = string ;
if (value == null)
throw new ConversionException("Can not convert to Object using requested types " + getTypesNames(types) + "!") ;
}
return value;
}
public static List getTypesNames(int types)
{
List labels = new LinkedList() ;
if ((types & SHORT_ID) != 0)
labels.add(SHORT.getName()) ;
if ((types & INT_ID) != 0)
labels.add(INTEGER.getName()) ;
if ((types & SHORT_ID) != 0)
labels.add(SHORT.getName()) ;
if ((types & LONG_ID) != 0)
labels.add(LONG.getName()) ;
if ((types & FLOAT_ID) != 0)
labels.add(FLOAT.getName()) ;
if ((types & DOUBLE_ID) != 0)
labels.add(DOUBLE.getName()) ;
if ((types & BIG_INTEGER_ID) != 0)
labels.add(BIG_INTEGER.getName()) ;
if ((types & BIG_DECIMAL_ID) != 0)
labels.add(BIG_DECIMAL.getName()) ;
if ((types & BOOLEAN_ID) != 0)
labels.add(BOOLEAN.getName()) ;
if ((types & DATE_ID) != 0)
labels.add(DATE.getName()) ;
if ((types & STRING_ID) != 0)
labels.add(STRING.getName()) ;
return labels ;
}
/**
* Converts a string to an number given a set of possible types to convert to. Will try to convert in following order
* Short, Integer, Long, Float, Double, BigInteger, BigDecmal
*
* @param string the string to be converted
* @param types types to convert
* @return a object representation of the string
* @throws ConversionException if conversion to all given types fails
*/
public static final Number convertToNumber(String string, int types) throws ConversionException
{
Object value = null ;
if (string != null)
{
if (value == null && (types & SHORT_ID) != 0)
value = convertToShortInternal(string) ;
if (value == null && (types & INT_ID) != 0)
value = convertToIntegerInternal(string) ;
if (value == null && (types & LONG_ID) != 0)
value = convertToLongInternal(string) ;
if (value == null && (types & FLOAT_ID) != 0)
value = convertToFloatInternal(string) ;
if (value == null && (types & DOUBLE_ID) != 0)
value = convertToDoubleInternal(string) ;
if (value == null && (types & BIG_INTEGER_ID) != 0)
value = convertToBigIntegerInternal(string) ;
if (value == null && (types & BIG_DECIMAL_ID) != 0)
value = convertToBigDecimalInternal(string) ;
if (value == null)
throw new ConversionException("Can not convert to Number using requested types " + getTypesNames(types) + "!") ;
}
return (Number)value;
}
public static final BigDecimal convertToBigDecimal(String string) throws ConversionException
{
BigDecimal value = null ;
if (string != null)
{
try
{
value = new BigDecimal(string) ;
}
catch (NumberFormatException e)
{
throw new ConversionException(e.getMessage(), e);
}
}
return value ;
}
public static final BigInteger convertToBigInteger(String string) throws ConversionException
{
BigInteger value = null ;
if (string != null)
{
try
{
value = new BigInteger(string) ;
}
catch (NumberFormatException e)
{
throw new ConversionException(e.getMessage(), e);
}
}
return value ;
}
public static final Double convertToDouble(String string) throws ConversionException
{
Double value = null ;
if (string != null)
{
try
{
value = Double.valueOf(string);
}
catch (NumberFormatException e)
{
throw new ConversionException(e.getMessage(), e);
}
}
return value ;
}
public static final Float convertToFloat(String string) throws ConversionException
{
Float value = null ;
if (string != null)
{
try
{
value = Float.valueOf(string);
}
catch (NumberFormatException e)
{
throw new ConversionException(e.getMessage(), e);
}
}
return value ;
}
public static final Long convertToLong(String string) throws ConversionException
{
Long value = null ;
if (string != null)
{
try
{
value = Long.valueOf(string);
}
catch (NumberFormatException e)
{
throw new ConversionException(e.getMessage(), e);
}
}
return value ;
}
public static final Integer convertToInteger(String string) throws ConversionException
{
Integer value = null ;
if (string != null)
{
try
{
value = Integer.valueOf(string);
}
catch (NumberFormatException e)
{
throw new ConversionException(e.getMessage(), e);
}
}
return value ;
}
public static final Short convertToShort(String string) throws ConversionException
{
Short value = null ;
if (string != null)
{
try
{
value = Short.valueOf(string);
}
catch (NumberFormatException e)
{
throw new ConversionException(e.getMessage(), e);
}
}
return value ;
}
public static final Boolean convertToBoolean(String string) throws ConversionException
{
Boolean value = null ;
if (string != null)
{
if (TRUE.equals(string.toLowerCase()))
{
return true ;
}
else
{
if (FALSE.equals(string.toLowerCase()))
{
return false ;
}
}
}
return value ;
}
@SuppressWarnings("deprecation")
public static final Date convertToDate(String value) throws ConversionException
{
if (value != null)
{
try
{
return DATE_FORMAT.parse(value) ;
}
catch (Exception e)
{
try
{
return new Date(value) ;
}
catch (Exception e2)
{
throw new ConversionException(e2) ;
}
}
}
else
{
return null ;
}
}
public static final String convertToString(Object value)
{
if (value != null)
if (value instanceof Date)
return DATE_FORMAT.format((Date)value) ;
else
return value.toString() ;
else
return null ;
}
public static final Double convertToDouble(Object value) throws ConversionException
{
if (value != null)
{
if (value instanceof Double)
{
return (Double)value ;
}
else
{
if (value instanceof Number)
{
return ((Number)value).doubleValue() ;
}
else
{
if (value instanceof String)
{
return convertToDouble((String)value) ;
}
else
{
throw new ConversionException("Can not convert value to double : " + value) ;
}
}
}
}
else
{
return null ;
}
}
public static final Integer convertToInteger(Object value) throws ConversionException
{
if (value != null)
{
if (value instanceof Integer)
{
return (Integer)value ;
}
else
{
if (value instanceof Number)
{
return ((Number)value).intValue() ;
}
else
{
if (value instanceof String)
{
return convertToInteger((String)value) ;
}
else
{
throw new ConversionException("Can not convert value to integer : " + value) ;
}
}
}
}
else
{
return null ;
}
}
public static List convertToShortList(List values) throws ConversionException
{
List list = new ArrayList() ;
Iterator iterator = values.iterator() ;
while(iterator.hasNext())
{
list.add(convertToShort(iterator.next())) ;
}
return list;
}
public static List convertToIntegerList(List values) throws ConversionException
{
List list = new ArrayList() ;
Iterator iterator = values.iterator() ;
while(iterator.hasNext())
{
list.add(convertToInteger(iterator.next())) ;
}
return list;
}
public static List convertToLongList(List values) throws ConversionException
{
List list = new ArrayList() ;
Iterator iterator = values.iterator() ;
while(iterator.hasNext())
{
list.add(convertToLong(iterator.next())) ;
}
return list;
}
public static List convertToFloatList(List values) throws ConversionException
{
List list = new ArrayList() ;
Iterator iterator = values.iterator() ;
while(iterator.hasNext())
{
list.add(convertToFloat(iterator.next())) ;
}
return list;
}
public static List convertToDoubleList(List values) throws ConversionException
{
List list = new ArrayList() ;
Iterator iterator = values.iterator() ;
while(iterator.hasNext())
{
list.add(convertToDouble(iterator.next())) ;
}
return list;
}
public static List