Bug 1382332: Add MapUtils.putIfAbsent. r=liuche draft
authorMichael Comella <michael.l.comella@gmail.com>
Thu, 10 Aug 2017 17:11:02 -0700
changeset 646232 83de391ae827409f8798d2fd00749b4137ba0a88
parent 646217 02e1f1224854df4006f275a6877eb5cd6ecf8a78
child 646233 be57f0b3618d33ec34e23e3b5c7e343aca65425e
push id74034
push usermichael.l.comella@gmail.com
push dateTue, 15 Aug 2017 00:29:50 +0000
reviewersliuche
bugs1382332
milestone57.0a1
Bug 1382332: Add MapUtils.putIfAbsent. r=liuche MozReview-Commit-ID: 1Q78aEmsiYM
mobile/android/base/moz.build
mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/MapUtils.java
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -281,16 +281,17 @@ gujar.sources += [geckoview_source_dir +
     'util/HardwareCodecCapabilityUtils.java',
     'util/HardwareUtils.java',
     'util/INIParser.java',
     'util/INISection.java',
     'util/InputOptionsUtils.java',
     'util/IntentUtils.java',
     'util/IOUtils.java',
     'util/JSONUtils.java',
+    'util/MapUtils.java',
     'util/MenuUtils.java',
     'util/NetworkUtils.java',
     'util/NonEvictingLruCache.java',
     'util/PrefUtils.java',
     'util/ProxySelector.java',
     'util/publicsuffix/PublicSuffix.java',
     'util/publicsuffix/PublicSuffixPatterns.java',
     'util/RawResource.java',
new file mode 100644
--- /dev/null
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/MapUtils.java
@@ -0,0 +1,26 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.util;
+
+import android.support.annotation.Nullable;
+
+import java.util.Map;
+
+/** Utils for {@link java.util.Map} and friends. */
+public class MapUtils {
+    private MapUtils() {}
+
+    // impl via https://developer.android.com/reference/java/util/Map.html#putIfAbsent(K,%20V)
+    // note: this method is available in API 24.
+    @Nullable
+    public static <K, V> V putIfAbsent(final Map<K, V> map, final K key, final V value) {
+        V v = map.get(key);
+        if (v == null) {
+            v = map.put(key, value);
+        }
+
+        return v;
+    }
+}