use of org.mule.runtime.core.api.transformer.Converter in project mule by mulesoft.
the class GraphTransformerResolver method resolve.
@Override
public Transformer resolve(DataType source, DataType result) throws ResolverException {
String cacheKey = getDataTypeSourceResultPairHash(source, result);
readWriteLock.readLock().lock();
try {
if (cache.containsKey(cacheKey)) {
return (Converter) cache.get(cacheKey);
}
} finally {
readWriteLock.readLock().unlock();
}
List<Converter> converters = converterFilter.filter(lookupStrategyTransformation.lookupConverters(source, result), source, result);
if (converters.size() > 1) {
throw new ResolverException(CoreMessages.transformHasMultipleMatches(source.getType(), result.getType(), converters));
}
Transformer converter = (converters.size() == 0) ? null : converters.get(0);
readWriteLock.writeLock().lock();
try {
cache.put(cacheKey, converter);
} finally {
readWriteLock.writeLock().unlock();
}
return converter;
}
use of org.mule.runtime.core.api.transformer.Converter in project mule by mulesoft.
the class PriorityWeightingConverterFilter method filter.
@Override
public List<Converter> filter(List<Converter> converters, DataType source, DataType result) {
if (converters.size() == 0) {
return Collections.emptyList();
}
List<TransformerWeighting> weightings = getTransformerWeightings(converters, source.getType(), result.getType());
TransformerWeighting transformerWeighting = weightings.get(weightings.size() - 1);
int index = weightings.size() - 2;
List<Converter> heaviestConverter = new LinkedList<>();
heaviestConverter.add((Converter) transformerWeighting.getTransformer());
for (; index > -1; --index) {
if (weightings.get(index).compareTo(transformerWeighting) < 0) {
break;
} else {
heaviestConverter.add((Converter) weightings.get(index).getTransformer());
}
}
return heaviestConverter;
}
use of org.mule.runtime.core.api.transformer.Converter in project mule by mulesoft.
the class TransformationGraphLookupStrategy method lookupConverters.
/**
* Looks for {@link Converter} to convert from the source to the target data types.
* All {@link Converter}s found will have a source that is compatible with {@param source}
* (since if a converter can convert a super type, it should be able to convert any type that extends it)
* and a target such that {@param target} isCompatibleWith() the {@link Converter}'s one
* (since if we want a converter that returns an specific type, it should return exactly that type or any type that extends it.)
*
* @param source data type to be converted
* @param target data type to be converted to
* @return a list of {@link Converter} that are able to convert from the source to the target data types.
*/
public List<Converter> lookupConverters(DataType source, DataType target) {
List<Converter> converters = new LinkedList<>();
if (!graph.containsVertexOrSuper(source)) {
return converters;
}
if (!graph.containsVertexOrSub(target)) {
return converters;
}
// Since we should have all possible transformations we should check for them all.
List<DataType> compatibleSourceVertexes = graph.getSuperVertexes(source);
List<DataType> compatibleTargetVertexes = graph.getSubVertexes(target);
List<List<TransformationEdge>> transformationPaths = new LinkedList<>();
for (DataType sourceVertex : compatibleSourceVertexes) {
for (DataType targetVertex : compatibleTargetVertexes) {
transformationPaths.addAll(findTransformationPaths(sourceVertex, targetVertex, new HashSet<>()));
}
}
converters = createConverters(transformationPaths);
return converters;
}
use of org.mule.runtime.core.api.transformer.Converter in project mule by mulesoft.
the class TransformationGraphLookupStrategy method createConverters.
private List<Converter> createConverters(List<List<TransformationEdge>> transformationPaths) {
// Using a set instead of a list for when a path of just one converter is found multiple times.
Set<Converter> converters = new HashSet<>();
for (List<TransformationEdge> transformationPath : transformationPaths) {
Converter[] pathConverters = new Converter[transformationPath.size()];
int index = 0;
for (TransformationEdge edge : transformationPath) {
pathConverters[index++] = edge.getConverter();
}
Converter converter;
if (transformationPath.size() == 1) {
converter = transformationPath.get(0).getConverter();
} else {
converter = new CompositeConverter(pathConverters);
}
converters.add(converter);
}
return new ArrayList<>(converters);
}
use of org.mule.runtime.core.api.transformer.Converter in project mule by mulesoft.
the class CompositeConverterTestCase method isSourceDataTypeSupported.
@Test
public void isSourceDataTypeSupported() {
Converter converter = mock(Converter.class);
when(converter.isSourceDataTypeSupported(DataType.STRING)).thenReturn(true);
CompositeConverter chain = new CompositeConverter(converter);
assertTrue(chain.isSourceDataTypeSupported(DataType.STRING));
}
Aggregations