org.klojang.path.CollectionSegmentReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of klojang-invoke Show documentation
Show all versions of klojang-invoke Show documentation
Klojang Invoke is a Java module focused on path-based object access and dynamic
invocation. Its central classes are the Path class and the PathWalker class. The
Path class captures a path through an object graph. For example
"employee.address.city". The PathWalker class lets you read from and write to
a wide variety of types using Path objects.
The newest version!
package org.klojang.path;
import java.util.Collection;
import java.util.Iterator;
import java.util.OptionalInt;
import static org.klojang.path.PathWalkerException.indexExpected;
import static org.klojang.path.PathWalkerException.indexOutOfBounds;
import static org.klojang.convert.NumberMethods.toInt;
@SuppressWarnings("rawtypes")
final class CollectionSegmentReader extends SegmentReader {
CollectionSegmentReader(boolean suppressExceptions,
KeyDeserializer keyDeserializer) {
super(suppressExceptions, keyDeserializer);
}
@Override
Object read(Collection collection, Path path, int segment) {
OptionalInt opt = toInt(path.segment(segment));
if (opt.isEmpty()) {
return deadEnd(indexExpected(path, segment));
}
int idx = opt.getAsInt();
if (idx < collection.size()) {
Iterator iter = collection.iterator();
for (; idx != 0 && iter.hasNext(); --idx, iter.next())
;
if (iter.hasNext()) {
return new ObjectReader(se, kd).read(iter.next(), path, ++segment);
}
}
return deadEnd(indexOutOfBounds(path, segment));
}
}