diff --git a/briar-api/src/org/briarproject/api/data/BdfDictionary.java b/briar-api/src/org/briarproject/api/data/BdfDictionary.java index 5fa726fa64b6edf58c248bf7214c458d1400762b..920387cae1fe58775e57367fa4db6e471948b262 100644 --- a/briar-api/src/org/briarproject/api/data/BdfDictionary.java +++ b/briar-api/src/org/briarproject/api/data/BdfDictionary.java @@ -40,6 +40,13 @@ public class BdfDictionary extends Hashtable<String, Object> { throw new FormatException(); } + public Boolean getOptionalBoolean(String key) throws FormatException { + Object o = get(key); + if (o == null || o == NULL_VALUE) return null; + if (o instanceof Boolean) return (Boolean) o; + throw new FormatException(); + } + public Boolean getBoolean(String key, Boolean defaultValue) { Object o = get(key); if (o instanceof Boolean) return (Boolean) o; @@ -55,6 +62,16 @@ public class BdfDictionary extends Hashtable<String, Object> { throw new FormatException(); } + public Long getOptionalLong(String key) throws FormatException { + Object o = get(key); + if (o == null || o == NULL_VALUE) return null; + if (o instanceof Long) return (Long) o; + if (o instanceof Integer) return ((Integer) o).longValue(); + if (o instanceof Short) return ((Short) o).longValue(); + if (o instanceof Byte) return ((Byte) o).longValue(); + throw new FormatException(); + } + public Long getLong(String key, Long defaultValue) { Object o = get(key); if (o instanceof Long) return (Long) o; @@ -71,6 +88,14 @@ public class BdfDictionary extends Hashtable<String, Object> { throw new FormatException(); } + public Double getOptionalDouble(String key) throws FormatException { + Object o = get(key); + if (o == null || o == NULL_VALUE) return null; + if (o instanceof Double) return (Double) o; + if (o instanceof Float) return ((Float) o).doubleValue(); + throw new FormatException(); + } + public Double getDouble(String key, Double defaultValue) { Object o = get(key); if (o instanceof Double) return (Double) o; @@ -84,6 +109,13 @@ public class BdfDictionary extends Hashtable<String, Object> { throw new FormatException(); } + public String getOptionalString(String key) throws FormatException { + Object o = get(key); + if (o == null || o == NULL_VALUE) return null; + if (o instanceof String) return (String) o; + throw new FormatException(); + } + public String getString(String key, String defaultValue) { Object o = get(key); if (o instanceof String) return (String) o; @@ -97,6 +129,14 @@ public class BdfDictionary extends Hashtable<String, Object> { throw new FormatException(); } + public byte[] getOptionalRaw(String key) throws FormatException { + Object o = get(key); + if (o == null || o == NULL_VALUE) return null; + if (o instanceof byte[]) return (byte[]) o; + if (o instanceof Bytes) return ((Bytes) o).getBytes(); + throw new FormatException(); + } + public byte[] getRaw(String key, byte[] defaultValue) { Object o = get(key); if (o instanceof byte[]) return (byte[]) o; @@ -110,6 +150,13 @@ public class BdfDictionary extends Hashtable<String, Object> { throw new FormatException(); } + public BdfList getOptionalList(String key) throws FormatException { + Object o = get(key); + if (o == null || o == NULL_VALUE) return null; + if (o instanceof BdfList) return (BdfList) o; + throw new FormatException(); + } + public BdfList getList(String key, BdfList defaultValue) { Object o = get(key); if (o instanceof BdfList) return (BdfList) o; @@ -122,6 +169,14 @@ public class BdfDictionary extends Hashtable<String, Object> { throw new FormatException(); } + public BdfDictionary getOptionalDictionary(String key) + throws FormatException { + Object o = get(key); + if (o == null || o == NULL_VALUE) return null; + if (o instanceof BdfDictionary) return (BdfDictionary) o; + throw new FormatException(); + } + public BdfDictionary getDictionary(String key, BdfDictionary defaultValue) { Object o = get(key); if (o instanceof BdfDictionary) return (BdfDictionary) o; diff --git a/briar-api/src/org/briarproject/api/data/BdfList.java b/briar-api/src/org/briarproject/api/data/BdfList.java index 0cbd0b25292a2b24ff4987066da551524ace2d45..20a68c9477c40888073d0b893a16dea6e598f90e 100644 --- a/briar-api/src/org/briarproject/api/data/BdfList.java +++ b/briar-api/src/org/briarproject/api/data/BdfList.java @@ -7,6 +7,8 @@ import java.util.Arrays; import java.util.List; import java.util.Vector; +import static org.briarproject.api.data.BdfDictionary.NULL_VALUE; + public class BdfList extends Vector<Object> { /** @@ -33,6 +35,13 @@ public class BdfList extends Vector<Object> { throw new FormatException(); } + public Boolean getOptionalBoolean(int index) throws FormatException { + Object o = get(index); + if (o == null || o == NULL_VALUE) return null; + if (o instanceof Boolean) return (Boolean) o; + throw new FormatException(); + } + public Boolean getBoolean(int index, Boolean defaultValue) { Object o = get(index); if (o instanceof Boolean) return (Boolean) o; @@ -48,6 +57,16 @@ public class BdfList extends Vector<Object> { throw new FormatException(); } + public Long getOptionalLong(int index) throws FormatException { + Object o = get(index); + if (o == null || o == NULL_VALUE) return null; + if (o instanceof Long) return (Long) o; + if (o instanceof Integer) return ((Integer) o).longValue(); + if (o instanceof Short) return ((Short) o).longValue(); + if (o instanceof Byte) return ((Byte) o).longValue(); + throw new FormatException(); + } + public Long getLong(int index, Long defaultValue) { Object o = get(index); if (o instanceof Long) return (Long) o; @@ -64,6 +83,14 @@ public class BdfList extends Vector<Object> { throw new FormatException(); } + public Double getOptionalDouble(int index) throws FormatException { + Object o = get(index); + if (o == null || o == NULL_VALUE) return null; + if (o instanceof Double) return (Double) o; + if (o instanceof Float) return ((Float) o).doubleValue(); + throw new FormatException(); + } + public Double getDouble(int index, Double defaultValue) { Object o = get(index); if (o instanceof Double) return (Double) o; @@ -77,6 +104,13 @@ public class BdfList extends Vector<Object> { throw new FormatException(); } + public String getOptionalString(int index) throws FormatException { + Object o = get(index); + if (o == null || o == NULL_VALUE) return null; + if (o instanceof String) return (String) o; + throw new FormatException(); + } + public String getString(int index, String defaultValue) { Object o = get(index); if (o instanceof String) return (String) o; @@ -90,6 +124,14 @@ public class BdfList extends Vector<Object> { throw new FormatException(); } + public byte[] getOptionalRaw(int index) throws FormatException { + Object o = get(index); + if (o == null || o == NULL_VALUE) return null; + if (o instanceof byte[]) return (byte[]) o; + if (o instanceof Bytes) return ((Bytes) o).getBytes(); + throw new FormatException(); + } + public byte[] getRaw(int index, byte[] defaultValue) { Object o = get(index); if (o instanceof byte[]) return (byte[]) o; @@ -103,6 +145,13 @@ public class BdfList extends Vector<Object> { throw new FormatException(); } + public BdfList getOptionalList(int index) throws FormatException { + Object o = get(index); + if (o == null || o == NULL_VALUE) return null; + if (o instanceof BdfList) return (BdfList) o; + throw new FormatException(); + } + public BdfList getList(int index, BdfList defaultValue) { Object o = get(index); if (o instanceof BdfList) return (BdfList) o; @@ -115,6 +164,14 @@ public class BdfList extends Vector<Object> { throw new FormatException(); } + public BdfDictionary getOptionalDictionary(int index) + throws FormatException { + Object o = get(index); + if (o == null || o == NULL_VALUE) return null; + if (o instanceof BdfDictionary) return (BdfDictionary) o; + throw new FormatException(); + } + public BdfDictionary getDictionary(int index, BdfDictionary defaultValue) { Object o = get(index); if (o instanceof BdfDictionary) return (BdfDictionary) o;