Search in sources :

Example 1 with Converter

use of org.apache.commons.beanutils.Converter in project onebusaway-gtfs-modules by OneBusAway.

the class DeferredValueConverter method convertValue.

/**
   * Converts the specified value as appropriate such that the resulting value
   * can be assigned to the specified property of the specified bean.
   */
public Object convertValue(BeanWrapper targetBean, String targetPropertyName, Object value) {
    if (value == null) {
        return null;
    }
    Class<?> expectedValueType = targetBean.getPropertyType(targetPropertyName);
    Class<?> actualValueType = value.getClass();
    /**
     * When we introspect the "id" property of an IdentityBean instance, the
     * return type is always Serializable, when the actual type is String or
     * AgencyAndId. This causes trouble with the "isAssignableFrom" check below,
     * so we do a first check here.
     */
    Object parentObject = targetBean.getWrappedInstance(Object.class);
    if (parentObject instanceof IdentityBean && targetPropertyName.equals("id")) {
        Class<?> idType = getIdentityBeanIdType(parentObject);
        if (idType == AgencyAndId.class && actualValueType == String.class) {
            return _support.resolveAgencyAndId(targetBean, targetPropertyName, (String) value);
        }
    }
    if (expectedValueType.isAssignableFrom(actualValueType)) {
        return value;
    }
    if (isPrimitiveAssignable(expectedValueType, actualValueType)) {
        return value;
    }
    if (actualValueType == String.class) {
        String stringValue = (String) value;
        if (AgencyAndId.class.isAssignableFrom(expectedValueType)) {
            return _support.resolveAgencyAndId(targetBean, targetPropertyName, stringValue);
        }
        if (IdentityBean.class.isAssignableFrom(expectedValueType)) {
            Serializable id = stringValue;
            if (getIdType(expectedValueType) == AgencyAndId.class) {
                GtfsReaderContext context = _support.getReader().getGtfsReaderContext();
                String agencyId = context.getAgencyForEntity(expectedValueType, stringValue);
                id = new AgencyAndId(agencyId, stringValue);
            }
            Object entity = _dao.getEntityForId(expectedValueType, id);
            if (entity == null) {
                throw new IllegalStateException("entity not found: type=" + expectedValueType.getName() + " id=" + id);
            }
            return entity;
        }
        Class<?> parentEntityType = parentObject.getClass();
        Converter converter = _support.resolveConverter(parentEntityType, targetPropertyName, expectedValueType);
        if (converter != null) {
            return converter.convert(expectedValueType, value);
        }
    }
    throw new IllegalStateException("no conversion possible from type \"" + actualValueType.getName() + "\" to type \"" + expectedValueType.getName() + "\"");
}
Also used : Serializable(java.io.Serializable) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Converter(org.apache.commons.beanutils.Converter) GtfsReaderContext(org.onebusaway.gtfs.serialization.GtfsReaderContext) IdentityBean(org.onebusaway.gtfs.model.IdentityBean)

Example 2 with Converter

use of org.apache.commons.beanutils.Converter in project onebusaway-gtfs-modules by OneBusAway.

the class ObjectEquality method objectsAreEqual.

public static boolean objectsAreEqual(Object expected, Object actual) {
    boolean nullA = expected == null;
    boolean nullB = actual == null;
    if (nullA && nullB)
        return true;
    if (nullA ^ nullB)
        return false;
    Class<?> expectedType = expected.getClass();
    Class<?> actualType = actual.getClass();
    /**
     * Implementation note: This conversion theoretically will happen over and
     * over with the same value. Is there some way to cache it?
     */
    if (!actualType.isAssignableFrom(expectedType) && expectedType == String.class) {
        Converter converter = ConvertUtils.lookup(actualType);
        if (converter != null) {
            Object converted = converter.convert(actualType, expected);
            if (converted != null)
                expected = converted;
        }
    }
    return (expected == null && actual == null) || (expected != null && expected.equals(actual));
}
Also used : Converter(org.apache.commons.beanutils.Converter)

Example 3 with Converter

use of org.apache.commons.beanutils.Converter in project onebusaway-gtfs-modules by OneBusAway.

the class DeferredValueSupportTest method testResolveConverter_FieldMappingConverter.

@Test
public void testResolveConverter_FieldMappingConverter() {
    EntitySchema schema = new EntitySchema(Object.class, "object.txt", false);
    FieldMappingAndConverter field = mock(FieldMappingAndConverter.class);
    when(field.getCsvFieldName()).thenReturn("xyz");
    schema.addField(field);
    _schemaCache.addEntitySchema(schema);
    Converter converter = _support.resolveConverter(Object.class, "xyz", String.class);
    assertSame(field, converter);
}
Also used : Converter(org.apache.commons.beanutils.Converter) EntitySchema(org.onebusaway.csv_entities.schema.EntitySchema) Test(org.junit.Test)

Example 4 with Converter

use of org.apache.commons.beanutils.Converter in project apex-core by apache.

the class StringCodecs method register.

public static <T> void register(final Class<? extends StringCodec<?>> codec, final Class<T> clazz) throws InstantiationException, IllegalAccessException {
    check();
    final StringCodec<?> codecInstance = codec.newInstance();
    ConvertUtils.register(new Converter() {

        @Override
        public Object convert(Class type, Object value) {
            return value == null ? null : codecInstance.fromString(value.toString());
        }
    }, clazz);
    codecs.put(clazz, codec);
}
Also used : Converter(org.apache.commons.beanutils.Converter)

Example 5 with Converter

use of org.apache.commons.beanutils.Converter in project apex-core by apache.

the class StringCodecs method loadDefaultConverters.

public static void loadDefaultConverters() {
    LOG.debug("Loading default converters for BeanUtils");
    ConvertUtils.register(new Converter() {

        @Override
        @SuppressWarnings("unchecked")
        public Object convert(Class type, Object value) {
            if (value == null) {
                return null;
            }
            for (Class<?> clazz = value.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
                Class<? extends StringCodec> codec = codecs.get(clazz);
                if (codec == null) {
                    continue;
                }
                StringCodec instance;
                try {
                    instance = codec.newInstance();
                } catch (IllegalAccessException ex) {
                    throw new RuntimeException("Internal Error - it's impossible for this exception to be thrown!", ex);
                } catch (InstantiationException ex) {
                    throw new RuntimeException("Internal Error - it's impossible for this exception to be thrown!", ex);
                }
                return instance.toString(value);
            }
            return value.toString();
        }
    }, String.class);
    ConvertUtils.register(new Converter() {

        @Override
        public Object convert(Class type, Object value) {
            return value == null ? null : URI.create(value.toString());
        }
    }, URI.class);
}
Also used : StringCodec(com.datatorrent.api.StringCodec) Converter(org.apache.commons.beanutils.Converter)

Aggregations

Converter (org.apache.commons.beanutils.Converter)9 StringCodec (com.datatorrent.api.StringCodec)2 Test (org.junit.Test)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 IdentityBean (org.onebusaway.gtfs.model.IdentityBean)2 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 EntitySchema (org.onebusaway.csv_entities.schema.EntitySchema)1 GtfsReaderContext (org.onebusaway.gtfs.serialization.GtfsReaderContext)1