/** * Added for apache commons beanutils compatibility, * you should probably use {@link #convert(Object, Class)}<br/> * Convert the string value to an object of the specified class (if * possible). Otherwise, return a String representation of the value. * * @param value the string value to be converted * @param type any class type that you want to try to convert the object to * @return the converted value * @throws UnsupportedOperationException if the conversion cannot be completed */ public Object convertString(String value, Class<?> type) { Object convert = null; try { convert = convert(value, type); } catch (UnsupportedOperationException e) { convert = value; } return convert; }
protected ConversionUtils getConversionUtils() { return ConversionUtils.getInstance(); }
/** * Set the singleton instance of the class which will be stored statically * @param instance the instance to use as the singleton instance */ public static ConversionUtils setInstance(ConversionUtils newInstance) { ConversionUtils instance = newInstance; if (instance == null) { instance = new ConversionUtils(); instance.singleton = true; } ConversionUtils.timesCreated++; instanceStorage = new SoftReference<ConversionUtils>(instance); return instance; }
/** * This helper method will convert the incoming data if it needs to be * converted for the given column, otherwise it will do nothing to the value * @param namesRecord the names record for the persistent class * @param column the name of the column this value is associated with * @param value the value to convert * @return the converted value or the original value if no conversion needed */ public static Object convertColumn(NamesRecord namesRecord, String column, Object value) { if (namesRecord != null && column != null) { try { Class<?> convertType = namesRecord.getTypeForColumn(column); if (convertType != null) { value = ConversionUtils.getInstance().convert(value, convertType); } } catch (Exception e) { // nothing to do but continue on } } return value; }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("converters="); sb.append(getConverters().size()); sb.append(":"); for (Entry<Class<?>, Converter<?>> entry : getConverters().entrySet()) { sb.append("["); sb.append(entry.getKey().getName()); sb.append("=>"); sb.append(entry.getValue().getClass().getName()); sb.append("]"); } sb.append(":variable="); sb.append(getVariableConverters().size()); sb.append(":"); for (VariableConverter variableConverter : getVariableConverters()) { sb.append("("); sb.append(variableConverter.getClass().getName()); sb.append(")"); } return "Convert::c="+ConversionUtils.timesCreated+":s="+singleton+":" + sb.toString(); }
T convert = null; VariableConverter variableConverter = getVariableConverter(value, type); if (variableConverter != null) { } else { Converter<T> converter = getConverterOrFail(type); if (InterfaceConverter.class.isAssignableFrom(converter.getClass())) { convert = ((InterfaceConverter<T>)converter).convertInterface(value, type);
/** * Add a converter to the default set which will convert objects to the supplied type * @param type the type this converter will convert objects to * @param converter the converter */ public void addConverter(Class<?> type, Converter<?> converter) { if (type == null || converter == null) { throw new IllegalArgumentException("You must specify a type and a converter in order to add a converter (no nulls)"); } getConverters().put(type, converter); }
/** * Get the converter or throw exception * @param <T> * @param type type to convert to * @return the converter or die * @throws UnsupportedOperationException if the converter cannot be found */ protected <T> Converter<T> getConverterOrFail(Class<T> type) { Converter<T> converter = getConverter(type); if (converter == null) { throw new UnsupportedOperationException("Conversion failure: No converter available to handle conversions to type ("+type+")"); } return converter; }
protected void loadDefaultVariableConverters() { if (variableConverters == null) { variableConverters = new Vector<VariableConverter>(); } else { clearVariableConverters(); } variableConverters.add( new ArrayConverter() ); variableConverters.add( new ScalarConverter() ); }
/** * Add a converter to the default set which will convert objects to the supplied type * @param type the type this converter will convert objects to * @param converter the converter */ public void addConverter(Class<?> type, Converter<?> converter) { getConversionUtils().addConverter(type, converter); }
convert = convertWithConverter(toConvert, type); } catch (RuntimeException e) { throw new UnsupportedOperationException("Could not convert object ("+toConvert+") from type ("+fromType+") to type ("+type+"): " + e.getMessage(), e);
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("converters="); sb.append(getConverters().size()); sb.append(":"); for (Entry<Class<?>, Converter<?>> entry : getConverters().entrySet()) { sb.append("["); sb.append(entry.getKey().getName()); sb.append("=>"); sb.append(entry.getValue().getClass().getName()); sb.append("]"); } sb.append(":variable="); sb.append(getVariableConverters().size()); sb.append(":"); for (VariableConverter variableConverter : getVariableConverters()) { sb.append("("); sb.append(variableConverter.getClass().getName()); sb.append(")"); } return "Convert::c="+ConversionUtils.timesCreated+":s="+singleton+":" + sb.toString(); }
T convert = null; VariableConverter variableConverter = getVariableConverter(value, type); if (variableConverter != null) { } else { Converter<T> converter = getConverterOrFail(type); if (InterfaceConverter.class.isAssignableFrom(converter.getClass())) { convert = ((InterfaceConverter<T>)converter).convertInterface(value, type);
/** * Add a converter to the default set which will convert objects to the supplied type * @param type the type this converter will convert objects to * @param converter the converter */ public void addConverter(Class<?> type, Converter<?> converter) { if (type == null || converter == null) { throw new IllegalArgumentException("You must specify a type and a converter in order to add a converter (no nulls)"); } getConverters().put(type, converter); }
/** * Get the converter or throw exception * @param <T> * @param type type to convert to * @return the converter or die * @throws UnsupportedOperationException if the converter cannot be found */ protected <T> Converter<T> getConverterOrFail(Class<T> type) { Converter<T> converter = getConverter(type); if (converter == null) { throw new UnsupportedOperationException("Conversion failure: No converter available to handle conversions to type ("+type+")"); } return converter; }
protected void loadDefaultVariableConverters() { if (variableConverters == null) { variableConverters = new Vector<VariableConverter>(); } else { clearVariableConverters(); } variableConverters.add( new ArrayConverter() ); variableConverters.add( new ScalarConverter() ); }
/** * Add a converter to the default set which will convert objects to the supplied type * @param type the type this converter will convert objects to * @param converter the converter */ public void addConverter(Class<?> type, Converter<?> converter) { getConversionUtils().addConverter(type, converter); }
convert = convertWithConverter(toConvert, type); } catch (RuntimeException e) { throw new UnsupportedOperationException("Could not convert object ("+toConvert+") from type ("+fromType+") to type ("+type+"): " + e.getMessage(), e);
/** * Added for apache commons beanutils compatibility, * you should probably use {@link #convert(Object, Class)}<br/> * Convert the string value to an object of the specified class (if * possible). Otherwise, return a String representation of the value. * * @param value the string value to be converted * @param type any class type that you want to try to convert the object to * @return the converted value * @throws UnsupportedOperationException if the conversion cannot be completed */ public Object convertString(String value, Class<?> type) { Object convert = null; try { convert = convert(value, type); } catch (UnsupportedOperationException e) { convert = value; } return convert; }
public ConversionUtils getConversionUtils() { return ConversionUtils.getInstance(); }