aboutsummaryrefslogtreecommitdiff
path: root/android/guava/src/com/google/common/collect/ImmutableMap.java
diff options
context:
space:
mode:
Diffstat (limited to 'android/guava/src/com/google/common/collect/ImmutableMap.java')
-rw-r--r--android/guava/src/com/google/common/collect/ImmutableMap.java72
1 files changed, 72 insertions, 0 deletions
diff --git a/android/guava/src/com/google/common/collect/ImmutableMap.java b/android/guava/src/com/google/common/collect/ImmutableMap.java
index 94484a0d5..79b39b7ad 100644
--- a/android/guava/src/com/google/common/collect/ImmutableMap.java
+++ b/android/guava/src/com/google/common/collect/ImmutableMap.java
@@ -23,6 +23,7 @@ import static com.google.common.collect.CollectPreconditions.checkNonnegative;
import static java.util.Objects.requireNonNull;
import com.google.common.annotations.GwtCompatible;
+import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.DoNotCall;
@@ -45,6 +46,10 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
+import java.util.function.BinaryOperator;
+import java.util.function.Function;
+import java.util.stream.Collector;
+import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -66,6 +71,44 @@ import org.checkerframework.checker.nullness.qual.Nullable;
public abstract class ImmutableMap<K, V> implements Map<K, V>, Serializable {
/**
+ * Returns a {@link Collector} that accumulates elements into an {@code ImmutableMap} whose keys
+ * and values are the result of applying the provided mapping functions to the input elements.
+ * Entries appear in the result {@code ImmutableMap} in encounter order.
+ *
+ * <p>If the mapped keys contain duplicates (according to {@link Object#equals(Object)}, an {@code
+ * IllegalArgumentException} is thrown when the collection operation is performed. (This differs
+ * from the {@code Collector} returned by {@link Collectors#toMap(Function, Function)}, which
+ * throws an {@code IllegalStateException}.)
+ */
+ @SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
+ @IgnoreJRERequirement // Users will use this only if they're already using streams.
+ static <T extends @Nullable Object, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
+ Function<? super T, ? extends K> keyFunction,
+ Function<? super T, ? extends V> valueFunction) {
+ return CollectCollectors.toImmutableMap(keyFunction, valueFunction);
+ }
+
+ /**
+ * Returns a {@link Collector} that accumulates elements into an {@code ImmutableMap} whose keys
+ * and values are the result of applying the provided mapping functions to the input elements.
+ *
+ * <p>If the mapped keys contain duplicates (according to {@link Object#equals(Object)}), the
+ * values are merged using the specified merging function. If the merging function returns {@code
+ * null}, then the collector removes the value that has been computed for the key thus far (though
+ * future occurrences of the key would reinsert it).
+ *
+ * <p>Entries will appear in the encounter order of the first occurrence of the key.
+ */
+ @SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
+ @IgnoreJRERequirement // Users will use this only if they're already using streams.
+ static <T extends @Nullable Object, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
+ Function<? super T, ? extends K> keyFunction,
+ Function<? super T, ? extends V> valueFunction,
+ BinaryOperator<V> mergeFunction) {
+ return CollectCollectors.toImmutableMap(keyFunction, valueFunction, mergeFunction);
+ }
+
+ /**
* Returns the empty map. This map behaves and performs comparably to {@link
* Collections#emptyMap}, and is preferable mainly for consistency and maintainability of your
* code.
@@ -727,6 +770,15 @@ public abstract class ImmutableMap<K, V> implements Map<K, V>, Serializable {
public UnmodifiableIterator<Entry<K, V>> iterator() {
return entryIterator();
}
+
+ // redeclare to help optimizers with b/310253115
+ @SuppressWarnings("RedundantOverride")
+ @Override
+ @J2ktIncompatible // serialization
+ @GwtIncompatible // serialization
+ Object writeReplace() {
+ return super.writeReplace();
+ }
}
return new EntrySetImpl();
}
@@ -735,6 +787,15 @@ public abstract class ImmutableMap<K, V> implements Map<K, V>, Serializable {
ImmutableCollection<V> createValues() {
return new ImmutableMapValues<>(this);
}
+
+ // redeclare to help optimizers with b/310253115
+ @SuppressWarnings("RedundantOverride")
+ @Override
+ @J2ktIncompatible // serialization
+ @GwtIncompatible // serialization
+ Object writeReplace() {
+ return super.writeReplace();
+ }
}
ImmutableMap() {}
@@ -1016,6 +1077,15 @@ public abstract class ImmutableMap<K, V> implements Map<K, V>, Serializable {
}
};
}
+
+ // redeclare to help optimizers with b/310253115
+ @SuppressWarnings("RedundantOverride")
+ @Override
+ @J2ktIncompatible // serialization
+ @GwtIncompatible // serialization
+ Object writeReplace() {
+ return super.writeReplace();
+ }
}
@Override
@@ -1133,4 +1203,6 @@ public abstract class ImmutableMap<K, V> implements Map<K, V>, Serializable {
private void readObject(ObjectInputStream stream) throws InvalidObjectException {
throw new InvalidObjectException("Use SerializedForm");
}
+
+ private static final long serialVersionUID = 0xdecaf;
}