All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.conqat.lib.commons.collections.BidirectionalMap Maven / Gradle / Ivy

There is a newer version: 2024.7.2
Show newest version
/*
 * Copyright (c) CQSE GmbH
 *
 * 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 org.conqat.lib.commons.collections;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.conqat.lib.commons.assertion.CCSMAssert;

/**
 * A collection which implements a bidirectional mapping.
 */
public class BidirectionalMap {

	/** Mapping from s to t. */
	private final Map stMap;

	/** Mapping from t to s. */
	private final Map tsMap;

	/** Creates new bidirectional map based on hash maps. */
	public BidirectionalMap() {
		stMap = new HashMap<>();
		tsMap = new HashMap<>();
	}

	/** Creates new bidirectional map based given maps. */
	@SuppressWarnings("null")
	public BidirectionalMap(Map stMap, Map tsMap) {
		CCSMAssert.isTrue(stMap != null && tsMap != null, "Maps may not be null!");
		CCSMAssert.isTrue(stMap != tsMap, "Maps may not be equal!");
		CCSMAssert.isTrue(stMap.isEmpty() && tsMap.isEmpty(), "Maps may not be used (filled)!");

		this.stMap = stMap;
		this.tsMap = tsMap;
	}

	/** Get first element. */
	public S getFirst(T t) {
		return tsMap.get(t);
	}

	/** Get second element. */
	public T getSecond(S s) {
		return stMap.get(s);
	}

	/** Returns whether this map is empty. */
	public boolean isEmpty() {
		return stMap.isEmpty();
	}

	/** Returns the size. */
	public int size() {
		return stMap.size();
	}

	/** Clears the map. */
	public void clear() {
		stMap.clear();
		tsMap.clear();
	}

	/**
	 * Returns whether the given element is in the first set (the domain of the bijection).
	 */
	public boolean containsFirst(S s) {
		return stMap.containsKey(s);
	}

	/**
	 * Returns whether the given element is in the second set (the range of the bijection).
	 */
	public boolean containsSecond(T t) {
		return tsMap.containsKey(t);
	}

	/** Returns the first set (the domain). */
	public UnmodifiableSet getFirstSet() {
		return CollectionUtils.asUnmodifiable(stMap.keySet());
	}

	/** Returns the second set (the range). */
	public UnmodifiableSet getSecondSet() {
		return CollectionUtils.asUnmodifiable(tsMap.keySet());
	}

	/** Returns the entries. */
	public UnmodifiableSet> getEntrySet() {
		return CollectionUtils.asUnmodifiable(stMap.entrySet());
	}

	/** Returns the inverted entries. */
	public UnmodifiableSet> getInvertedEntrySet() {
		return CollectionUtils.asUnmodifiable(tsMap.entrySet());
	}

	/**
	 * Inserts the given pair into the bijection. Any mapping associated with those values is removed
	 * before. This map does not support null values.
	 */
	public void put(S s, T t) {
		CCSMAssert.isTrue(s != null && t != null, "null values not supported.");

		removeFirst(s);
		removeSecond(t);

		stMap.put(s, t);
		tsMap.put(t, s);
	}

	/** Removes the first object */
	public void removeFirst(S s) {
		T t = stMap.get(s);
		if (t != null) {
			stMap.remove(s);
			tsMap.remove(t);
		}
	}

	/** Removes the second object */
	public void removeSecond(T t) {
		S s = tsMap.get(t);
		if (s != null) {
			stMap.remove(s);
			tsMap.remove(t);
		}
	}

	/** {@inheritDoc} */
	@Override
	public String toString() {
		return stMap.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy