Created
November 18, 2016 15:03
-
-
Save elucidator/03212f07cf8411f2a1995fb99f1af9d1 to your computer and use it in GitHub Desktop.
Iterable for keyvalue string
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 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