alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (ImmutableBiMap.java)

This example Java source code file (ImmutableBiMap.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

builder, entry, immutablebimap, immutablemap, iterable, override, regularimmutablebimap, singletonimmutablebimap, suppresswarnings, unsupportedoperationexception, util

The ImmutableBiMap.java Java example source code

/*
 * Copyright (C) 2009 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect;

import static com.google.common.collect.CollectPreconditions.checkEntryNotNull;

import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * GWT emulation of {@link ImmutableBiMap}.
 *
 * @author Hayward Chan
 */
public abstract class ImmutableBiMap<K, V> extends ForwardingImmutableMap
    implements BiMap<K, V> {

  // Casting to any type is safe because the set will never hold any elements.
  @SuppressWarnings("unchecked")
  public static <K, V> ImmutableBiMap of() {
    return (ImmutableBiMap<K, V>) RegularImmutableBiMap.EMPTY;
  }

  public static <K, V> ImmutableBiMap of(K k1, V v1) {
    checkEntryNotNull(k1, v1);
    return new SingletonImmutableBiMap<K, V>(k1, v1);
  }

  public static <K, V> ImmutableBiMap of(K k1, V v1, K k2, V v2) {
    return new RegularImmutableBiMap<K, V>(ImmutableMap.of(k1, v1, k2, v2));
  }

  public static <K, V> ImmutableBiMap of(
      K k1, V v1, K k2, V v2, K k3, V v3) {
    return new RegularImmutableBiMap<K, V>(ImmutableMap.of(
        k1, v1, k2, v2, k3, v3));
  }

  public static <K, V> ImmutableBiMap of(
      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
    return new RegularImmutableBiMap<K, V>(ImmutableMap.of(
        k1, v1, k2, v2, k3, v3, k4, v4));
  }

  public static <K, V> ImmutableBiMap of(
      K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
    return new RegularImmutableBiMap<K, V>(ImmutableMap.of(
        k1, v1, k2, v2, k3, v3, k4, v4, k5, v5));
  }

  public static <K, V> Builder builder() {
    return new Builder<K, V>();
  }

  public static final class Builder<K, V> extends ImmutableMap.Builder {

    public Builder() {}

    Builder(int initCapacity) {
      super(initCapacity);
    }

    @Override public Builder<K, V> put(K key, V value) {
      super.put(key, value);
      return this;
    }

    @Override public Builder<K, V> put(Map.Entry entry) {
      super.put(entry);
      return this;
    }

    @Override public Builder<K, V> putAll(Map map) {
      super.putAll(map);
      return this;
    }

    @Override public Builder<K, V> putAll(
        Iterable<? extends Entry entries) {
      super.putAll(entries);
      return this;
    }

    public Builder<K, V> orderEntriesByValue(Comparator valueComparator) {
      super.orderEntriesByValue(valueComparator);
      return this;
    }

    @Override public ImmutableBiMap<K, V> build() {
      ImmutableMap<K, V> map = super.build();
      if (map.isEmpty()) {
        return of();
      }
      return new RegularImmutableBiMap<K, V>(super.build());
    }
  }

  public static <K, V> ImmutableBiMap copyOf(
      Map<? extends K, ? extends V> map) {
    if (map instanceof ImmutableBiMap) {
      @SuppressWarnings("unchecked") // safe since map is not writable
      ImmutableBiMap<K, V> bimap = (ImmutableBiMap) map;
      return bimap;
    }

    if (map.isEmpty()) {
      return of();
    }

    ImmutableMap<K, V> immutableMap = ImmutableMap.copyOf(map);
    return new RegularImmutableBiMap<K, V>(immutableMap);
  }

  public static <K, V> ImmutableBiMap copyOf(
      Iterable<? extends Entry entries) {
    return new Builder<K, V>().putAll(entries).build();
  }

  ImmutableBiMap(Map<K, V> delegate) {
    super(delegate);
  }

  public abstract ImmutableBiMap<V, K> inverse();

  @Override public ImmutableSet<V> values() {
    return inverse().keySet();
  }

  public final V forcePut(K key, V value) {
    throw new UnsupportedOperationException();
  }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java ImmutableBiMap.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.