/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.openjpa.sdo;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Map for reverse lookup.
 * 
 * @author Pinaki Poddar
 *
 */
class BiDiMap<K,V> implements Map<K,V> {

	private HashMap<K,V> forward = new HashMap<K,V>();
	private HashMap<V,K> reverse = new HashMap<V,K>();
	
	public void clear() {
		forward.clear();
		reverse.clear();
	}

	public boolean containsKey(Object key) {
		return forward.containsKey(key);
	}

	public boolean containsValue(Object value) {
		return forward.containsValue(value);
	}

	public Set<Entry<K,V>> entrySet() {
		return forward.entrySet();
	}

	public V get(Object key) {
		return forward.get(key);
	}

	public boolean isEmpty() {
		return forward.isEmpty();
	}

	public Set<K> keySet() {
		return forward.keySet();
	}

	public V put(K key, V value) {
		reverse.put(value, key);
		return forward.put(key, value);
	}

	@SuppressWarnings("unchecked")
	public void putAll(Map t) {
		for (Object k:t.keySet())
			put((K)k, (V)t.get(k));
	}

	public V remove(Object key) {
		V v = forward.get(key);
		reverse.remove(v);
		return forward.remove(key);
	}

	public int size() {
		return forward.size();
	}

	public Collection<V> values() {
		return forward.values();
	}
	
	public K getReverse(V v) {
		return reverse.get(v);
	}
}
