Skip to content

Instantly share code, notes, and snippets.

@gunnarmorling
Last active February 5, 2016 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gunnarmorling/980d7857cd57c766cc48 to your computer and use it in GitHub Desktop.
Save gunnarmorling/980d7857cd57c766cc48 to your computer and use it in GitHub Desktop.
interface SingleValueExtractor<I, O> {
O extractValue(I input);
// only invoked if invalid; Property name enough as input?
Path.Node getNode(String property);
}
interface MultiValueExtractor<I, O> {
ValueAndNodeIterator<O> extractValues(I input);
public interface ValueAndNodeIterator<O> {
boolean hasNext();
O next();
TypeVariable<?> typeVariable();
// only invoked if invalid; Property name enough as input?
Path.Node getNode(String property);
}
}
class MapExtractor implements MultiValueExtractor<Map, Object> {
public ValueAndNodeIterator<Object> extractValues(Map input) {
Set<Map.Entry<?, ?>> entrySet = input.entrySet();
final Iterator<Map.Entry> iterator = input.entrySet().iterator();
final TypeVariable<Class<Map>> k = Map.class.getTypeParameters()[0];
final TypeVariable<Class<Map>> v = Map.class.getTypeParameters()[1];
return new ValueAndNodeIterator<Object>() {
private boolean atKey = true;
private Map.Entry<?, ?> current;
public boolean hasNext() {
return iterator.hasNext();
}
public Object next() {
if ( atKey ) {
current = iterator.next();
atKey = false;
return current.getKey();
}
else {
atKey = true;
return current.getValue();
}
}
public TypeVariable<?> typeVariable() {
return atKey ? k : v;
}
public Node getNode(String property) {
// TODO Auto-generated method stub
return null;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment