Added Toml#entrySet()

This commit is contained in:
moandji.ezana 2015-04-28 11:57:19 +02:00
parent 5402ce22f3
commit a0bd307f58
7 changed files with 145 additions and 2 deletions

View file

@ -12,6 +12,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -39,7 +40,7 @@ import com.google.gson.JsonElement;
*
*/
public class Toml {
private static final Gson DEFAULT_GSON = new Gson();
private Map<String, Object> values = new HashMap<String, Object>();
@ -281,6 +282,50 @@ public class Toml {
return DEFAULT_GSON.fromJson(json, targetClass);
}
public Set<Toml.Entry> entrySet() {
Set<Toml.Entry> entries = new LinkedHashSet<Toml.Entry>();
for (Map.Entry<String, Object> entry : values.entrySet()) {
Class<? extends Object> entryClass = entry.getValue().getClass();
if (Map.class.isAssignableFrom(entryClass)) {
entries.add(new Toml.Entry(entry.getKey(), getTable(entry.getKey())));
} else if (List.class.isAssignableFrom(entryClass)) {
List<?> value = (List<?>) entry.getValue();
if (value.isEmpty()) {
entries.add(new Toml.Entry(entry.getKey(), Collections.emptyList()));
} else if (value.get(0) instanceof Map) {
entries.add(new Toml.Entry(entry.getKey(), getTables(entry.getKey())));
} else {
entries.add(new Toml.Entry(entry.getKey(), entry.getValue()));
}
} else {
entries.add(new Toml.Entry(entry.getKey(), entry.getValue()));
}
}
return entries;
}
public static class Entry {
private final String key;
private final Object value;
public String getKey() {
return key;
}
public Object getValue() {
return value;
}
private Entry(String key, Object value) {
this.key = key;
this.value = value;
}
}
@SuppressWarnings("unchecked")
private Object get(String key) {
@ -314,7 +359,7 @@ public class Toml {
return current;
}
private Toml(Toml defaults, Map<String, Object> values) {
this.values = values != null ? values : Collections.<String, Object>emptyMap();
this.defaults = defaults;

View file

@ -0,0 +1,85 @@
package com.moandjiezana.toml;
import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import com.moandjiezana.toml.testutils.Utils;
public class IteratorTest {
@Test
public void should_iterate_over_primitive() throws Exception {
Toml toml = new Toml().parse(file("long"));
Toml.Entry entry = toml.entrySet().iterator().next();
assertEquals("long", entry.getKey());
assertEquals(2L, entry.getValue());
}
@Test
@SuppressWarnings("unchecked")
public void should_iterate_over_list() throws Exception {
Toml toml = new Toml().parse(file("list"));
Toml.Entry entry = toml.entrySet().iterator().next();
assertEquals("list", entry.getKey());
assertThat((List<String>) entry.getValue(), contains("a", "b", "c"));
}
@Test
public void should_iterate_over_table() throws Exception {
Toml toml = new Toml().parse(file("table"));
Toml.Entry entry = toml.entrySet().iterator().next();
assertEquals("table", entry.getKey());
assertEquals("a", ((Toml) entry.getValue()).getString("a"));
}
@Test
@SuppressWarnings("unchecked")
public void should_iterate_over_table_array() throws Exception {
Toml toml = new Toml().parse(file("table_array"));
Toml.Entry entry = toml.entrySet().iterator().next();
List<Toml> tableArray = (List<Toml>) entry.getValue();
assertEquals("table_array", entry.getKey());
assertThat(tableArray, contains(instanceOf(Toml.class), instanceOf(Toml.class)));
}
@Test
@SuppressWarnings("unchecked")
public void should_iterate_over_multiple_entries() throws Exception {
Toml toml = new Toml().parse(file("multiple"));
Map<String, Object> entries = new HashMap<String, Object>();
for (Toml.Entry entry : toml.entrySet()) {
entries.put(entry.getKey(), entry.getValue());
}
assertThat(entries.keySet(), containsInAnyOrder("a", "b", "c", "e"));
assertThat(entries, hasEntry("a", (Object) "a"));
assertThat(entries, hasEntry("b", (Object) asList(1L, 2L, 3L)));
assertTrue(((Toml) entries.get("c")).getBoolean("d"));
assertThat(((List<Toml>) entries.get("e")), hasSize(1));
}
private File file(String name) {
return Utils.file(getClass(), "/IteratorTest/" + name);
}
}

View file

@ -0,0 +1 @@
list = ["a", "b", "c"]

View file

@ -0,0 +1 @@
long = 2

View file

@ -0,0 +1,7 @@
a = "a"
b = [1, 2, 3]
[c]
d = true
[[e]]

View file

@ -0,0 +1 @@
table = { a = "a" }

View file

@ -0,0 +1,3 @@
[[table_array]]
[[table_array]]