From 7f0ea04e99f4f38c287f37adb55af71b7d971533 Mon Sep 17 00:00:00 2001 From: Nick Jiang Date: Thu, 26 Mar 2015 16:46:39 +0800 Subject: [PATCH 1/4] Ignore the folder "target". --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target From bc04eec538fdf6339de7126d17880c9316e5a1c8 Mon Sep 17 00:00:00 2001 From: Nick Jiang Date: Thu, 26 Mar 2015 16:50:48 +0800 Subject: [PATCH 2/4] Returns the default value if the key was not found. --- src/main/java/com/moandjiezana/toml/Toml.java | 25 ++++++ .../moandjiezana/toml/DefaultValueTest.java | 85 +++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/test/java/com/moandjiezana/toml/DefaultValueTest.java diff --git a/src/main/java/com/moandjiezana/toml/Toml.java b/src/main/java/com/moandjiezana/toml/Toml.java index 882654f..9b75e01 100644 --- a/src/main/java/com/moandjiezana/toml/Toml.java +++ b/src/main/java/com/moandjiezana/toml/Toml.java @@ -136,10 +136,20 @@ public class Toml { return (String) get(key); } + public String getString(String key, String defaultValue) { + Object val = get(key); + return val == null ? defaultValue : (String) val; + } + public Long getLong(String key) { return (Long) get(key); } + public Long getLong(String key, Long defaultValue) { + Object val = get(key); + return val == null ? defaultValue : (Long) val; + } + /** * @param key a TOML key * @param type of list items @@ -160,14 +170,29 @@ public class Toml { return (Boolean) get(key); } + public Boolean getBoolean(String key, Boolean defaultValue) { + Object val = get(key); + return val == null ? defaultValue : (Boolean) val; + } + public Date getDate(String key) { return (Date) get(key); } + public Date getDate(String key, Date defaultValue) { + Object val = get(key); + return val == null ? defaultValue : (Date) val; + } + public Double getDouble(String key) { return (Double) get(key); } + public Double getDouble(String key, Double defaultValue) { + Object val = get(key); + return val == null ? defaultValue : (Double) val; + } + /** * @param key A table name, not including square brackets. * @return A new Toml instance. Empty if no value is found for key. diff --git a/src/test/java/com/moandjiezana/toml/DefaultValueTest.java b/src/test/java/com/moandjiezana/toml/DefaultValueTest.java new file mode 100644 index 0000000..016ba7d --- /dev/null +++ b/src/test/java/com/moandjiezana/toml/DefaultValueTest.java @@ -0,0 +1,85 @@ +package com.moandjiezana.toml; + +import java.util.Calendar; +import java.util.TimeZone; +import java.util.Date; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class DefaultValueTest { + private static final TimeZone UTC = TimeZone.getTimeZone("UTC"); + + @Test + public void should_get_string() throws Exception { + Toml toml = new Toml().parse("s = \"string value\""); + assertEquals("string value", toml.getString("s", "default string value")); + } + + @Test + public void should_get_string_default_value() throws Exception { + Toml toml = new Toml().parse(""); + assertEquals("default string value", toml.getString("s", "default string value")); + } + + @Test + public void should_get_long() throws Exception { + Toml toml = new Toml().parse("n = 1001"); + assertEquals(Long.valueOf(1001), toml.getLong("n", 1002L)); + } + + @Test + public void should_get_long_default_value() throws Exception { + Toml toml = new Toml().parse(""); + assertEquals(Long.valueOf(1002), toml.getLong("n", 1002L)); + } + + @Test + public void should_get_double() throws Exception { + Toml toml = new Toml().parse("n = 0.5"); + assertEquals(Double.valueOf(0.5), toml.getDouble("n", Double.valueOf(0.6))); + } + + @Test + public void should_get_double_default_value() throws Exception { + Toml toml = new Toml().parse(""); + assertEquals(Double.valueOf(0.6), toml.getDouble("n", Double.valueOf(0.6))); + } + + @Test + public void should_get_boolean() throws Exception { + Toml toml = new Toml().parse("b = true"); + assertEquals(Boolean.TRUE, toml.getBoolean("b", Boolean.FALSE)); + } + + @Test + public void should_get_boolean_default_value() throws Exception { + Toml toml = new Toml().parse(""); + assertEquals(Boolean.FALSE, toml.getBoolean("b", Boolean.FALSE)); + } + + @Test + public void should_get_date() throws Exception { + Toml toml = new Toml().parse("d = 2011-11-10T13:12:00Z"); + + Calendar calendar = Calendar.getInstance(UTC); + calendar.set(2011, Calendar.NOVEMBER, 10, 13, 12, 00); + calendar.set(Calendar.MILLISECOND, 0); + Date expected = calendar.getTime(); + calendar.set(2012, Calendar.NOVEMBER, 10, 13, 12, 00); + Date defaultValue = calendar.getTime(); + assertEquals(expected, toml.getDate("d", defaultValue)); + } + + @Test + public void should_get_date_default_value() throws Exception { + Toml toml = new Toml().parse(""); + + Calendar calendar = Calendar.getInstance(UTC); + calendar.set(2012, Calendar.NOVEMBER, 10, 13, 12, 00); + calendar.set(Calendar.MILLISECOND, 0); + Date defaultValue = calendar.getTime(); + assertEquals(defaultValue, toml.getDate("d", defaultValue)); + } +} From 3773c4eadf3dfcb2e1a34961af230225230f405d Mon Sep 17 00:00:00 2001 From: Nick Jiang Date: Fri, 27 Mar 2015 09:41:43 +0800 Subject: [PATCH 3/4] Remove the ignores use global .gitignore instead. --- .gitignore | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index eb5a316..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -target From 414bd396fac285abb2c039a7499b72aef787fc53 Mon Sep 17 00:00:00 2001 From: Nick Jiang Date: Fri, 27 Mar 2015 10:13:09 +0800 Subject: [PATCH 4/4] Call original method to avoid duplicated work. --- src/main/java/com/moandjiezana/toml/Toml.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/moandjiezana/toml/Toml.java b/src/main/java/com/moandjiezana/toml/Toml.java index 9b75e01..0da535d 100644 --- a/src/main/java/com/moandjiezana/toml/Toml.java +++ b/src/main/java/com/moandjiezana/toml/Toml.java @@ -137,8 +137,8 @@ public class Toml { } public String getString(String key, String defaultValue) { - Object val = get(key); - return val == null ? defaultValue : (String) val; + String val = getString(key); + return val == null ? defaultValue : val; } public Long getLong(String key) { @@ -146,8 +146,8 @@ public class Toml { } public Long getLong(String key, Long defaultValue) { - Object val = get(key); - return val == null ? defaultValue : (Long) val; + Long val = getLong(key); + return val == null ? defaultValue : val; } /** @@ -171,8 +171,8 @@ public class Toml { } public Boolean getBoolean(String key, Boolean defaultValue) { - Object val = get(key); - return val == null ? defaultValue : (Boolean) val; + Boolean val = getBoolean(key); + return val == null ? defaultValue : val; } public Date getDate(String key) { @@ -180,8 +180,8 @@ public class Toml { } public Date getDate(String key, Date defaultValue) { - Object val = get(key); - return val == null ? defaultValue : (Date) val; + Date val = getDate(key); + return val == null ? defaultValue : val; } public Double getDouble(String key) { @@ -189,8 +189,8 @@ public class Toml { } public Double getDouble(String key, Double defaultValue) { - Object val = get(key); - return val == null ? defaultValue : (Double) val; + Double val = getDouble(key); + return val == null ? defaultValue : val; } /**