Ejemplo n.º 1
0
 /**
  * Returns a newly-created immutable bimap.
  *
  * @throws IllegalArgumentException if duplicate keys or values were added
  */
 @Override
 public ImmutableBiMap<K, V> build() {
   switch (size) {
     case 0:
       return of();
     case 1:
       return of(entries[0].getKey(), entries[0].getValue());
     default:
       /*
        * If entries is full, then this implementation may end up using the entries array
        * directly and writing over the entry objects with non-terminal entries, but this is
        * safe; if this Builder is used further, it will grow the entries array (so it can't
        * affect the original array), and future build() calls will always copy any entry
        * objects that cannot be safely reused.
        */
       if (valueComparator != null) {
         if (entriesUsed) {
           entries = Arrays.copyOf(entries, size);
         }
         Arrays.sort(
             entries,
             0,
             size,
             Ordering.from(valueComparator).onResultOf(Maps.<V>valueFunction()));
       }
       entriesUsed = size == entries.length;
       return RegularImmutableBiMap.fromEntryArray(size, entries);
   }
 }
Ejemplo n.º 2
0
 /**
  * Returns an immutable bimap containing the given entries.
  *
  * @throws IllegalArgumentException if two keys have the same value or two values have the same
  *     key
  * @throws NullPointerException if any key, value, or entry is null
  * @since 19.0
  */
 @Beta
 public static <K, V> ImmutableBiMap<K, V> copyOf(
     Iterable<? extends Entry<? extends K, ? extends V>> entries) {
   @SuppressWarnings("unchecked") // we'll only be using getKey and getValue, which are covariant
   Entry<K, V>[] entryArray = (Entry<K, V>[]) Iterables.toArray(entries, EMPTY_ENTRY_ARRAY);
   switch (entryArray.length) {
     case 0:
       return of();
     case 1:
       Entry<K, V> entry = entryArray[0];
       return of(entry.getKey(), entry.getValue());
     default:
       /*
        * The current implementation will end up using entryArray directly, though it will write
        * over the (arbitrary, potentially mutable) Entry objects actually stored in entryArray.
        */
       return RegularImmutableBiMap.fromEntries(entryArray);
   }
 }
Ejemplo n.º 3
0
 /**
  * Returns an immutable map containing the given entries, in order.
  *
  * @throws IllegalArgumentException if duplicate keys or values are added
  */
 public static <K, V> ImmutableBiMap<K, V> of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
   return RegularImmutableBiMap.fromEntries(
       entryOf(k1, v1), entryOf(k2, v2), entryOf(k3, v3), entryOf(k4, v4));
 }
Ejemplo n.º 4
0
 /**
  * Returns an immutable map containing the given entries, in order.
  *
  * @throws IllegalArgumentException if duplicate keys or values are added
  */
 public static <K, V> ImmutableBiMap<K, V> of(K k1, V v1, K k2, V v2) {
   return RegularImmutableBiMap.fromEntries(entryOf(k1, v1), entryOf(k2, v2));
 }