Class | Line # | Actions | |||
---|---|---|---|---|---|
MapUtils | 25 | 12 | 8 |
1 | /* | |
2 | * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) | |
3 | * Copyright (C) $$Year-Rel$$ The Jalview Authors | |
4 | * | |
5 | * This file is part of Jalview. | |
6 | * | |
7 | * Jalview is free software: you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License | |
9 | * as published by the Free Software Foundation, either version 3 | |
10 | * of the License, or (at your option) any later version. | |
11 | * | |
12 | * Jalview is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty | |
14 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR | |
15 | * PURPOSE. See the GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with Jalview. If not, see <http://www.gnu.org/licenses/>. | |
19 | * The Jalview Authors are detailed in the 'AUTHORS' file. | |
20 | */ | |
21 | package jalview.util; | |
22 | ||
23 | import java.util.Map; | |
24 | ||
25 | public class MapUtils | |
26 | { | |
27 | /** | |
28 | * Return the value of the first key that exists in the map and has a non-null | |
29 | * value | |
30 | */ | |
31 | 0 | public static <K, V> V getFirst(Map<K, V> map, K... keys) |
32 | { | |
33 | 0 | return getFirst(false, map, keys); |
34 | } | |
35 | ||
36 | /** | |
37 | * Return the value of the first key that exists in the map - optionally | |
38 | * limiting to only returning non-null values for first extant key encountered | |
39 | */ | |
40 | 0 | public static <K, V> V getFirst(boolean nonNull, Map<K, V> map, K... keys) |
41 | { | |
42 | 0 | for (K key : keys) |
43 | { | |
44 | 0 | if (map.containsKey(key)) |
45 | { | |
46 | 0 | if (!(nonNull && (map.get(key) == null))) |
47 | { | |
48 | 0 | return map.get(key); |
49 | } | |
50 | 0 | else if (!nonNull) |
51 | { | |
52 | 0 | return map.get(key); |
53 | } | |
54 | } | |
55 | } | |
56 | 0 | return null; |
57 | } | |
58 | ||
59 | /** | |
60 | * peeks in to the map and returns true if one of a bunch of keys is contained | |
61 | * in it | |
62 | * | |
63 | * @param <K> | |
64 | * @param map | |
65 | * @param keys | |
66 | * @return | |
67 | */ | |
68 | 0 | public static <K> boolean containsAKey(Map<K, ?> map, K... keys) |
69 | { | |
70 | 0 | for (K key : keys) |
71 | { | |
72 | 0 | if (map.containsKey(key)) |
73 | 0 | return true; |
74 | } | |
75 | 0 | return false; |
76 | } | |
77 | } |