/* * 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 java.util.Collections; import java.util.Map; /** * GWT emulation of {@link ImmutableBiMap}. * * @author Hayward Chan */ public abstract class ImmutableBiMap extends ImmutableMap implements BiMap { private static final ImmutableBiMap EMPTY_IMMUTABLE_BIMAP = new EmptyBiMap(); // Casting to any type is safe because the set will never hold any elements. @SuppressWarnings("unchecked") public static ImmutableBiMap of() { return (ImmutableBiMap) EMPTY_IMMUTABLE_BIMAP; } public static ImmutableBiMap of(K k1, V v1) { return new RegularImmutableBiMap(ImmutableMap.of(k1, v1)); } public static ImmutableBiMap of(K k1, V v1, K k2, V v2) { return new RegularImmutableBiMap(ImmutableMap.of(k1, v1, k2, v2)); } public static ImmutableBiMap of( K k1, V v1, K k2, V v2, K k3, V v3) { return new RegularImmutableBiMap(ImmutableMap.of( k1, v1, k2, v2, k3, v3)); } public static ImmutableBiMap of( K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) { return new RegularImmutableBiMap(ImmutableMap.of( k1, v1, k2, v2, k3, v3, k4, v4)); } public static 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(ImmutableMap.of( k1, v1, k2, v2, k3, v3, k4, v4, k5, v5)); } public static Builder builder() { return new Builder(); } public static final class Builder extends ImmutableMap.Builder { public Builder() {} @Override public Builder put(K key, V value) { super.put(key, value); return this; } @Override public Builder putAll(Map map) { super.putAll(map); return this; } @Override public ImmutableBiMap build() { ImmutableMap map = super.build(); if (map.isEmpty()) { return of(); } return new RegularImmutableBiMap(super.build()); } } public static ImmutableBiMap copyOf( Map map) { if (map instanceof ImmutableBiMap) { @SuppressWarnings("unchecked") // safe since map is not writable ImmutableBiMap bimap = (ImmutableBiMap) map; return bimap; } if (map.isEmpty()) { return of(); } ImmutableMap immutableMap = ImmutableMap.copyOf(map); return new RegularImmutableBiMap(immutableMap); } ImmutableBiMap(Map delegate) { super(delegate); } public abstract ImmutableBiMap inverse(); @Override public ImmutableSet values() { return inverse().keySet(); } public final V forcePut(K key, V value) { throw new UnsupportedOperationException(); } @SuppressWarnings("serial") static class EmptyBiMap extends ImmutableBiMap { EmptyBiMap() { super(Collections.emptyMap()); } @Override public ImmutableBiMap inverse() { return this; } } }