Skip to content

Instantly share code, notes, and snippets.

@elucidator
Created November 18, 2016 15:03
Show Gist options
  • Save elucidator/03212f07cf8411f2a1995fb99f1af9d1 to your computer and use it in GitHub Desktop.
Save elucidator/03212f07cf8411f2a1995fb99f1af9d1 to your computer and use it in GitHub Desktop.
Iterable for keyvalue string
class KeyValueReaderIterable extends KeyValueReader implements Iterable<Map.Entry<String, String>> {
/**
* Default constructor
* @param l line
*/
public KeyValueReaderIterable(String l) {
super(l);
}
@Override
public Iterator<Map.Entry<String, String>> iterator() {
return new Iterator<Map.Entry<String, String>>() {
@Override
public boolean hasNext() {
return available();
}
@Override
public Map.Entry<String, String> next() {
return nextEntry();
}
};
}
private String getKey() {
if (is('\"')) {
getc();
}
mark();
skipUntil('=');
return getMarkedSegment();
}
private String getValue() {
skipTo('"');
mark();
skipUntil('"');
return getMarkedSegment();
}
private Map.Entry<String, String> nextEntry() {
return new AbstractMap.SimpleEntry<>(getKey().trim(), getValue());
}
/**
* Stream a KVString
* @param data kv data string
* @return {@link Stream} of {@link Map.Entry}
*/
public static Stream<Map.Entry<String, String>> stream(final String data) {
return StreamSupport.stream(new KeyValueReaderIterable(data).spliterator(), true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment