/** * Returns an immutable bimap containing the same entries as {@code map}. If {@code map} somehow * contains entries with duplicate keys (for example, if it is a {@code SortedMap} whose * comparator is not <i>consistent with equals</i>), the results of this method are undefined. * * <p>The returned {@code BiMap} iterates over entries in the same order as the {@code entrySet} * of the original map. * * <p>Despite the method name, this method attempts to avoid actually copying the data when it is * safe to do so. The exact circumstances under which a copy will or will not be performed are * undocumented and subject to change. * * @throws IllegalArgumentException if two keys have the same value or two values have the same * key * @throws NullPointerException if any key or value in {@code map} is null */ public static <K, V> ImmutableBiMap<K, V> copyOf(Map<? extends K, ? extends V> map) { if (map instanceof ImmutableBiMap) { @SuppressWarnings("unchecked") // safe since map is not writable ImmutableBiMap<K, V> bimap = (ImmutableBiMap<K, V>) map; // TODO(lowasser): if we need to make a copy of a BiMap because the // forward map is a view, don't make a copy of the non-view delegate map if (!bimap.isPartialView()) { return bimap; } } return copyOf(map.entrySet()); }
/** * Returns an immutable bimap containing the same entries as {@code map}. If {@code map} somehow * contains entries with duplicate keys (for example, if it is a {@code SortedMap} whose * comparator is not <i>consistent with equals</i>), the results of this method are undefined. * * <p>The returned {@code BiMap} iterates over entries in the same order as the {@code entrySet} * of the original map. * * <p>Despite the method name, this method attempts to avoid actually copying the data when it is * safe to do so. The exact circumstances under which a copy will or will not be performed are * undocumented and subject to change. * * @throws IllegalArgumentException if two keys have the same value or two values have the same * key * @throws NullPointerException if any key or value in {@code map} is null */ public static <K, V> ImmutableBiMap<K, V> copyOf(Map<? extends K, ? extends V> map) { if (map instanceof ImmutableBiMap) { @SuppressWarnings("unchecked") // safe since map is not writable ImmutableBiMap<K, V> bimap = (ImmutableBiMap<K, V>) map; // TODO(lowasser): if we need to make a copy of a BiMap because the // forward map is a view, don't make a copy of the non-view delegate map if (!bimap.isPartialView()) { return bimap; } } return copyOf(map.entrySet()); }
/** * A builder for creating immutable bimap instances, especially {@code public static final} bimaps * ("constant bimaps"). Example: * * <pre>{@code * static final ImmutableBiMap<String, Integer> WORD_TO_INT = * new ImmutableBiMap.Builder<String, Integer>() * .put("one", 1) * .put("two", 2) * .put("three", 3) * .build(); * }</pre> * * <p>For <i>small</i> immutable bimaps, the {@code ImmutableBiMap.of()} methods are even more * convenient. * * <p>By default, a {@code Builder} will generate bimaps that iterate over entries in the order * they were inserted into the builder. For example, in the above example, {@code * WORD_TO_INT.entrySet()} is guaranteed to iterate over the entries in the order {@code "one"=1, * "two"=2, "three"=3}, and {@code keySet()} and {@code values()} respect the same order. If you * want a different order, consider using {@link #orderEntriesByValue(Comparator)}, which changes * this builder to sort entries by value. * * <p>Builder instances can be reused - it is safe to call {@link #build} multiple times to build * multiple bimaps in series. Each bimap is a superset of the bimaps created before it. * * @since 2.0 */ public static final class Builder<K, V> extends ImmutableMap.Builder<K, V> { /**
/** * Returns an immutable bimap containing the same entries as {@code map}. If * {@code map} somehow contains entries with duplicate keys (for example, if * it is a {@code SortedMap} whose comparator is not <i>consistent with * equals</i>), the results of this method are undefined. * * <p>Despite the method name, this method attempts to avoid actually copying * the data when it is safe to do so. The exact circumstances under which a * copy will or will not be performed are undocumented and subject to change. * * @throws IllegalArgumentException if two keys have the same value * @throws NullPointerException if any key or value in {@code map} is null */ public static <K, V> ImmutableBiMap<K, V> copyOf(Map<? extends K, ? extends V> map) { if (map instanceof ImmutableBiMap) { @SuppressWarnings("unchecked") // safe since map is not writable ImmutableBiMap<K, V> bimap = (ImmutableBiMap<K, V>) map; // TODO(lowasser): if we need to make a copy of a BiMap because the // forward map is a view, don't make a copy of the non-view delegate map if (!bimap.isPartialView()) { return bimap; } } return copyOf(map.entrySet()); }
/** * Returns an immutable bimap containing the same entries as {@code map}. If * {@code map} somehow contains entries with duplicate keys (for example, if * it is a {@code SortedMap} whose comparator is not <i>consistent with * equals</i>), the results of this method are undefined. * * <p>Despite the method name, this method attempts to avoid actually copying * the data when it is safe to do so. The exact circumstances under which a * copy will or will not be performed are undocumented and subject to change. * * @throws IllegalArgumentException if two keys have the same value * @throws NullPointerException if any key or value in {@code map} is null */ public static <K, V> ImmutableBiMap<K, V> copyOf( Map<? extends K, ? extends V> map) { if (map instanceof ImmutableBiMap) { @SuppressWarnings("unchecked") // safe since map is not writable ImmutableBiMap<K, V> bimap = (ImmutableBiMap<K, V>) map; // TODO(user): if we need to make a copy of a BiMap because the // forward map is a view, don't make a copy of the non-view delegate map if (!bimap.isPartialView()) { return bimap; } } return fromEntries(ImmutableList.copyOf(map.entrySet())); }
/** * Returns an immutable bimap containing the same entries as {@code map}. If {@code map} somehow * contains entries with duplicate keys (for example, if it is a {@code SortedMap} whose * comparator is not <i>consistent with equals</i>), the results of this method are undefined. * * <p>The returned {@code BiMap} iterates over entries in the same order as the {@code entrySet} * of the original map. * * <p>Despite the method name, this method attempts to avoid actually copying the data when it is * safe to do so. The exact circumstances under which a copy will or will not be performed are * undocumented and subject to change. * * @throws IllegalArgumentException if two keys have the same value or two values have the same * key * @throws NullPointerException if any key or value in {@code map} is null */ public static <K, V> ImmutableBiMap<K, V> copyOf(Map<? extends K, ? extends V> map) { if (map instanceof ImmutableBiMap) { @SuppressWarnings("unchecked") // safe since map is not writable ImmutableBiMap<K, V> bimap = (ImmutableBiMap<K, V>) map; // TODO(lowasser): if we need to make a copy of a BiMap because the // forward map is a view, don't make a copy of the non-view delegate map if (!bimap.isPartialView()) { return bimap; } } return copyOf(map.entrySet()); }
/** * Returns an immutable bimap containing the same entries as {@code map}. If {@code map} somehow * contains entries with duplicate keys (for example, if it is a {@code SortedMap} whose * comparator is not <i>consistent with equals</i>), the results of this method are undefined. * * <p>The returned {@code BiMap} iterates over entries in the same order as the {@code entrySet} * of the original map. * * <p>Despite the method name, this method attempts to avoid actually copying the data when it is * safe to do so. The exact circumstances under which a copy will or will not be performed are * undocumented and subject to change. * * @throws IllegalArgumentException if two keys have the same value or two values have the same * key * @throws NullPointerException if any key or value in {@code map} is null */ public static <K, V> ImmutableBiMap<K, V> copyOf(Map<? extends K, ? extends V> map) { if (map instanceof ImmutableBiMap) { @SuppressWarnings("unchecked") // safe since map is not writable ImmutableBiMap<K, V> bimap = (ImmutableBiMap<K, V>) map; // TODO(lowasser): if we need to make a copy of a BiMap because the // forward map is a view, don't make a copy of the non-view delegate map if (!bimap.isPartialView()) { return bimap; } } return copyOf(map.entrySet()); }
/** * Returns an immutable bimap containing the same entries as {@code map}. If * {@code map} somehow contains entries with duplicate keys (for example, if * it is a {@code SortedMap} whose comparator is not <i>consistent with * equals</i>), the results of this method are undefined. * * <p>Despite the method name, this method attempts to avoid actually copying * the data when it is safe to do so. The exact circumstances under which a * copy will or will not be performed are undocumented and subject to change. * * @throws IllegalArgumentException if two keys have the same value * @throws NullPointerException if any key or value in {@code map} is null */ public static <K, V> ImmutableBiMap<K, V> copyOf( Map<? extends K, ? extends V> map) { if (map instanceof ImmutableBiMap) { @SuppressWarnings("unchecked") // safe since map is not writable ImmutableBiMap<K, V> bimap = (ImmutableBiMap<K, V>) map; // TODO(user): if we need to make a copy of a BiMap because the // forward map is a view, don't make a copy of the non-view delegate map if (!bimap.isPartialView()) { return bimap; } } if (map.isEmpty()) { return of(); } ImmutableMap<K, V> immutableMap = ImmutableMap.copyOf(map); return new RegularImmutableBiMap<K, V>(immutableMap); }
/** * Returns an immutable bimap containing the same entries as {@code map}. If * {@code map} somehow contains entries with duplicate keys (for example, if * it is a {@code SortedMap} whose comparator is not <i>consistent with * equals</i>), the results of this method are undefined. * * <p>Despite the method name, this method attempts to avoid actually copying * the data when it is safe to do so. The exact circumstances under which a * copy will or will not be performed are undocumented and subject to change. * * @throws IllegalArgumentException if two keys have the same value * @throws NullPointerException if any key or value in {@code map} is null */ public static <K, V> ImmutableBiMap<K, V> copyOf( Map<? extends K, ? extends V> map) { if (map instanceof ImmutableBiMap) { @SuppressWarnings("unchecked") // safe since map is not writable ImmutableBiMap<K, V> bimap = (ImmutableBiMap<K, V>) map; // TODO(user): if we need to make a copy of a BiMap because the // forward map is a view, don't make a copy of the non-view delegate map if (!bimap.isPartialView()) { return bimap; } } if (map.isEmpty()) { return of(); } ImmutableMap<K, V> immutableMap = ImmutableMap.copyOf(map); return new RegularImmutableBiMap<K, V>(immutableMap); }
if (!bimap.isPartialView()) { return bimap;
if (!bimap.isPartialView()) { return bimap;
if (!bimap.isPartialView()) { return bimap;
if (!bimap.isPartialView()) { return bimap;
if (!bimap.isPartialView()) { return bimap;
if (!bimap.isPartialView()) { return bimap;