You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2018/04/23 19:29:14 UTC

svn commit: r1829922 - /pivot/trunk/core/src/org/apache/pivot/collections/Dictionary.java

Author: rwhitcomb
Date: Mon Apr 23 19:29:14 2018
New Revision: 1829922

URL: http://svn.apache.org/viewvc?rev=1829922&view=rev
Log:
PIVOT-999:  Add two new default methods to Dictionary:
* containsAny(K...) that will check if any of the given keys are in the dictionary.
* getFirst(K...) that will retrieve the first value for which "containsKey" is true.

This methods are defaults in the interface, so they will work for any subclass of
Dictionary (such as Map).

Modified:
    pivot/trunk/core/src/org/apache/pivot/collections/Dictionary.java

Modified: pivot/trunk/core/src/org/apache/pivot/collections/Dictionary.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/collections/Dictionary.java?rev=1829922&r1=1829921&r2=1829922&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/collections/Dictionary.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/collections/Dictionary.java Mon Apr 23 19:29:14 2018
@@ -104,6 +104,43 @@ public interface Dictionary<K, V> {
     boolean containsKey(K key);
 
     /**
+     * Determines if any of the given keys exists in the dictionary.
+     *
+     * @param keys The list of keys to search for in the dictionary.
+     * @return {@code true} if {@link #containsKey} returns true for any
+     * of the given keys, or {@code false} if none of the keys exist.
+     */
+    @SuppressWarnings("unchecked")
+    default boolean containsAny(K... keys) {
+        for (K key : keys) {
+            if (containsKey(key)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Retrieves the value of the first of the given keys that exists in the
+     * dictionary (that is, the first key for which {@link #containsKey} returns
+     * true).
+     *
+     * @param keys The list of keys to search for in the dictionary.
+     * @return The first value found, or {@code null} if either the value is
+     * null for the first key found, or none of the keys exists in the dictionary.
+     * Use {@link #containsAny} to determine the difference.
+     */
+    @SuppressWarnings("unchecked")
+    default V getFirst(K... keys) {
+        for (K key : keys) {
+            if (containsKey(key)) {
+                return get(key);
+            }
+        }
+        return null;
+    }
+
+    /**
      * Retrieve a String value from this dictionary; returning null if the key
      * does not exist.
      *