
org.apache.commons.collections4.map.AbstractLinkedMap Maven / Gradle / Ivy
Show all versions of commons-collections4 Show documentation
/*
* 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.map;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import org.apache.commons.collections4.OrderedIterator;
import org.apache.commons.collections4.OrderedMap;
import org.apache.commons.collections4.OrderedMapIterator;
import org.apache.commons.collections4.ResettableIterator;
import org.apache.commons.collections4.iterators.EmptyOrderedIterator;
import org.apache.commons.collections4.iterators.EmptyOrderedMapIterator;
/**
* An abstract implementation of a hash-based map that links entries to create an
* ordered map and which provides numerous points for subclasses to override.
*
* This class implements all the features necessary for a subclass linked
* hash-based map. Key-value entries are stored in instances of the
* {@code LinkEntry} class which can be overridden and replaced.
* The iterators can similarly be replaced, without the need to replace the KeySet,
* EntrySet and Values view classes.
*
*
* Overridable methods are provided to change the default hashing behavior, and
* to change how entries are added to and removed from the map. Hopefully, all you
* need for unusual subclasses is here.
*
*
* This implementation maintains order by original insertion, but subclasses
* may work differently. The {@code OrderedMap} interface is implemented
* to provide access to bidirectional iteration and extra convenience methods.
*
*
* The {@code orderedMapIterator()} method provides direct access to a
* bidirectional iterator. The iterators from the other views can also be cast
* to {@code OrderedIterator} if required.
*
*
* All the available iterators can be reset back to the start by casting to
* {@code ResettableIterator} and calling {@code reset()}.
*
*
* The implementation is also designed to be subclassed, with lots of useful
* methods exposed.
*
*
* @param the type of the keys in this map
* @param the type of the values in this map
* @since 3.0
*/
public abstract class AbstractLinkedMap extends AbstractHashedMap implements OrderedMap {
/**
* EntrySet iterator.
*
* @param the key type.
* @param the value type.
*/
protected static class EntrySetIterator extends LinkIterator implements
OrderedIterator>, ResettableIterator> {
/**
* Constructs a new instance.
*
* @param parent The parent AbstractLinkedMap.
*/
protected EntrySetIterator(final AbstractLinkedMap parent) {
super(parent);
}
@Override
public Map.Entry next() {
return super.nextEntry();
}
@Override
public Map.Entry previous() {
return super.previousEntry();
}
}
/**
* KeySet iterator.
*
* @param the key type.
*/
protected static class KeySetIterator extends LinkIterator implements
OrderedIterator, ResettableIterator {
/**
* Constructs a new instance.
*
* @param parent The parent AbstractLinkedMap.
*/
@SuppressWarnings("unchecked")
protected KeySetIterator(final AbstractLinkedMap parent) {
super((AbstractLinkedMap) parent);
}
@Override
public K next() {
return super.nextEntry().getKey();
}
@Override
public K previous() {
return super.previousEntry().getKey();
}
}
/**
* LinkEntry that stores the data.
*
* If you subclass {@code AbstractLinkedMap} but not {@code LinkEntry}
* then you will not be able to access the protected fields.
* The {@code entryXxx()} methods on {@code AbstractLinkedMap} exist
* to provide the necessary access.
*
*
* @param the key type.
* @param the value type.
*/
protected static class LinkEntry extends HashEntry {
/** The entry before this one in the order */
protected LinkEntry before;
/** The entry after this one in the order */
protected LinkEntry after;
/**
* Constructs a new entry.
*
* @param next the next entry in the hash bucket sequence
* @param hashCode the hash code
* @param key the key
* @param value the value
*/
protected LinkEntry(final HashEntry next, final int hashCode, final Object key, final V value) {
super(next, hashCode, key, value);
}
}
/**
* Base Iterator that iterates in link order.
*
* @param the key type.
* @param the value type.
*/
protected abstract static class LinkIterator {
/** The parent map */
protected final AbstractLinkedMap parent;
/** The current (last returned) entry */
protected LinkEntry last;
/** The next entry */
protected LinkEntry next;
/** The modification count expected */
protected int expectedModCount;
/**
* Constructs a new instance.
*
* @param parent The parent AbstractLinkedMap.
*/
protected LinkIterator(final AbstractLinkedMap parent) {
this.parent = Objects.requireNonNull(parent);
this.next = parent.header.after;
this.expectedModCount = parent.modCount;
}
/**
* Gets the current entry.
*
* @return the current entry.
*/
protected LinkEntry currentEntry() {
return last;
}
/**
* Tests whether there is another entry.
*
* @return whether there is another entry.
*/
public boolean hasNext() {
return next != parent.header;
}
/**
* Tests whether there is a previous entry.
*
* @return whether there is a previous entry.
*/
public boolean hasPrevious() {
return next.before != parent.header;
}
/**
* Gets the next entry.
*
* @return the next entry.
*/
protected LinkEntry nextEntry() {
if (parent.modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
if (next == parent.header) {
throw new NoSuchElementException(NO_NEXT_ENTRY);
}
last = next;
next = next.after;
return last;
}
/**
* Gets the previous entry.
*
* @return the previous entry.
*/
protected LinkEntry previousEntry() {
if (parent.modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
final LinkEntry previous = next.before;
if (previous == parent.header) {
throw new NoSuchElementException(NO_PREVIOUS_ENTRY);
}
next = previous;
last = previous;
return last;
}
/**
* Removes the current entry.
*/
public void remove() {
if (last == null) {
throw new IllegalStateException(REMOVE_INVALID);
}
if (parent.modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
parent.remove(last.getKey());
last = null;
expectedModCount = parent.modCount;
}
/**
* Resets the state to the end.
*/
public void reset() {
last = null;
next = parent.header.after;
}
@Override
public String toString() {
if (last != null) {
return "Iterator[" + last.getKey() + "=" + last.getValue() + "]";
}
return "Iterator[]";
}
}
/**
* MapIterator implementation.
*
* @param the key type.
* @param the value type.
*/
protected static class LinkMapIterator extends LinkIterator implements
OrderedMapIterator, ResettableIterator {
/**
* Constructs a new instance.
*
* @param parent The parent AbstractLinkedMap.
*/
protected LinkMapIterator(final AbstractLinkedMap parent) {
super(parent);
}
@Override
public K getKey() {
final LinkEntry current = currentEntry();
if (current == null) {
throw new IllegalStateException(GETKEY_INVALID);
}
return current.getKey();
}
@Override
public V getValue() {
final LinkEntry current = currentEntry();
if (current == null) {
throw new IllegalStateException(GETVALUE_INVALID);
}
return current.getValue();
}
@Override
public K next() {
return super.nextEntry().getKey();
}
@Override
public K previous() {
return super.previousEntry().getKey();
}
@Override
public V setValue(final V value) {
final LinkEntry current = currentEntry();
if (current == null) {
throw new IllegalStateException(SETVALUE_INVALID);
}
return current.setValue(value);
}
}
/**
* Values iterator.
*
* @param the value type.
*/
protected static class ValuesIterator extends LinkIterator