Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replaced map by iterator in parser
  • Loading branch information
Der-Zauberer committed Oct 16, 2022
commit aab95048b51c42ca23f2f5cbe52f8057da44cd41
6 changes: 3 additions & 3 deletions src/eu/derzauberer/javautils/parser/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
* }
* <pre>
*
* @see {@link TreeMap}, {@link KeyParser}
* @see {@link TreeMap}, {@link KeyValueParser}
*/
public class JsonParser extends KeyParser {
public class JsonParser extends KeyValueParser {

public JsonParser() {
super();
Expand Down Expand Up @@ -251,7 +251,7 @@ private String out(boolean oneliner, int offset) {
* {@inheritDoc}
*/
@Override
protected KeyParser getImplementationInstance() {
protected KeyValueParser getImplementationInstance() {
return new JsonParser();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import eu.derzauberer.javautils.util.DataUtil;

/**
* The class provides a parser based on a {@link TreeMap} and basic parser
* The class provides a parser based on key-value pairs and basic parser
* operations. Input and output operation are based on keys. Each key represents
* a value, but the value can be null. A string key is a path separated by dots.
* Stored values can be null.<br>
Expand All @@ -26,30 +26,30 @@
* String string = parser.get("my.path", String.class);
* </pre>
*
* @see {@link TreeMap}, {@link Parser}
* @see {@link Parser}
*/
public abstract class KeyParser implements Parser {
public abstract class KeyValueParser implements Parser {

//TODO Collections and Maps implementations

private final ArrayList<String> structrue = new ArrayList<>();
private final HashMap<String, Object> entries = new HashMap<>();

public KeyParser() {}
public KeyValueParser() {}

public KeyParser(final String string) {
public KeyValueParser(final String string) {
parse(string);
}

public KeyParser(Map<String, ?> map) {
public KeyValueParser(Map<String, ?> map) {
map.forEach(this::setObject);
}

/**
* Set the value to a given key. A key represents a value, but the value can be
* null. A string key is a path separated by dots. Supported types are primitive
* types and their wrappers, {@link String}, {@link Collection} and
* superclasses, {@link Map} and superclasses and {@link KeyParser}. All other
* superclasses, {@link Map} and superclasses and {@link KeyValueParser}. All other
* objects will be saved as {@link String} by their <tt>toString()</tt> method.
* The value will be overwritten, if the key already stores a value.<br>
* Example:<br>
Expand Down Expand Up @@ -183,24 +183,20 @@ public List<String> getKeys(final String key) {
.filter(string -> string.startsWith(Objects.requireNonNull(key)))
.collect(Collectors.toList());
}

/**
* Returns a {@link Map} representation of the parser. The map may not have
* the correct order of the keys. Changes of this map doesn't have any impact on
* the original parser.
* @return a {@link Map} representation of the parser
* Iterates over all entries in the parser.
* @param iterates over all entries by key and value
*/
public Map<String, Object> getMap() {
final Map<String, Object> map = new TreeMap<>();
for (String key : structrue) map.put(key, entries.get(key));
return map;
public void forEach(BiConsumer<String, Object> action) {
for (String key : structrue) action.accept(key, entries.get(key));
}

/**
* Set the value to a given key. A key represents a value, but the value can be
* null. A string key is a path separated by dots. Supported types are primitive
* types and their wrappers, {@link String}, {@link Collection} and
* superclasses, {@link Map} and superclasses and {@link KeyParser}. All other
* superclasses, {@link Map} and superclasses and {@link KeyValueParser}. All other
* objects will be saved as {@link String} by their <tt>toString()</tt> method.
* The value will be overwritten, if the key already stores a value.<br>
* @param key the path, which represents to the value
Expand All @@ -209,13 +205,14 @@ public Map<String, Object> getMap() {
*/
protected void setObject(final String key, final Object value) {
Objects.requireNonNull(key);
if (value instanceof Map<?, ?> || value instanceof KeyParser) {
Map<?, ?> map;
if (value instanceof Map<?, ?>) map = (Map<?, ?>) value;
else map = ((KeyParser) value).getMap();
map.forEach((mapKey, mapValue) -> {
if (value instanceof Map<?, ?>) {
((Map<?, ?>) value).forEach((mapKey, mapValue) -> {
setObject(key + "." + mapKey, mapValue);
});
} else if (value instanceof KeyValueParser) {
((KeyValueParser) value).forEach((parserKey, parserValue) -> {
setObject(key + "." + parserKey, parserValue);
});
} else {
entries.put(key, value);
if (structrue.contains(key)) return;
Expand Down Expand Up @@ -253,7 +250,7 @@ protected Object getObject(final String key) {
} else if (list.size() == 1) {
return entries.get(key);
} else {
KeyParser parser = getImplementationInstance();
KeyValueParser parser = getImplementationInstance();
list.forEach(path -> parser.set(path.substring(key.length()), entries.get(path)));
return parser;
}
Expand Down Expand Up @@ -291,6 +288,6 @@ protected HashMap<String, Object> getEntries() {
* Create an instance of the KeyParser implementation and returns it.
* @return the instance of the implementation
*/
protected abstract KeyParser getImplementationInstance();
protected abstract KeyValueParser getImplementationInstance();

}