mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-11 03:30:00 +00:00
Using a private implementation of Map.Entry instead of public Toml.Entry
class
This commit is contained in:
parent
68513b0851
commit
4074242e0b
2 changed files with 30 additions and 11 deletions
|
@ -284,10 +284,10 @@ public class Toml {
|
|||
}
|
||||
|
||||
/**
|
||||
* @return a {@link Set} of Toml.Entry instances, each exposing a name and a deserialised value. Modifications to this {@link Set} are not reflected in this Toml instance.
|
||||
* @return a {@link Set} of Map.Entry instances. Modifications to the {@link Set} are not reflected in this Toml instance. Entries are immutable, so {@link Map.Entry#setValue(Object)} throws an UnsupportedOperationException.
|
||||
*/
|
||||
public Set<Toml.Entry> entrySet() {
|
||||
Set<Toml.Entry> entries = new LinkedHashSet<Toml.Entry>();
|
||||
public Set<Map.Entry<String,Object>> entrySet() {
|
||||
Set<Map.Entry<String, Object>> entries = new LinkedHashSet<Map.Entry<String, Object>>();
|
||||
|
||||
for (Map.Entry<String, Object> entry : values.entrySet()) {
|
||||
Class<? extends Object> entryClass = entry.getValue().getClass();
|
||||
|
@ -311,18 +311,25 @@ public class Toml {
|
|||
return entries;
|
||||
}
|
||||
|
||||
public static class Entry {
|
||||
private class Entry implements Map.Entry<String, Object> {
|
||||
|
||||
private final String key;
|
||||
private final Object value;
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object setValue(Object value) {
|
||||
throw new UnsupportedOperationException("TOML entry values cannot be changed.");
|
||||
}
|
||||
|
||||
private Entry(String key, Object value) {
|
||||
this.key = key;
|
||||
|
|
|
@ -15,16 +15,21 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import com.moandjiezana.toml.testutils.Utils;
|
||||
|
||||
public class IteratorTest {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException expectedException = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void should_iterate_over_primitive() throws Exception {
|
||||
Toml toml = new Toml().parse(file("long"));
|
||||
Toml.Entry entry = toml.entrySet().iterator().next();
|
||||
Map.Entry<String, Object> entry = toml.entrySet().iterator().next();
|
||||
|
||||
assertEquals("long", entry.getKey());
|
||||
assertEquals(2L, entry.getValue());
|
||||
|
@ -34,8 +39,7 @@ public class IteratorTest {
|
|||
@SuppressWarnings("unchecked")
|
||||
public void should_iterate_over_list() throws Exception {
|
||||
Toml toml = new Toml().parse(file("list"));
|
||||
|
||||
Toml.Entry entry = toml.entrySet().iterator().next();
|
||||
Map.Entry<String, Object> entry = toml.entrySet().iterator().next();
|
||||
|
||||
assertEquals("list", entry.getKey());
|
||||
assertThat((List<String>) entry.getValue(), contains("a", "b", "c"));
|
||||
|
@ -44,7 +48,7 @@ public class IteratorTest {
|
|||
@Test
|
||||
public void should_iterate_over_table() throws Exception {
|
||||
Toml toml = new Toml().parse(file("table"));
|
||||
Toml.Entry entry = toml.entrySet().iterator().next();
|
||||
Map.Entry<String, Object> entry = toml.entrySet().iterator().next();
|
||||
|
||||
assertEquals("table", entry.getKey());
|
||||
assertEquals("a", ((Toml) entry.getValue()).getString("a"));
|
||||
|
@ -55,7 +59,7 @@ public class IteratorTest {
|
|||
public void should_iterate_over_table_array() throws Exception {
|
||||
Toml toml = new Toml().parse(file("table_array"));
|
||||
|
||||
Toml.Entry entry = toml.entrySet().iterator().next();
|
||||
Map.Entry<String, Object> entry = toml.entrySet().iterator().next();
|
||||
List<Toml> tableArray = (List<Toml>) entry.getValue();
|
||||
|
||||
assertEquals("table_array", entry.getKey());
|
||||
|
@ -68,7 +72,7 @@ public class IteratorTest {
|
|||
Toml toml = new Toml().parse(file("multiple"));
|
||||
|
||||
Map<String, Object> entries = new HashMap<String, Object>();
|
||||
for (Toml.Entry entry : toml.entrySet()) {
|
||||
for (Map.Entry<String, Object> entry : toml.entrySet()) {
|
||||
entries.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
|
@ -79,6 +83,14 @@ public class IteratorTest {
|
|||
assertThat(((List<Toml>) entries.get("e")), hasSize(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_not_support_setValue_method() throws Exception {
|
||||
Map.Entry<String, Object> entry = new Toml().parse("a = 1").entrySet().iterator().next();
|
||||
|
||||
expectedException.expect(UnsupportedOperationException.class);
|
||||
entry.setValue(2L);
|
||||
}
|
||||
|
||||
private File file(String name) {
|
||||
return Utils.file(getClass(), "/IteratorTest/" + name);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue