mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
Helper methods and unit tests for BdfList/Dictionary.
This commit is contained in:
@@ -4,11 +4,36 @@ import org.briarproject.api.Bytes;
|
||||
import org.briarproject.api.FormatException;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class BdfDictionary extends Hashtable<String, Object> {
|
||||
|
||||
public static final Object NULL_VALUE = new Object();
|
||||
|
||||
/**
|
||||
* Factory method for constructing dictionaries inline.
|
||||
* <pre>
|
||||
* BdfDictionary.of(
|
||||
* new BdfEntry("foo", foo),
|
||||
* new BdfEntry("bar", bar)
|
||||
* );
|
||||
* </pre>
|
||||
*/
|
||||
public static BdfDictionary of(Entry<String, Object>... entries) {
|
||||
BdfDictionary d = new BdfDictionary();
|
||||
for (Entry<String, Object> e : entries) d.put(e.getKey(), e.getValue());
|
||||
return d;
|
||||
}
|
||||
|
||||
public BdfDictionary() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BdfDictionary(Map<String, Object> m) {
|
||||
super(m);
|
||||
}
|
||||
|
||||
public Boolean getBoolean(String key) throws FormatException {
|
||||
Object o = get(key);
|
||||
if (o instanceof Boolean) return (Boolean) o;
|
||||
|
||||
32
briar-api/src/org/briarproject/api/data/BdfEntry.java
Normal file
32
briar-api/src/org/briarproject/api/data/BdfEntry.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package org.briarproject.api.data;
|
||||
|
||||
import java.util.Map.Entry;
|
||||
|
||||
// This class is not thread-safe
|
||||
public class BdfEntry implements Entry<String, Object> {
|
||||
|
||||
private final String key;
|
||||
private Object value;
|
||||
|
||||
public BdfEntry(String key, Object value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object setValue(Object value) {
|
||||
Object oldValue = this.value;
|
||||
this.value = value;
|
||||
return oldValue;
|
||||
}
|
||||
}
|
||||
@@ -3,10 +3,30 @@ package org.briarproject.api.data;
|
||||
import org.briarproject.api.Bytes;
|
||||
import org.briarproject.api.FormatException;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
public class BdfList extends Vector<Object> {
|
||||
|
||||
/**
|
||||
* Factory method for constructing lists inline.
|
||||
* <pre>
|
||||
* BdfList.of(1, 2, 3);
|
||||
* </pre>
|
||||
*/
|
||||
public static BdfList of(Object... items) {
|
||||
return new BdfList(Arrays.asList(items));
|
||||
}
|
||||
|
||||
public BdfList() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BdfList(List<Object> items) {
|
||||
super(items);
|
||||
}
|
||||
|
||||
public Boolean getBoolean(int index) throws FormatException {
|
||||
Object o = get(index);
|
||||
if (o instanceof Boolean) return (Boolean) o;
|
||||
|
||||
Reference in New Issue
Block a user