6ab053a6087402d6750c9b4f794b448ec2305bc2
[jalview.git] / src / jalview / util / MapUtils.java
1 package jalview.util;
2
3 import java.util.Map;
4
5 public class MapUtils
6 {
7   /**
8    * Return the value of the first key that exists in the map and has a non-null
9    * value
10    */
11   public static <K, V> V getFirst(Map<K, V> map, K... keys)
12   {
13     return getFirst(false, map, keys);
14   }
15
16   /**
17    * Return the value of the first key that exists in the map - optionally
18    * limiting to only returning non-null values for first extant key encountered
19    */
20   public static <K, V> V getFirst(boolean nonNull, Map<K, V> map, K... keys)
21   {
22     for (K key : keys)
23     {
24       if (map.containsKey(key))
25       {
26         if (!(nonNull && (map.get(key) == null)))
27         {
28           return map.get(key);
29         }
30         else if (!nonNull)
31         {
32           return map.get(key);
33         }
34       }
35     }
36     return null;
37   }
38
39   /**
40    * peeks in to the map and returns true if one of a bunch of keys is contained
41    * in it
42    * 
43    * @param <K>
44    * @param map
45    * @param keys
46    * @return
47    */
48   public static <K> boolean containsAKey(Map<K, ?> map, K... keys)
49   {
50     for (K key : keys)
51     {
52       if (map.containsKey(key))
53         return true;
54     }
55     return false;
56   }
57 }