Unverified Commit 418798b1 authored by akwizgran's avatar akwizgran
Browse files

Added BdfList/Dictionary getters for optional values.

parent 08099714
......@@ -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;
......
......@@ -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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment