org.apache.commons.collections.map.ListOrderedMap Maven / Gradle / Ivy
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003-2004 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [email protected].
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* .
*/
package org.apache.commons.collections.map;
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import org.apache.commons.collections.MapIterator;
import org.apache.commons.collections.OrderedMap;
import org.apache.commons.collections.OrderedMapIterator;
import org.apache.commons.collections.ResettableIterator;
import org.apache.commons.collections.iterators.AbstractIteratorDecorator;
import org.apache.commons.collections.keyvalue.AbstractMapEntry;
import org.apache.commons.collections.list.UnmodifiableList;
/**
* Decorates a Map
to ensure that the order of addition is retained.
*
* The order will be used via the iterators and toArray methods on the views.
* The order is also returned by the MapIterator
.
* The orderedMapIterator()
method accesses an iterator that can
* iterate both forwards and backwards through the map.
* In addition, non-interface methods are provided to access the map by index.
*
* If an object is added to the Map for a second time, it will remain in the
* original position in the iteration.
*
* @since Commons Collections 3.0
* @version $Revision: 1.11 $ $Date: 2004/01/14 21:43:11 $
*
* @author Henri Yandell
* @author Stephen Colebourne
*/
public class ListOrderedMap
extends AbstractMapDecorator implements OrderedMap {
/** Internal list to hold the sequence of objects */
protected final List insertOrder = new ArrayList();
/**
* Factory method to create an ordered map.
*
* An ArrayList
is used to retain order.
*
* @param map the map to decorate, must not be null
* @throws IllegalArgumentException if map is null
*/
public static OrderedMap decorate(Map map) {
return new ListOrderedMap(map);
}
//-----------------------------------------------------------------------
/**
* Constructor that wraps (not copies).
*
* @param map the map to decorate, must not be null
* @throws IllegalArgumentException if map is null
*/
protected ListOrderedMap(Map map) {
super(map);
insertOrder.addAll(getMap().keySet());
}
// Implement OrderedMap
//-----------------------------------------------------------------------
public MapIterator mapIterator() {
return orderedMapIterator();
}
public OrderedMapIterator orderedMapIterator() {
return new ListOrderedMapIterator(this);
}
/**
* Gets the first key in this map by insert order.
*
* @return the first key currently in this map
* @throws NoSuchElementException if this map is empty
*/
public Object firstKey() {
if (size() == 0) {
throw new NoSuchElementException("Map is empty");
}
return insertOrder.get(0);
}
/**
* Gets the last key in this map by insert order.
*
* @return the last key currently in this map
* @throws NoSuchElementException if this map is empty
*/
public Object lastKey() {
if (size() == 0) {
throw new NoSuchElementException("Map is empty");
}
return insertOrder.get(size() - 1);
}
/**
* Gets the next key to the one specified using insert order.
* This method performs a list search to find the key and is O(n).
*
* @param key the key to find previous for
* @return the next key, null if no match or at start
*/
public Object nextKey(Object key) {
int index = insertOrder.indexOf(key);
if (index >= 0 && index < size() - 1) {
return insertOrder.get(index + 1);
}
return null;
}
/**
* Gets the previous key to the one specified using insert order.
* This method performs a list search to find the key and is O(n).
*
* @param key the key to find previous for
* @return the previous key, null if no match or at start
*/
public Object previousKey(Object key) {
int index = insertOrder.indexOf(key);
if (index > 0) {
return insertOrder.get(index - 1);
}
return null;
}
//-----------------------------------------------------------------------
public Object put(Object key, Object value) {
if (getMap().containsKey(key)) {
// re-adding doesn't change order
return getMap().put(key, value);
} else {
// first add, so add to both map and list
Object result = getMap().put(key, value);
insertOrder.add(key);
return result;
}
}
public void putAll(Map map) {
for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
put(entry.getKey(), entry.getValue());
}
}
public Object remove(Object key) {
Object result = getMap().remove(key);
insertOrder.remove(key);
return result;
}
public void clear() {
getMap().clear();
insertOrder.clear();
}
//-----------------------------------------------------------------------
public Set keySet() {
return new KeySetView(this);
}
public Collection values() {
return new ValuesView(this);
}
public Set entrySet() {
return new EntrySetView(this, this.insertOrder);
}
//-----------------------------------------------------------------------
/**
* Returns the Map as a string.
*
* @return the Map as a String
*/
public String toString() {
if (isEmpty()) {
return "{}";
}
StringBuffer buf = new StringBuffer();
buf.append('{');
boolean first = true;
Iterator it = entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
if (first) {
first = false;
} else {
buf.append(", ");
}
buf.append(key == this ? "(this Map)" : key);
buf.append('=');
buf.append(value == this ? "(this Map)" : value);
}
buf.append('}');
return buf.toString();
}
//-----------------------------------------------------------------------
/**
* Gets the key at the specified index.
*
* @param index the index to retrieve
* @return the key at the specified index
* @throws IndexOutOfBoundsException if the index is invalid
*/
public Object get(int index) {
return insertOrder.get(index);
}
/**
* Gets the value at the specified index.
*
* @param index the index to retrieve
* @return the key at the specified index
* @throws IndexOutOfBoundsException if the index is invalid
*/
public Object getValue(int index) {
return get(insertOrder.get(index));
}
/**
* Gets the index of the specified key.
*
* @param key the key to find the index of
* @return the index, or -1 if not found
*/
public int indexOf(Object key) {
return insertOrder.indexOf(key);
}
/**
* Removes the element at the specified index.
*
* @param index the index of the object to remove
* @return the previous value corresponding the key
,
* or null
if none existed
* @throws IndexOutOfBoundsException if the index is invalid
*/
public Object remove(int index) {
return remove(get(index));
}
/**
* Gets an unmodifiable List view of the keys which changes as the map changes.
*
* The returned list is unmodifiable because changes to the values of
* the list (using {@link java.util.ListIterator#set(Object)}) will
* effectively remove the value from the list and reinsert that value at
* the end of the list, which is an unexpected side effect of changing the
* value of a list. This occurs because changing the key, changes when the
* mapping is added to the map and thus where it appears in the list.
*
* An alternative to this method is to use {@link #keySet()}.
*
* @see #keySet()
* @return The ordered list of keys.
*/
public List asList() {
return UnmodifiableList.decorate(insertOrder);
}
//-----------------------------------------------------------------------
static class ValuesView extends AbstractCollection {
private final ListOrderedMap parent;
ValuesView(ListOrderedMap parent) {
super();
this.parent = parent;
}
public int size() {
return this.parent.size();
}
public boolean contains(Object value) {
return this.parent.containsValue(value);
}
public void clear() {
this.parent.clear();
}
public Iterator iterator() {
return new AbstractIteratorDecorator(parent.entrySet().iterator()) {
public Object next() {
return ((Map.Entry) iterator.next()).getValue();
}
};
}
}
//-----------------------------------------------------------------------
static class KeySetView extends AbstractSet {
private final ListOrderedMap parent;
KeySetView(ListOrderedMap parent) {
super();
this.parent = parent;
}
public int size() {
return this.parent.size();
}
public boolean contains(Object value) {
return this.parent.containsKey(value);
}
public void clear() {
this.parent.clear();
}
public Iterator iterator() {
return new AbstractIteratorDecorator(parent.entrySet().iterator()) {
public Object next() {
return ((Map.Entry) super.next()).getKey();
}
};
}
}
//-----------------------------------------------------------------------
static class EntrySetView extends AbstractSet {
private final ListOrderedMap parent;
private final List insertOrder;
private Set entrySet;
public EntrySetView(ListOrderedMap parent, List insertOrder) {
super();
this.parent = parent;
this.insertOrder = insertOrder;
}
private Set getEntrySet() {
if (entrySet == null) {
entrySet = parent.getMap().entrySet();
}
return entrySet;
}
public int size() {
return this.parent.size();
}
public boolean isEmpty() {
return this.parent.isEmpty();
}
public boolean contains(Object obj) {
return getEntrySet().contains(obj);
}
public boolean containsAll(Collection coll) {
return getEntrySet().containsAll(coll);
}
public boolean remove(Object obj) {
if (obj instanceof Map.Entry == false) {
return false;
}
Object key = ((Map.Entry) obj).getKey();
if (parent.getMap().containsKey(key) == false) {
return false;
}
parent.remove(key);
return true;
}
public void clear() {
this.parent.clear();
}
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
return getEntrySet().equals(obj);
}
public int hashCode() {
return getEntrySet().hashCode();
}
public String toString() {
return getEntrySet().toString();
}
public Iterator iterator() {
return new ListOrderedIterator(parent, insertOrder);
}
}
//-----------------------------------------------------------------------
static class ListOrderedIterator extends AbstractIteratorDecorator {
private final ListOrderedMap parent;
private Object last = null;
ListOrderedIterator(ListOrderedMap parent, List insertOrder) {
super(insertOrder.iterator());
this.parent = parent;
}
public Object next() {
last = super.next();
return new ListOrderedMapEntry(parent, last);
}
public void remove() {
super.remove();
parent.getMap().remove(last);
}
}
//-----------------------------------------------------------------------
static class ListOrderedMapEntry extends AbstractMapEntry {
private final ListOrderedMap parent;
ListOrderedMapEntry(ListOrderedMap parent, Object key) {
super(key, null);
this.parent = parent;
}
public Object getValue() {
return parent.get(key);
}
public Object setValue(Object value) {
return parent.getMap().put(key, value);
}
}
//-----------------------------------------------------------------------
static class ListOrderedMapIterator implements OrderedMapIterator, ResettableIterator {
private final ListOrderedMap parent;
private ListIterator iterator;
private Object last = null;
private boolean readable = false;
ListOrderedMapIterator(ListOrderedMap parent) {
super();
this.parent = parent;
this.iterator = parent.insertOrder.listIterator();
}
public boolean hasNext() {
return iterator.hasNext();
}
public Object next() {
last = iterator.next();
readable = true;
return last;
}
public boolean hasPrevious() {
return iterator.hasPrevious();
}
public Object previous() {
last = iterator.previous();
readable = true;
return last;
}
public void remove() {
if (readable == false) {
throw new IllegalStateException(AbstractHashedMap.REMOVE_INVALID);
}
iterator.remove();
parent.map.remove(last);
readable = false;
}
public Object getKey() {
if (readable == false) {
throw new IllegalStateException(AbstractHashedMap.GETKEY_INVALID);
}
return last;
}
public Object getValue() {
if (readable == false) {
throw new IllegalStateException(AbstractHashedMap.GETVALUE_INVALID);
}
return parent.get(last);
}
public Object setValue(Object value) {
if (readable == false) {
throw new IllegalStateException(AbstractHashedMap.SETVALUE_INVALID);
}
return parent.map.put(last, value);
}
public void reset() {
iterator = parent.insertOrder.listIterator();
last = null;
readable = false;
}
public String toString() {
if (readable == true) {
return "Iterator[" + getKey() + "=" + getValue() + "]";
} else {
return "Iterator[]";
}
}
}
}