
org.apache.commons.collections4.bidimap.AbstractDualBidiMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-collections4 Show documentation
Show all versions of commons-collections4 Show documentation
The Apache Commons Collections package contains types that extend and augment the Java Collections Framework.
The newest version!
/*
* 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.commons.collections4.bidimap;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.MapIterator;
import org.apache.commons.collections4.ResettableIterator;
import org.apache.commons.collections4.collection.AbstractCollectionDecorator;
import org.apache.commons.collections4.iterators.AbstractIteratorDecorator;
import org.apache.commons.collections4.keyvalue.AbstractMapEntryDecorator;
/**
* Abstract {@link BidiMap} implemented using two maps.
*
* An implementation can be written simply by implementing the
* {@link #createBidiMap(Map, Map, BidiMap)} method.
*
*
* @param the type of the keys in the map
* @param the type of the values in the map
* @see DualHashBidiMap
* @see DualTreeBidiMap
* @since 3.0
*/
public abstract class AbstractDualBidiMap implements BidiMap {
/**
* Inner class MapIterator.
*
* @param the type of the keys.
* @param the type of the values.
*/
protected static class BidiMapIterator implements MapIterator, ResettableIterator {
/** The parent map */
protected final AbstractDualBidiMap parent;
/** The iterator being wrapped */
protected Iterator> iterator;
/** The last returned entry */
protected Map.Entry last;
/** Whether remove is allowed at present */
protected boolean canRemove;
/**
* Constructs a new instance.
* @param parent the parent map
*/
protected BidiMapIterator(final AbstractDualBidiMap parent) {
this.parent = parent;
this.iterator = parent.normalMap.entrySet().iterator();
}
@Override
public K getKey() {
if (last == null) {
throw new IllegalStateException(
"Iterator getKey() can only be called after next() and before remove()");
}
return last.getKey();
}
@Override
public V getValue() {
if (last == null) {
throw new IllegalStateException(
"Iterator getValue() can only be called after next() and before remove()");
}
return last.getValue();
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public K next() {
last = iterator.next();
canRemove = true;
return last.getKey();
}
@Override
public void remove() {
if (!canRemove) {
throw new IllegalStateException("Iterator remove() can only be called once after next()");
}
// store value as remove may change the entry in the decorator (e.g. TreeMap)
final V value = last.getValue();
iterator.remove();
parent.reverseMap.remove(value);
last = null;
canRemove = false;
}
@Override
public void reset() {
iterator = parent.normalMap.entrySet().iterator();
last = null;
canRemove = false;
}
@Override
public V setValue(final V value) {
if (last == null) {
throw new IllegalStateException(
"Iterator setValue() can only be called after next() and before remove()");
}
if (parent.reverseMap.containsKey(value) &&
parent.reverseMap.get(value) != last.getKey()) {
throw new IllegalArgumentException(
"Cannot use setValue() when the object being set is already in the map");
}
return parent.put(last.getKey(), value);
}
@Override
public String toString() {
if (last != null) {
return "MapIterator[" + getKey() + "=" + getValue() + "]";
}
return "MapIterator[]";
}
}
/**
* Inner class EntrySet.
*
* @param the type of the keys.
* @param the type of the values.
*/
protected static class EntrySet extends View> implements Set> {
/** Serialization version */
private static final long serialVersionUID = 4040410962603292348L;
/**
* Constructs a new instance.
*
* @param parent the parent BidiMap
*/
protected EntrySet(final AbstractDualBidiMap parent) {
super(parent.normalMap.entrySet(), parent);
}
@Override
public Iterator> iterator() {
return parent.createEntrySetIterator(super.iterator());
}
@Override
public boolean remove(final Object obj) {
if (!(obj instanceof Map.Entry)) {
return false;
}
final Map.Entry, ?> entry = (Map.Entry, ?>) obj;
final Object key = entry.getKey();
if (parent.containsKey(key)) {
final V value = parent.normalMap.get(key);
if (Objects.equals(value, entry.getValue())) {
parent.normalMap.remove(key);
parent.reverseMap.remove(value);
return true;
}
}
return false;
}
}
/**
* Inner class EntrySetIterator.
*
* @param the type of the keys.
* @param the type of the values.
*/
protected static class EntrySetIterator extends AbstractIteratorDecorator> {
/** The parent map */
protected final AbstractDualBidiMap parent;
/** The last returned entry */
protected Map.Entry last;
/** Whether remove is allowed at present */
protected boolean canRemove;
/**
* Constructs a new instance.
* @param iterator the iterator to decorate
* @param parent the parent map
*/
protected EntrySetIterator(final Iterator> iterator, final AbstractDualBidiMap parent) {
super(iterator);
this.parent = parent;
}
@Override
public Map.Entry next() {
last = new MapEntry<>(super.next(), parent);
canRemove = true;
return last;
}
@Override
public void remove() {
if (!canRemove) {
throw new IllegalStateException("Iterator remove() can only be called once after next()");
}
// store value as remove may change the entry in the decorator (e.g. TreeMap)
final Object value = last.getValue();
super.remove();
parent.reverseMap.remove(value);
last = null;
canRemove = false;
}
}
/**
* Inner class KeySet.
*
* @param the type of elements maintained by this set
*/
protected static class KeySet extends View implements Set {
/** Serialization version */
private static final long serialVersionUID = -7107935777385040694L;
/**
* Constructs a new instance.
*
* @param parent the parent BidiMap
*/
@SuppressWarnings("unchecked")
protected KeySet(final AbstractDualBidiMap parent) {
super(parent.normalMap.keySet(), (AbstractDualBidiMap) parent);
}
@Override
public boolean contains(final Object key) {
return parent.normalMap.containsKey(key);
}
@Override
public Iterator iterator() {
return parent.createKeySetIterator(super.iterator());
}
@Override
public boolean remove(final Object key) {
if (parent.normalMap.containsKey(key)) {
final Object value = parent.normalMap.remove(key);
parent.reverseMap.remove(value);
return true;
}
return false;
}
}
/**
* Inner class KeySetIterator.
*
* @param the key type.
*/
protected static class KeySetIterator extends AbstractIteratorDecorator {
/** The parent map */
protected final AbstractDualBidiMap parent;
/** The last returned key */
protected K lastKey;
/** Whether remove is allowed at present */
protected boolean canRemove;
/**
* Constructs a new instance.
* @param iterator the iterator to decorate
* @param parent the parent map
*/
protected KeySetIterator(final Iterator iterator, final AbstractDualBidiMap parent) {
super(iterator);
this.parent = parent;
}
@Override
public K next() {
lastKey = super.next();
canRemove = true;
return lastKey;
}
@Override
public void remove() {
if (!canRemove) {
throw new IllegalStateException("Iterator remove() can only be called once after next()");
}
final Object value = parent.normalMap.get(lastKey);
super.remove();
parent.reverseMap.remove(value);
lastKey = null;
canRemove = false;
}
}
/**
* Inner class MapEntry.
*
* @param the type of the keys.
* @param the type of the values.
*/
protected static class MapEntry extends AbstractMapEntryDecorator {
/** The parent map */
protected final AbstractDualBidiMap parent;
/**
* Constructs a new instance.
* @param entry the entry to decorate
* @param parent the parent map
*/
protected MapEntry(final Map.Entry entry, final AbstractDualBidiMap parent) {
super(entry);
this.parent = parent;
}
@Override
public V setValue(final V value) {
final K key = getKey();
if (parent.reverseMap.containsKey(value) &&
parent.reverseMap.get(value) != key) {
throw new IllegalArgumentException(
"Cannot use setValue() when the object being set is already in the map");
}
parent.put(key, value);
return super.setValue(value);
}
}
/**
* Inner class Values.
*
* @param the type of the values.
*/
protected static class Values extends View
© 2015 - 2025 Weber Informatics LLC | Privacy Policy