From 418798b1f04b2b6fbe14c77f462c263d1d9e0bd6 Mon Sep 17 00:00:00 2001
From: akwizgran <akwizgran@users.sourceforge.net>
Date: Mon, 29 Feb 2016 22:11:34 +0000
Subject: [PATCH] Added BdfList/Dictionary getters for optional values.

---
 .../briarproject/api/data/BdfDictionary.java  | 55 ++++++++++++++++++
 .../org/briarproject/api/data/BdfList.java    | 57 +++++++++++++++++++
 2 files changed, 112 insertions(+)

diff --git a/briar-api/src/org/briarproject/api/data/BdfDictionary.java b/briar-api/src/org/briarproject/api/data/BdfDictionary.java
index 5fa726fa64..920387cae1 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 0cbd0b2529..20a68c9477 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;
-- 
GitLab