diff -r 000000000000 -r fb279309251b project/com.nokia.carbide.cpp.epoc.engine/src/com/nokia/carbide/internal/cpp/epoc/engine/model/OrderedObjectMap.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/project/com.nokia.carbide.cpp.epoc.engine/src/com/nokia/carbide/internal/cpp/epoc/engine/model/OrderedObjectMap.java Fri Apr 03 23:33:03 2009 +0100 @@ -0,0 +1,100 @@ +/* +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of the License "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* +*/ +package com.nokia.carbide.internal.cpp.epoc.engine.model; + +import java.util.AbstractMap; +import java.util.IdentityHashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Map; +import java.util.Set; + +public class OrderedObjectMap extends AbstractMap { + + IdentityHashMap identityMap; + LinkedList ordering; + + public OrderedObjectMap() { + this.identityMap = new IdentityHashMap(); + this.ordering = new LinkedList(); + } + + @Override + public Set entrySet() { + return identityMap.entrySet(); + } + + @Override + public V put(K key, V value) { + V orig = identityMap.put(key, value); + if (orig == null) { + ordering.add(key); + } + return orig; + } + + @Override + public V remove(Object key) { + ordering.remove(key); + return super.remove(key); + } + + /** + * This method returns an iterator over the entries in + * insertion order. + * @return + */ + public Iterator> orderedIterator() { + return new Iterator>() { + Iterator listIterator = ordering.iterator(); + K lastKey = null; + public boolean hasNext() { + return listIterator.hasNext(); + } + + public java.util.Map.Entry next() { + lastKey = listIterator.next(); + Entry entry = new Entry() { + + public K getKey() { + return lastKey; + } + + public V getValue() { + return identityMap.get(lastKey); + } + + public V setValue(V value) { + return identityMap.put(lastKey, value); + } + + }; + return entry; + } + + public void remove() { + listIterator.remove(); + identityMap.remove(lastKey); + } + + }; + } + + public Iterator orderedKeyIterator() { + return ordering.iterator(); + } +}