Toml#getList() returns null if key is missing

This commit is contained in:
moandji.ezana 2015-04-29 20:27:31 +02:00
parent 4d32f77ce0
commit a342b1fef6
3 changed files with 9 additions and 16 deletions

View file

@ -153,16 +153,12 @@ public class Toml {
/**
* @param key a TOML key
* @param <T> type of list items
* @return an empty {@link List} if the key is not found
* @return <code>null</code> if the key is not found
*/
public <T> List<T> getList(String key) {
@SuppressWarnings("unchecked")
List<T> list = (List<T>) get(key);
if (list == null) {
return Collections.emptyList();
}
return list;
}
@ -170,13 +166,12 @@ public class Toml {
* @param key a TOML key
* @param defaultValue a list of default values
* @param <T> type of list items
* @return an empty {@link List} is the key is not found
* @return <code>null</code> is the key is not found
*/
public <T> List<T> getList(String key, List<T> defaultValue) {
@SuppressWarnings("unchecked")
List<T> val = (List<T>) get(key);
List<T> list = getList(key);
return val != null ? val : defaultValue;
return list != null ? list : defaultValue;
}
public Boolean getBoolean(String key) {

View file

@ -4,8 +4,8 @@ import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.Arrays;
@ -25,10 +25,10 @@ public class ArrayTest {
}
@Test
public void should_return_empty_list_if_no_value_for_key() throws Exception {
public void should_return_null_if_no_value_for_key() throws Exception {
Toml toml = new Toml().parse("");
assertTrue(toml.<String>getList("a").isEmpty());
assertNull(toml.getList("a"));
}
@Test

View file

@ -1,9 +1,7 @@
package com.moandjiezana.toml;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
@ -61,7 +59,7 @@ public class TableTest {
assertNull(toml.getString("a.b"));
assertNull(toml.getString("a.b[0].c"));
assertThat(toml.getList("a.b"), hasSize(0));
assertNull(toml.getList("a.b"));
assertTrue(toml.getTable("a.b").isEmpty());
assertTrue(toml.getTable("a.b[0]").isEmpty());
}