use of org.knime.core.data.convert.ConversionKey in project knime-core by knime.
the class JavaToDataCellConverterRegistry method register.
/**
* Register a DataCellToJavaConverterFactory.
*
* @param factory The factory to register
*/
public synchronized void register(final JavaToDataCellConverterFactory<?> factory) {
if (factory == null) {
throw new IllegalArgumentException("factory must not be null");
}
final ConversionKey key = new ConversionKey(factory);
ArrayList<JavaToDataCellConverterFactory<?>> list = m_converterFactories.get(key);
if (list == null) {
list = new ArrayList<>();
m_converterFactories.put(key, list);
}
list.add(factory);
final DataType destType = factory.getDestinationType();
Set<JavaToDataCellConverterFactory<?>> byDestinationType = m_byDestinationType.get(destType);
if (byDestinationType == null) {
byDestinationType = new LinkedHashSet<>();
m_byDestinationType.put(destType, byDestinationType);
}
byDestinationType.add(factory);
final Class<?> sourceType = factory.getSourceType();
Set<JavaToDataCellConverterFactory<?>> bySourceType = m_bySourceType.get(sourceType);
if (bySourceType == null) {
bySourceType = new LinkedHashSet<>();
m_bySourceType.put(sourceType, bySourceType);
bySourceType.add(factory);
} else {
bySourceType.add(factory);
}
final JavaToDataCellConverterFactory<?> previous = m_byIdentifier.put(factory.getIdentifier(), factory);
if (previous != null) {
LOGGER.coding("JavaToDataCellConverterFactory identifier is not unique (" + factory.getIdentifier() + ")");
}
}
use of org.knime.core.data.convert.ConversionKey in project knime-core by knime.
the class DataCellToJavaConverterRegistry method register.
/**
* Register a DataCellToJavaConverterFactory.
*
* @param factory the factory to register
*/
public void register(final DataCellToJavaConverterFactory<?, ?> factory) {
if (factory == null) {
throw new IllegalArgumentException("factory must not be null");
}
final ConversionKey key = new ConversionKey(factory);
ArrayList<DataCellToJavaConverterFactory<?, ?>> list = m_converterFactories.get(key);
if (list == null) {
list = new ArrayList<>();
m_converterFactories.put(key, list);
}
list.add(factory);
final Class<?> destType = factory.getDestinationType();
Set<DataCellToJavaConverterFactory<?, ?>> byDestType = m_byDestinationType.get(destType);
if (byDestType == null) {
byDestType = new LinkedHashSet<>();
m_byDestinationType.put(destType, byDestType);
}
byDestType.add(factory);
final Class<?> sourceType = factory.getSourceType();
Set<DataCellToJavaConverterFactory<?, ?>> bySourceType = m_bySourceType.get(sourceType);
if (bySourceType == null) {
bySourceType = new LinkedHashSet<>();
m_bySourceType.put(sourceType, bySourceType);
}
bySourceType.add(factory);
final DataCellToJavaConverterFactory<?, ?> previous = m_byIdentifier.put(factory.getIdentifier(), factory);
if (previous != null) {
LOGGER.coding("DataCellToJavaConverterFactory identifier is not unique (" + factory.getIdentifier() + ")");
}
}
use of org.knime.core.data.convert.ConversionKey in project knime-core by knime.
the class JavaToDataCellConverterRegistry method getConverterFactories.
/**
* Get all {@link JavaToDataCellConverterFactory converter factories} which create {@link JavaToDataCellConverter}s
* that convert <code>sourceType</code> into <code>destType</code>. If you do not require more than one converter
* factory, you should consider using {@link #getPreferredConverterFactory(Class, DataType)} instead.
*
* @param sourceType Source type to convert
* @param destType {@link DataType} to convert to
* @return collection of {@link JavaToDataCellConverterFactory converter factories} which create converters which
* convert from <code>sourceType</code> to <code>destType</code>
* @param <S> A JavaToDataCellConverter type (letting java infer the type is highly recommended)
*/
// we only put JavaToDataCellConverter<T> into the map for Class<T>
public <S> Collection<JavaToDataCellConverterFactory<S>> getConverterFactories(final Class<S> sourceType, final DataType destType) {
final LinkedBlockingQueue<Class<?>> classes = new LinkedBlockingQueue<>();
classes.add(sourceType);
Class<?> curClass = null;
final ArrayList<JavaToDataCellConverterFactory<S>> factories = new ArrayList<>();
while ((curClass = classes.poll()) != null) {
final ArrayList<JavaToDataCellConverterFactory<?>> newFactories = m_factories.get(new ConversionKey(curClass, destType));
if (newFactories != null) {
factories.addAll((Collection<? extends JavaToDataCellConverterFactory<S>>) newFactories);
}
/* check if a supertype has a compatible converter factory */
classes.addAll(Arrays.asList(curClass.getInterfaces()));
if (curClass.getSuperclass() != null) {
classes.add(curClass.getSuperclass());
}
}
if (destType.isCollectionType() && sourceType.isArray()) {
final Collection<? extends JavaToDataCellConverterFactory<S>> elementFactories = (Collection<? extends JavaToDataCellConverterFactory<S>>) getConverterFactories(sourceType.getComponentType(), destType.getCollectionElementType());
final List<?> arrayFactories = elementFactories.stream().map((elementFactory) -> getArrayConverterFactory((JavaToDataCellConverterFactory<?>) elementFactory)).collect(Collectors.toList());
factories.addAll((Collection<? extends JavaToDataCellConverterFactory<S>>) arrayFactories);
}
return factories;
}
use of org.knime.core.data.convert.ConversionKey in project knime-core by knime.
the class DataCellToJavaConverterRegistry method getConverterFactories.
/**
* Get all {@link DataCellToJavaConverterFactory} which create {@link DataCellToJavaConverter}s that convert
* <code>sourceType</code> into <code>destType</code>. If you do not require more than one converter factory, you
* should consider using {@link #getPreferredConverterFactory(DataType, Class)} instead.
*
* @param sourceType Type the created {@link DataCellToJavaConverter}s convert from
* @param destType Type the created {@link DataCellToJavaConverter}s convert to
* @return collection of {@link DataCellToJavaConverterFactory converter factories} which create converters which
* convert from <code>sourceType</code> into <code>destType</code>.
*/
public <D> Collection<DataCellToJavaConverterFactory<? extends DataValue, D>> getConverterFactories(final DataType sourceType, final Class<D> destType) {
if (sourceType.equals(DataType.getMissingCell().getType())) {
return Arrays.asList(MissingToNullConverterFactory.getInstance());
}
final Collection<DataCellToJavaConverterFactory<? extends DataValue, D>> allFactories = new ArrayList<>();
for (final Class<? extends DataValue> curClass : sourceType.getValueClasses()) {
if (DataValue.class.equals(curClass)) {
// priority (e.g. DataValue.toString() converter)
continue;
}
final ArrayList<DataCellToJavaConverterFactory<?, ?>> factories = m_converterFactories.get(new ConversionKey(curClass, destType));
if (factories != null) {
allFactories.addAll((Collection<? extends DataCellToJavaConverterFactory<DataCell, D>>) factories);
}
}
final ArrayList<DataCellToJavaConverterFactory<?, ?>> factories = m_converterFactories.get(new ConversionKey(DataValue.class, destType));
if (factories != null) {
allFactories.addAll((Collection<? extends DataCellToJavaConverterFactory<DataCell, D>>) factories);
}
if (sourceType.isCollectionType() && destType.isArray()) {
allFactories.addAll(getCollectionConverterFactories(sourceType, destType));
}
return allFactories;
}
Aggregations