Search in sources :

Example 6 with Converter

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

the class StringCodecs method check.

public static void check() {
    if (classLoaders.putIfAbsent(Thread.currentThread().getContextClassLoader(), Boolean.TRUE) == null) {
        loadDefaultConverters();
        for (Map.Entry<Class<?>, Class<? extends StringCodec<?>>> entry : codecs.entrySet()) {
            try {
                final StringCodec<?> codecInstance = entry.getValue().newInstance();
                ConvertUtils.register(new Converter() {

                    @Override
                    public Object convert(Class type, Object value) {
                        return value == null ? null : codecInstance.fromString(value.toString());
                    }
                }, entry.getKey());
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}
Also used : StringCodec(com.datatorrent.api.StringCodec) Converter(org.apache.commons.beanutils.Converter) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 7 with Converter

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

the class DeferredValueMatcher method matches.

public boolean matches(Class<?> parentEntityType, String propertyName, Object value) {
    if (value == null) {
        return _value == null;
    } else if (_value == null) {
        return false;
    }
    if (_resolvedValueSet) {
        return value.equals(_resolvedValue);
    }
    Class<?> expectedValueType = value.getClass();
    Class<?> actualValueType = _value.getClass();
    if (expectedValueType.isAssignableFrom(actualValueType)) {
        return value.equals(_value);
    }
    if (actualValueType == String.class) {
        String actualValue = (String) _value;
        if (expectedValueType == AgencyAndId.class) {
            AgencyAndId expectedId = (AgencyAndId) value;
            return expectedId.getId().equals(actualValue);
        } else if (IdentityBean.class.isAssignableFrom(expectedValueType)) {
            IdentityBean<?> bean = (IdentityBean<?>) value;
            Object expectedId = bean.getId();
            if (expectedId == null) {
                return false;
            }
            if (expectedId instanceof AgencyAndId) {
                AgencyAndId expectedFullId = (AgencyAndId) expectedId;
                return expectedFullId.getId().equals(actualValue);
            } else if (expectedId instanceof String) {
                return expectedId.equals(actualValue);
            }
        } else {
            Converter converter = _support.resolveConverter(parentEntityType, propertyName, expectedValueType);
            if (converter != null) {
                _resolvedValue = converter.convert(expectedValueType, _value);
                _resolvedValueSet = true;
                return value.equals(_resolvedValue);
            } else {
                throw new IllegalStateException("no type conversion from type String to type \"" + expectedValueType.getName() + "\" for value comparison");
            }
        }
    }
    throw new IllegalStateException("no type conversion from type \"" + actualValueType.getName() + "\" to type \"" + expectedValueType.getName() + "\" for value comparison");
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Converter(org.apache.commons.beanutils.Converter) IdentityBean(org.onebusaway.gtfs.model.IdentityBean)

Example 8 with Converter

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

the class EntityFieldMappingImpl method translateFromCSVToObject.

public void translateFromCSVToObject(CsvEntityContext context, Map<String, Object> csvValues, BeanWrapper object) {
    if (isMissingAndOptional(csvValues))
        return;
    Converter converter = create(context);
    String entityId = (String) csvValues.get(_csvFieldName);
    Object entity = converter.convert(_objFieldType, entityId);
    object.setPropertyValue(_objFieldName, entity);
}
Also used : Converter(org.apache.commons.beanutils.Converter)

Example 9 with Converter

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

the class DeferredValueSupportTest method testResolveConverter.

@Test
public void testResolveConverter() {
    Converter converter = _support.resolveConverter(Object.class, "xyz", String.class);
    assertSame(ConvertUtils.lookup(String.class), converter);
}
Also used : Converter(org.apache.commons.beanutils.Converter) Test(org.junit.Test)

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