Last active
February 5, 2016 10:24
-
-
Save gunnarmorling/980d7857cd57c766cc48 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface SingleValueExtractor<I, O> { | |
O extractValue(I input); | |
// only invoked if invalid; Property name enough as input? | |
Path.Node getNode(String property); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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