From 920a0e0e94e86c1273d1eaf17a41546ee338c229 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Fri, 7 Nov 2014 09:30:25 -0500 Subject: [PATCH] fix how missing values are handled in dictionaries --- core/src/processing/data/FloatDict.java | 13 ++++++++++++- core/src/processing/data/IntDict.java | 12 +++++++++++- core/src/processing/data/StringDict.java | 8 ++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/core/src/processing/data/FloatDict.java b/core/src/processing/data/FloatDict.java index 9647dfd67..eafa0943d 100644 --- a/core/src/processing/data/FloatDict.java +++ b/core/src/processing/data/FloatDict.java @@ -276,7 +276,18 @@ public class FloatDict { */ public float get(String key) { int index = index(key); - if (index == -1) return 0; + if (index == -1) { + throw new IllegalArgumentException("No key named '" + key + "'"); + } + return values[index]; + } + + + public float get(String key, float alternate) { + int index = index(key); + if (index == -1) { + return alternate; + } return values[index]; } diff --git a/core/src/processing/data/IntDict.java b/core/src/processing/data/IntDict.java index 749a2562a..5b3efedbe 100644 --- a/core/src/processing/data/IntDict.java +++ b/core/src/processing/data/IntDict.java @@ -298,10 +298,20 @@ public class IntDict { */ public int get(String key) { int index = index(key); - if (index == -1) return 0; + if (index == -1) { + throw new IllegalArgumentException("No key named '" + key + "'"); + } return values[index]; } + + public int get(String key, int alternate) { + int index = index(key); + if (index == -1) return alternate; + return values[index]; + } + + /** * Create a new key/value pair or change the value of one. * diff --git a/core/src/processing/data/StringDict.java b/core/src/processing/data/StringDict.java index a5eb53c10..76021173c 100644 --- a/core/src/processing/data/StringDict.java +++ b/core/src/processing/data/StringDict.java @@ -246,6 +246,14 @@ public class StringDict { return values[index]; } + + public String get(String key, String alternate) { + int index = index(key); + if (index == -1) return alternate; + return values[index]; + } + + /** * @webref stringdict:method * @brief Create a new key/value pair or change the value of one