Added Toml#contains(String) and Toml#containsXxx(String) methods

This commit is contained in:
moandji.ezana 2015-06-24 01:07:34 +02:00
parent 0cea9ae187
commit 38666593dd
3 changed files with 133 additions and 6 deletions

View file

@ -221,6 +221,19 @@ Toml toml = new Toml().parse("a = 1");
Map<String, Object> map = toml.to(Map.class);
```
`Toml#contains(String)` verifies that the instance contains a key of any type (primitive, table or array of tables) of the given name. `Toml#containsKey(String)`, `Toml#containsTable(String)` and `Toml#containsTableArray(String)` return true only if a key exists and is a primitive, table or array of tables, respectively. Compound keys can be used to check existence at any depth.
```java
Toml toml = new Toml().parse("a = 1");
toml.contains("a"); // true
toml.conatinsKey("a"); // true
toml.containsTable("a"); // false
toml.containsTableArray("a"); // false
```
### Limitations
Date precision is limited to milliseconds.

View file

@ -9,7 +9,6 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashSet;
@ -234,6 +233,44 @@ public class Toml {
return tables;
}
/**
* @param key a key name, can be compound (eg. a.b.c)
* @return true if key is present
*/
public boolean contains(String key) {
return get(key) != null;
}
/**
* @param key a key name, can be compound (eg. a.b.c)
* @return true if key is present and is a primitive
*/
public boolean containsKey(String key) {
Object object = get(key);
return object != null && !(object instanceof Map) && !(object instanceof List);
}
/**
* @param key a key name, can be compound (eg. a.b.c)
* @return true if key is present and is a table
*/
public boolean containsTable(String key) {
Object object = get(key);
return object != null && (object instanceof Map);
}
/**
* @param key a key name, can be compound (eg. a.b.c)
* @return true if key is present and is a table array
*/
public boolean containsTableArray(String key) {
Object object = get(key);
return object != null && (object instanceof List);
}
public boolean isEmpty() {
return values.isEmpty();
}
@ -296,12 +333,10 @@ public class Toml {
entries.add(new Toml.Entry(entry.getKey(), getTable(entry.getKey())));
} else if (List.class.isAssignableFrom(entryClass)) {
List<?> value = (List<?>) entry.getValue();
if (value.isEmpty()) {
entries.add(new Toml.Entry(entry.getKey(), Collections.emptyList()));
} else if (value.get(0) instanceof Map) {
if (!value.isEmpty() && value.get(0) instanceof Map) {
entries.add(new Toml.Entry(entry.getKey(), getTables(entry.getKey())));
} else {
entries.add(new Toml.Entry(entry.getKey(), entry.getValue()));
entries.add(new Toml.Entry(entry.getKey(), value));
}
} else {
entries.add(new Toml.Entry(entry.getKey(), entry.getValue()));
@ -371,7 +406,7 @@ public class Toml {
}
private Toml(Toml defaults, Map<String, Object> values) {
this.values = values != null ? values : Collections.<String, Object>emptyMap();
this.values = values;
this.defaults = defaults;
}
}

View file

@ -0,0 +1,79 @@
package com.moandjiezana.toml;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class ContainsTest {
@Test
public void should_contain_top_level_of_any_type() throws Exception {
Toml toml = new Toml().parse("a = 1 \n [b] \n b1 = 1 \n [[c]] \n c1 = 1");
assertTrue(toml.contains("a"));
assertTrue(toml.contains("b"));
assertTrue(toml.contains("c"));
assertFalse(toml.contains("d"));
}
@Test
public void should_contain_nested_of_any_type() throws Exception {
Toml toml = new Toml().parse("[a] \n a1 = 1 \n [[b]] \n b1 = 1 \n [[b]] \n b1 = 2 \n b2 = 3");
assertTrue(toml.contains("a.a1"));
assertTrue(toml.contains("b[0].b1"));
assertTrue(toml.contains("b[1].b1"));
assertFalse(toml.contains("b[2].b1"));
assertFalse(toml.contains("c.d"));
}
@Test
public void should_contain_key() throws Exception {
Toml toml = new Toml().parse("a = 1 \n [b] \n b1 = 1 \n [[c]] \n c1 = 1");
assertTrue(toml.containsKey("a"));
assertTrue(toml.containsKey("b.b1"));
assertTrue(toml.containsKey("c[0].c1"));
assertFalse(toml.containsKey("b"));
assertFalse(toml.containsKey("c"));
assertFalse(toml.containsKey("d"));
}
@Test
public void should_contain_table() throws Exception {
Toml toml = new Toml().parse("a = 1 \n [b] \n b1 = 1 \n [b.b2] \n [[c]] \n c1 = 1 \n [c.c2]");
assertTrue(toml.containsTable("b"));
assertTrue(toml.containsTable("b.b2"));
assertTrue(toml.containsTable("c[0].c2"));
assertFalse(toml.containsTable("a"));
assertFalse(toml.containsTable("b.b1"));
assertFalse(toml.containsTable("c"));
assertFalse(toml.containsTable("c[0].c1"));
assertFalse(toml.containsTable("d"));
}
@Test
public void should_contain_table_array() throws Exception {
Toml toml = new Toml().parse("a = 1 \n [b] \n b1 = 1 \n [[c]] \n c1 = 1 \n [c.c2] \n [[c]] \n [[c.c3]] \n c4 = 4");
assertTrue(toml.containsTableArray("c"));
assertTrue(toml.containsTableArray("c[1].c3"));
assertFalse(toml.containsTableArray("a"));
assertFalse(toml.containsTableArray("b"));
assertFalse(toml.containsTableArray("b.b1"));
assertFalse(toml.containsTableArray("c[1].c3[0].c4"));
assertFalse(toml.containsTableArray("d"));
}
@Test
public void should_not_contain_when_parent_table_is_missing() throws Exception {
Toml toml = new Toml().parse("a = \"1\"");
assertFalse(toml.contains("b.b1"));
assertFalse(toml.containsKey("b.b1"));
assertFalse(toml.containsTable("b.b1"));
assertFalse(toml.containsTableArray("b.b1"));
}
}