From 4074242e0b77b4f1091fef56b753993cc78999e7 Mon Sep 17 00:00:00 2001 From: "moandji.ezana" Date: Tue, 28 Apr 2015 23:31:59 +0200 Subject: [PATCH] Using a private implementation of Map.Entry instead of public Toml.Entry class --- src/main/java/com/moandjiezana/toml/Toml.java | 17 +++++++++---- .../com/moandjiezana/toml/IteratorTest.java | 24 ++++++++++++++----- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/moandjiezana/toml/Toml.java b/src/main/java/com/moandjiezana/toml/Toml.java index e717a04..a4890f1 100644 --- a/src/main/java/com/moandjiezana/toml/Toml.java +++ b/src/main/java/com/moandjiezana/toml/Toml.java @@ -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 entrySet() { - Set entries = new LinkedHashSet(); + public Set> entrySet() { + Set> entries = new LinkedHashSet>(); for (Map.Entry entry : values.entrySet()) { Class entryClass = entry.getValue().getClass(); @@ -311,18 +311,25 @@ public class Toml { return entries; } - public static class Entry { + private class Entry implements Map.Entry { 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; diff --git a/src/test/java/com/moandjiezana/toml/IteratorTest.java b/src/test/java/com/moandjiezana/toml/IteratorTest.java index e94d148..10879a2 100644 --- a/src/test/java/com/moandjiezana/toml/IteratorTest.java +++ b/src/test/java/com/moandjiezana/toml/IteratorTest.java @@ -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 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 entry = toml.entrySet().iterator().next(); assertEquals("list", entry.getKey()); assertThat((List) 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 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 entry = toml.entrySet().iterator().next(); List tableArray = (List) entry.getValue(); assertEquals("table_array", entry.getKey()); @@ -68,7 +72,7 @@ public class IteratorTest { Toml toml = new Toml().parse(file("multiple")); Map entries = new HashMap(); - for (Toml.Entry entry : toml.entrySet()) { + for (Map.Entry entry : toml.entrySet()) { entries.put(entry.getKey(), entry.getValue()); } @@ -79,6 +83,14 @@ public class IteratorTest { assertThat(((List) entries.get("e")), hasSize(1)); } + @Test + public void should_not_support_setValue_method() throws Exception { + Map.Entry 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); }