project/com.nokia.carbide.cpp.epoc.engine/src/com/nokia/carbide/internal/cpp/epoc/engine/model/OrderedObjectMap.java
changeset 0 fb279309251b
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 package com.nokia.carbide.internal.cpp.epoc.engine.model;
       
    18 
       
    19 import java.util.AbstractMap;
       
    20 import java.util.IdentityHashMap;
       
    21 import java.util.Iterator;
       
    22 import java.util.LinkedList;
       
    23 import java.util.Map;
       
    24 import java.util.Set;
       
    25 
       
    26 public class OrderedObjectMap<K,V> extends AbstractMap<K,V> {
       
    27 
       
    28 	IdentityHashMap<K, V> identityMap;
       
    29 	LinkedList<K> ordering;
       
    30 	
       
    31 	public OrderedObjectMap() {
       
    32 		this.identityMap = new IdentityHashMap<K, V>();
       
    33 		this.ordering = new LinkedList<K>();
       
    34 	}
       
    35 	
       
    36 	@Override
       
    37 	public Set entrySet() {
       
    38 		return identityMap.entrySet();
       
    39 	}
       
    40 
       
    41 	@Override
       
    42 	public V put(K key, V value) {
       
    43 		V orig = identityMap.put(key, value);
       
    44 		if (orig == null) {
       
    45 			ordering.add(key);
       
    46 		}
       
    47 		return orig;
       
    48 	}
       
    49 	
       
    50 	@Override
       
    51 	public V remove(Object key) {
       
    52 		ordering.remove(key);
       
    53 		return super.remove(key);
       
    54 	}
       
    55 	
       
    56 	/**
       
    57 	 * This method returns an iterator over the entries in
       
    58 	 * insertion order.
       
    59 	 * @return
       
    60 	 */
       
    61 	public Iterator<Map.Entry<K, V>> orderedIterator() {
       
    62 		return new Iterator<Entry<K,V>>() {
       
    63 			Iterator<K> listIterator = ordering.iterator();
       
    64 			K lastKey = null;
       
    65 			public boolean hasNext() {
       
    66 				return listIterator.hasNext();
       
    67 			}
       
    68 
       
    69 			public java.util.Map.Entry<K, V> next() {
       
    70 				lastKey = listIterator.next();
       
    71 				Entry<K,V> entry = new Entry<K,V>() {
       
    72 
       
    73 					public K getKey() {
       
    74 						return lastKey;
       
    75 					}
       
    76 
       
    77 					public V getValue() {
       
    78 						return identityMap.get(lastKey);
       
    79 					}
       
    80 
       
    81 					public V setValue(V value) {
       
    82 						return identityMap.put(lastKey, value);
       
    83 					}
       
    84 					
       
    85 				};
       
    86 				return entry;
       
    87 			}
       
    88 
       
    89 			public void remove() {
       
    90 				listIterator.remove();
       
    91 				identityMap.remove(lastKey);
       
    92 			}
       
    93 			
       
    94 		};
       
    95 	}
       
    96 
       
    97 	public Iterator<K> orderedKeyIterator() {
       
    98 		return ordering.iterator();
       
    99 	}
       
   100 }