/*
 * 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.flink.runtime.jobmaster.slotpool;

import org.apache.flink.api.java.tuple.Tuple2;

import java.util.AbstractCollection;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;

/**
 * Map which stores values under two different indices.
 *
 * @param <A> Type of key A
 * @param <B> Type of key B
 * @param <V> Type of the value
 */
public class DualKeyLinkedMap<A, B, V> {

	private final LinkedHashMap<A, Tuple2<B, V>> aMap;

	private final LinkedHashMap<B, A> bMap;

	private transient Collection<V> values;

	public DualKeyLinkedMap(int initialCapacity) {
		this.aMap = new LinkedHashMap<>(initialCapacity);
		this.bMap = new LinkedHashMap<>(initialCapacity);
	}

	public int size() {
		return aMap.size();
	}

	public V getKeyA(A aKey) {
		final Tuple2<B, V> value = aMap.get(aKey);

		if (value != null) {
			return value.f1;
		} else {
			return null;
		}
	}

	public V getKeyB(B bKey) {
		final A aKey = bMap.get(bKey);

		if (aKey != null) {
			return aMap.get(aKey).f1;
		} else {
			return null;
		}
	}

	public V put(A aKey, B bKey, V value) {
		final V removedValue = removeKeyA(aKey);
		removeKeyB(bKey);

		aMap.put(aKey, Tuple2.of(bKey, value));
		bMap.put(bKey, aKey);

		if (removedValue != null) {
			return removedValue;
		} else {
			return null;
		}
	}

	public boolean containsKeyA(A aKey) {
		return aMap.containsKey(aKey);
	}

	public boolean containsKeyB(B bKey) {
		return bMap.containsKey(bKey);
	}

	public V removeKeyA(A aKey) {
		Tuple2<B, V> aValue = aMap.remove(aKey);

		if (aValue != null) {
			bMap.remove(aValue.f0);
			return aValue.f1;
		} else {
			return null;
		}
	}

	public V removeKeyB(B bKey) {
		A aKey = bMap.remove(bKey);

		if (aKey != null) {
			Tuple2<B, V> aValue = aMap.remove(aKey);
			if (aValue != null) {
				return aValue.f1;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	public Collection<V> values() {
		Collection<V> vs = values;

		if (vs == null) {
			vs = new Values();
			values = vs;
		}

		return vs;
	}

	public Set<A> keySetA() {
		return aMap.keySet();
	}

	public Set<B> keySetB() {
		return bMap.keySet();
	}

	public void clear() {
		aMap.clear();
		bMap.clear();
	}

	// -----------------------------------------------------------------------
	// Inner classes
	// -----------------------------------------------------------------------

	/**
	 * Collection which contains the values of the dual key map.
	 */
	private final class Values extends AbstractCollection<V> {

		@Override
		public Iterator<V> iterator() {
			return new ValueIterator();
		}

		@Override
		public int size() {
			return aMap.size();
		}
	}

	/**
	 * Iterator which iterates over the values of the dual key map.
	 */
	private final class ValueIterator implements Iterator<V> {

		private final Iterator<Tuple2<B, V>> iterator = aMap.values().iterator();

		@Override
		public boolean hasNext() {
			return iterator.hasNext();
		}

		@Override
		public V next() {
			Tuple2<B, V> value = iterator.next();

			return value.f1;
		}
	}
}

@@ -1205,23 +1205,16 @@
 lue%0A */%0A
-public 
 class Du
@@ -1378,23 +1378,16 @@
 lues;%0A%0A%09
-public 
 DualKeyL
@@ -1527,23 +1527,16 @@
 );%0A%09%7D%0A%0A%09
-public 
 int size
@@ -1563,31 +1563,24 @@
 ize();%0A%09%7D%0A%0A%09
-public 
 V getKeyA(A 
@@ -1712,23 +1712,16 @@
 %09%7D%0A%09%7D%0A%0A%09
-public 
 V getKey
@@ -1853,23 +1853,16 @@
 %09%7D%0A%09%7D%0A%0A%09
-public 
 V put(A 
@@ -2103,31 +2103,24 @@
 l;%0A%09%09%7D%0A%09%7D%0A%0A%09
-public 
 boolean cont
@@ -2176,23 +2176,16 @@
 );%0A%09%7D%0A%0A%09
-public 
 boolean 
@@ -2241,31 +2241,24 @@
 bKey);%0A%09%7D%0A%0A%09
-public 
 V removeKeyA
@@ -2420,23 +2420,16 @@
 %09%7D%0A%09%7D%0A%0A%09
-public 
 V remove
@@ -2657,23 +2657,16 @@
 %09%7D%0A%09%7D%0A%0A%09
-public 
 Collecti
@@ -2789,31 +2789,24 @@
 rn vs;%0A%09%7D%0A%0A%09
-public 
 Set%3CA%3E keySe
@@ -2841,23 +2841,16 @@
 );%0A%09%7D%0A%0A%09
-public 
 Set%3CB%3E k
@@ -2889,23 +2889,16 @@
 );%0A%09%7D%0A%0A%09
-public 
 void cle

Make DualKeyLinkedMap package private