@Override public <T> T to(final Object src, final Class<T> targetCls, final ConfigurationInterpolator ci) { final ConfigurationInterpolator interpolator = fetchInterpolator(ci); return convert(interpolator.interpolate(src), targetCls, interpolator); }
/** * {@inheritDoc} This implementation extracts all values stored in the * passed in source object, converts them to the target type, and adds them * to the target collection. The target collection must not be <b>null</b>. * If the source object is <b>null</b>, nothing is added to the collection. * * @throws IllegalArgumentException if the target collection is <b>null</b> */ @Override public <T> void toCollection(final Object src, final Class<T> elemClass, final ConfigurationInterpolator ci, final Collection<T> dest) { if (dest == null) { throw new IllegalArgumentException( "Target collection must not be null!"); } if (src != null && !isEmptyElement(src)) { final ConfigurationInterpolator interpolator = fetchInterpolator(ci); convertToCollection(src, elemClass, interpolator, dest); } }
/** * Performs the conversion from the passed in source object to the specified * target class. This method is called for each conversion to be done. The * source object has already been passed to the * {@link ConfigurationInterpolator}, so interpolation does not have to be * done again. (The passed in {@code ConfigurationInterpolator} may still be * necessary for extracting values from complex objects; it is guaranteed to * be non <b>null</b>.) The source object may be a complex object, e.g. a * collection or an array. This base implementation checks whether the * source object is complex. If so, it delegates to * {@link #extractConversionValue(Object, Class, ConfigurationInterpolator)} * to obtain a single value. Eventually, * {@link #convertValue(Object, Class, ConfigurationInterpolator)} is called * with the single value to be converted. * * @param <T> the desired target type of the conversion * @param src the source object to be converted * @param targetCls the desired target class * @param ci the {@code ConfigurationInterpolator} (not <b>null</b>) * @return the converted value * @throws ConversionException if conversion is not possible */ protected <T> T convert(final Object src, final Class<T> targetCls, final ConfigurationInterpolator ci) { final Object conversionSrc = isComplexObject(src) ? extractConversionValue(src, targetCls, ci) : src; return convertValue(ci.interpolate(conversionSrc), targetCls, ci); }
/** * {@inheritDoc} This implementation extracts all values stored in the * passed in source object, converts them to the target type, and adds them * to a result array. Arrays of objects and of primitive types are * supported. If the source object is <b>null</b>, result is <b>null</b>, * too. */ @Override public Object toArray(final Object src, final Class<?> elemClass, final ConfigurationInterpolator ci) { if (src == null) { return null; } if (isEmptyElement(src)) { return Array.newInstance(elemClass, 0); } final ConfigurationInterpolator interpolator = fetchInterpolator(ci); return elemClass.isPrimitive() ? toPrimitiveArray(src, elemClass, interpolator) : toObjectArray(src, elemClass, interpolator); }
/** * Helper method for converting all values of a source object and storing * them in a collection. * * @param <T> the target type of the conversion * @param src the source object * @param elemClass the target class of the conversion * @param ci the {@code ConfigurationInterpolator} * @param dest the collection in which to store the results * @throws ConversionException if a conversion cannot be performed */ private <T> void convertToCollection(final Object src, final Class<T> elemClass, final ConfigurationInterpolator ci, final Collection<T> dest) { for (final Object o : extractValues(ci.interpolate(src))) { dest.add(convert(o, elemClass, ci)); } }
final Collection<?> values = extractValues(src); final Class<?> targetClass = ClassUtils.primitiveToWrapper(elemClass); final Object array = Array.newInstance(elemClass, values.size()); convertValue(ci.interpolate(value), targetClass, ci));
/** * Extracts all values contained in the given source object and returns them * as a flat collection. * * @param source the source object (may be a single value or a complex * object) * @return a collection with all extracted values */ protected Collection<?> extractValues(final Object source) { return extractValues(source, Integer.MAX_VALUE); }
/** * {@inheritDoc} This implementation checks for a defined data format in * the following order: * <ul> * <li>If a temporary date format is set for the current call, it is * used.</li> * <li>If a date format is specified in this configuration using the * {@code DATE_FORMAT_KEY} property, it is used.</li> * <li>Otherwise, the date format set for the original conversion * handler is used if available.</li> * </ul> */ @Override public String getDateFormat() { if (StringUtils.isNotEmpty(TEMP_DATE_FORMAT.get())) { return TEMP_DATE_FORMAT.get(); } if (containsKey(DATE_FORMAT_KEY)) { return getDefaultDateFormat(); } final DefaultConversionHandler orgHandler = getOriginalConversionHandler(); return (orgHandler != null) ? orgHandler.getDateFormat() : null; } }
/** * Converts the given source object to an array of objects. * * @param src the source object * @param elemClass the element class of the array * @param ci the {@code ConfigurationInterpolator} * @return the result array * @throws ConversionException if a conversion cannot be performed */ private <T> T[] toObjectArray(final Object src, final Class<T> elemClass, final ConfigurationInterpolator ci) { final Collection<T> convertedCol = new LinkedList<>(); convertToCollection(src, elemClass, ci, convertedCol); // Safe to cast because the element class is specified @SuppressWarnings("unchecked") final T[] result = (T[]) Array.newInstance(elemClass, convertedCol.size()); return convertedCol.toArray(result); }
/** * Extracts a single value from a complex object. This method is called by * {@code convert()} if the source object is complex. This implementation * extracts the first value from the complex object and returns it. * * @param container the complex object * @param targetCls the target class of the conversion * @param ci the {@code ConfigurationInterpolator} (not <b>null</b>) * @return the value to be converted (may be <b>null</b> if no values are * found) */ protected Object extractConversionValue(final Object container, final Class<?> targetCls, final ConfigurationInterpolator ci) { final Collection<?> values = extractValues(container, 1); return values.isEmpty() ? null : ci.interpolate(values.iterator() .next()); }
return toDate(value, convHandler.getDateFormat()); return toCalendar(value, convHandler.getDateFormat());