📄 abstractdualbidimap.java
字号:
return false;
}
if (coll.isEmpty()) {
parent.clear();
return true;
}
boolean modified = false;
Iterator it = iterator();
while (it.hasNext()) {
if (coll.contains(it.next()) == false) {
it.remove();
modified = true;
}
}
return modified;
}
public void clear() {
parent.clear();
}
}
//-----------------------------------------------------------------------
/**
* Inner class KeySet.
*/
protected static class KeySet extends View implements Set {
/**
* Constructs a new view of the BidiMap.
*
* @param parent the parent BidiMap
*/
protected KeySet(AbstractDualBidiMap parent) {
super(parent.maps[0].keySet(), parent);
}
public Iterator iterator() {
return parent.createKeySetIterator(super.iterator());
}
public boolean contains(Object key) {
return parent.maps[0].containsKey(key);
}
public boolean remove(Object key) {
if (parent.maps[0].containsKey(key)) {
Object value = parent.maps[0].remove(key);
parent.maps[1].remove(value);
return true;
}
return false;
}
}
/**
* Inner class KeySetIterator.
*/
protected static class KeySetIterator extends AbstractIteratorDecorator {
/** The parent map */
protected final AbstractDualBidiMap parent;
/** The last returned key */
protected Object lastKey = null;
/** Whether remove is allowed at present */
protected boolean canRemove = false;
/**
* Constructor.
* @param iterator the iterator to decorate
* @param parent the parent map
*/
protected KeySetIterator(Iterator iterator, AbstractDualBidiMap parent) {
super(iterator);
this.parent = parent;
}
public Object next() {
lastKey = super.next();
canRemove = true;
return lastKey;
}
public void remove() {
if (canRemove == false) {
throw new IllegalStateException("Iterator remove() can only be called once after next()");
}
Object value = parent.maps[0].get(lastKey);
super.remove();
parent.maps[1].remove(value);
lastKey = null;
canRemove = false;
}
}
//-----------------------------------------------------------------------
/**
* Inner class Values.
*/
protected static class Values extends View implements Set {
/**
* Constructs a new view of the BidiMap.
*
* @param parent the parent BidiMap
*/
protected Values(AbstractDualBidiMap parent) {
super(parent.maps[0].values(), parent);
}
public Iterator iterator() {
return parent.createValuesIterator(super.iterator());
}
public boolean contains(Object value) {
return parent.maps[1].containsKey(value);
}
public boolean remove(Object value) {
if (parent.maps[1].containsKey(value)) {
Object key = parent.maps[1].remove(value);
parent.maps[0].remove(key);
return true;
}
return false;
}
}
/**
* Inner class ValuesIterator.
*/
protected static class ValuesIterator extends AbstractIteratorDecorator {
/** The parent map */
protected final AbstractDualBidiMap parent;
/** The last returned value */
protected Object lastValue = null;
/** Whether remove is allowed at present */
protected boolean canRemove = false;
/**
* Constructor.
* @param iterator the iterator to decorate
* @param parent the parent map
*/
protected ValuesIterator(Iterator iterator, AbstractDualBidiMap parent) {
super(iterator);
this.parent = parent;
}
public Object next() {
lastValue = super.next();
canRemove = true;
return lastValue;
}
public void remove() {
if (canRemove == false) {
throw new IllegalStateException("Iterator remove() can only be called once after next()");
}
super.remove(); // removes from maps[0]
parent.maps[1].remove(lastValue);
lastValue = null;
canRemove = false;
}
}
//-----------------------------------------------------------------------
/**
* Inner class EntrySet.
*/
protected static class EntrySet extends View implements Set {
/**
* Constructs a new view of the BidiMap.
*
* @param parent the parent BidiMap
*/
protected EntrySet(AbstractDualBidiMap parent) {
super(parent.maps[0].entrySet(), parent);
}
public Iterator iterator() {
return parent.createEntrySetIterator(super.iterator());
}
public boolean remove(Object obj) {
if (obj instanceof Map.Entry == false) {
return false;
}
Map.Entry entry = (Map.Entry) obj;
Object key = entry.getKey();
if (parent.containsKey(key)) {
Object value = parent.maps[0].get(key);
if (value == null ? entry.getValue() == null : value.equals(entry.getValue())) {
parent.maps[0].remove(key);
parent.maps[1].remove(value);
return true;
}
}
return false;
}
}
/**
* Inner class EntrySetIterator.
*/
protected static class EntrySetIterator extends AbstractIteratorDecorator {
/** The parent map */
protected final AbstractDualBidiMap parent;
/** The last returned entry */
protected Map.Entry last = null;
/** Whether remove is allowed at present */
protected boolean canRemove = false;
/**
* Constructor.
* @param iterator the iterator to decorate
* @param parent the parent map
*/
protected EntrySetIterator(Iterator iterator, AbstractDualBidiMap parent) {
super(iterator);
this.parent = parent;
}
public Object next() {
last = new MapEntry((Map.Entry) super.next(), parent);
canRemove = true;
return last;
}
public void remove() {
if (canRemove == false) {
throw new IllegalStateException("Iterator remove() can only be called once after next()");
}
// store value as remove may change the entry in the decorator (eg.TreeMap)
Object value = last.getValue();
super.remove();
parent.maps[1].remove(value);
last = null;
canRemove = false;
}
}
/**
* Inner class MapEntry.
*/
protected static class MapEntry extends AbstractMapEntryDecorator {
/** The parent map */
protected final AbstractDualBidiMap parent;
/**
* Constructor.
* @param entry the entry to decorate
* @param parent the parent map
*/
protected MapEntry(Map.Entry entry, AbstractDualBidiMap parent) {
super(entry);
this.parent = parent;
}
public Object setValue(Object value) {
Object key = MapEntry.this.getKey();
if (parent.maps[1].containsKey(value) &&
parent.maps[1].get(value) != key) {
throw new IllegalArgumentException("Cannot use setValue() when the object being set is already in the map");
}
parent.put(key, value);
final Object oldValue = super.setValue(value);
return oldValue;
}
}
/**
* Inner class MapIterator.
*/
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 = null;
/** Whether remove is allowed at present */
protected boolean canRemove = false;
/**
* Constructor.
* @param parent the parent map
*/
protected BidiMapIterator(AbstractDualBidiMap parent) {
super();
this.parent = parent;
this.iterator = parent.maps[0].entrySet().iterator();
}
public boolean hasNext() {
return iterator.hasNext();
}
public Object next() {
last = (Map.Entry) iterator.next();
canRemove = true;
return last.getKey();
}
public void remove() {
if (canRemove == false) {
throw new IllegalStateException("Iterator remove() can only be called once after next()");
}
// store value as remove may change the entry in the decorator (eg.TreeMap)
Object value = last.getValue();
iterator.remove();
parent.maps[1].remove(value);
last = null;
canRemove = false;
}
public Object getKey() {
if (last == null) {
throw new IllegalStateException("Iterator getKey() can only be called after next() and before remove()");
}
return last.getKey();
}
public Object getValue() {
if (last == null) {
throw new IllegalStateException("Iterator getValue() can only be called after next() and before remove()");
}
return last.getValue();
}
public Object setValue(Object value) {
if (last == null) {
throw new IllegalStateException("Iterator setValue() can only be called after next() and before remove()");
}
if (parent.maps[1].containsKey(value) &&
parent.maps[1].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);
}
public void reset() {
iterator = parent.maps[0].entrySet().iterator();
last = null;
canRemove = false;
}
public String toString() {
if (last != null) {
return "MapIterator[" + getKey() + "=" + getValue() + "]";
} else {
return "MapIterator[]";
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -