Returns the default value if the key was not found.

This commit is contained in:
Nick Jiang 2015-03-26 16:50:48 +08:00
parent 7f0ea04e99
commit bc04eec538
2 changed files with 110 additions and 0 deletions

View file

@ -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 <T> 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.

View file

@ -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));
}
}