Search in sources :

Example 1 with ObjectConverter

use of org.apache.sis.util.ObjectConverter in project sis by apache.

the class ConverterRegistry method toString.

/**
 * Returns a string representation of registered converters for debugging purpose.
 * The converters are show in a tree where all real converters are leafs. Parents
 * of those leafs are {@link FallbackConverter}s which delegate their work to the
 * leafs.
 *
 * @return a string representation of registered converters.
 */
@Debug
@Override
public String toString() {
    final TreeTable table = Column.createTable();
    final TreeTable.Node root = table.getRoot();
    root.setValue(Column.TARGET, getClass());
    synchronized (converters) {
        for (final Map.Entry<ClassPair<?, ?>, ObjectConverter<?, ?>> entry : converters.entrySet()) {
            TreeTable.Node addTo = root;
            final ClassPair<?, ?> key = entry.getKey();
            final ObjectConverter<?, ?> converter = entry.getValue();
            if (converter.getSourceClass() != key.sourceClass || converter.getTargetClass() != key.targetClass) {
                /*
                     * If we enter this block, then the converter is not really for this
                     * (source, target) classes pair. Instead, we are leveraging a converter
                     * which was defined for an other ClassPair.  We show this fact be first
                     * showing this ClassPair, then the actual converter (source, target) as
                     * below:
                     *
                     *     Number      ← String             (the ClassPair key)
                     *       └─Integer ← String             (the ObjectConverter value)
                     *
                     * This is the same idea than the formatting done by FallbackConverter,
                     * except that there is only one child. Actually this can be though as
                     * a lightweight fallback converter.
                     */
                addTo = addTo.newChild();
                addTo.setValue(Column.SOURCE, key.sourceClass);
                addTo.setValue(Column.TARGET, key.targetClass);
            }
            if (converter instanceof FallbackConverter<?, ?>) {
                ((FallbackConverter<?, ?>) converter).toTree(addTo.newChild(), true);
            } else {
                Column.toTree(converter, addTo);
            }
        }
    }
    return Column.format(table);
}
Also used : ObjectConverter(org.apache.sis.util.ObjectConverter) TreeTable(org.apache.sis.util.collection.TreeTable) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Debug(org.apache.sis.util.Debug)

Example 2 with ObjectConverter

use of org.apache.sis.util.ObjectConverter in project sis by apache.

the class PropertyAccessor method convert.

/**
 * Converts values in the specified array to the given type.
 * The array content is modified in-place. This method accepts an array instead than
 * a single value because the values to convert may be the content of a collection.
 *
 * @param  elements    the array which contains element to convert.
 * @param  targetType  the base type of target elements.
 * @throws ClassCastException if an element can't be converted.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private void convert(final Object[] elements, final Class<?> targetType) throws ClassCastException {
    boolean hasNewConverter = false;
    ObjectConverter<?, ?> converter = null;
    for (int i = 0; i < elements.length; i++) {
        final Object value = elements[i];
        if (value != null) {
            final Class<?> sourceType = value.getClass();
            if (!targetType.isAssignableFrom(sourceType))
                try {
                    if (converter == null) {
                        // Volatile field - read only if needed.
                        converter = lastConverter;
                    }
                    /*
                     * Require the exact same classes, not parent or subclass,
                     * otherwise the converter could be stricter than necessary.
                     */
                    if (converter == null || converter.getSourceClass() != sourceType || converter.getTargetClass() != targetType) {
                        converter = ObjectConverters.find(sourceType, targetType);
                        hasNewConverter = true;
                    }
                    elements[i] = ((ObjectConverter) converter).apply(value);
                } catch (UnconvertibleObjectException cause) {
                    throw (ClassCastException) new ClassCastException(Errors.format(Errors.Keys.IllegalClass_2, targetType, sourceType)).initCause(cause);
                }
        }
    }
    if (hasNewConverter) {
        // Volatile field - store only if needed.
        lastConverter = converter;
    }
}
Also used : UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) ObjectConverter(org.apache.sis.util.ObjectConverter) IdentifiedObject(org.apache.sis.xml.IdentifiedObject)

Aggregations

ObjectConverter (org.apache.sis.util.ObjectConverter)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Debug (org.apache.sis.util.Debug)1 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)1 TreeTable (org.apache.sis.util.collection.TreeTable)1 IdentifiedObject (org.apache.sis.xml.IdentifiedObject)1