use of com.oath.cyclops.types.traversable.IterableX in project cyclops by aol.
the class IterableXDeserializer method deserialize.
@Override
public IterableX<?> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
if (!p.isExpectedStartArrayToken()) {
return (IterableX) ctxt.handleUnexpectedToken(handledType(), p);
}
List multi = new ArrayList();
JsonToken t;
while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
Object value;
if (t == JsonToken.VALUE_NULL) {
value = null;
} else if (typeDeserializerForValue == null) {
value = valueDeserializer.deserialize(p, ctxt);
} else {
value = valueDeserializer.deserializeWithType(p, ctxt, typeDeserializerForValue);
}
multi.add(value);
}
if (Vector.class.isAssignableFrom(elementType))
return Vector.fromIterable(multi);
if (Seq.class.isAssignableFrom(elementType))
return Seq.fromIterable(multi);
if (LazySeq.class.isAssignableFrom(elementType))
return LazySeq.fromIterable(multi);
if (LazyString.class.isAssignableFrom(elementType))
return LazyString.fromLazySeq((LazySeq) LazySeq.fromIterable(multi));
if (IntMap.class.isAssignableFrom(elementType))
return IntMap.fromIterable(multi);
if (ReactiveSeq.class.isAssignableFrom(elementType))
return ReactiveSeq.fromIterable(multi);
if (Streamable.class.isAssignableFrom(elementType))
return Streamable.fromIterable(multi);
if (BankersQueue.class.isAssignableFrom(elementType))
return BankersQueue.fromIterable(multi);
if (Bag.class.isAssignableFrom(elementType))
return Bag.fromIterable(multi);
if (cyclops.data.HashSet.class.isAssignableFrom(elementType))
return HashSet.fromIterable(multi);
if (cyclops.data.TrieSet.class.isAssignableFrom(elementType))
return TrieSet.fromIterable(multi);
if (cyclops.data.TreeSet.class.isAssignableFrom(elementType))
return TreeSet.fromIterable(multi, (Comparator) Comparator.naturalOrder());
Optional<Method> m = streamMethod.computeIfAbsent(elementType, c -> Stream.of(c.getMethods()).filter(method -> "fromIterable".equals(method.getName())).filter(method -> method.getParameterCount() == 1).findFirst().map(m2 -> {
m2.setAccessible(true);
return m2;
}));
IterableX x = m.map(mt -> (IterableX) new Invoker().executeMethod(multi, mt, itX)).orElse(null);
return x;
}
Aggregations