project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/ObservableLinkedHashMap.java
changeset 7342 0e29eec2df5c
equal deleted inserted replaced
7340:62043f5f7c67 7342:0e29eec2df5c
       
     1 package org.hedgewars.hedgeroid.netplay;
       
     2 
       
     3 import java.util.Collection;
       
     4 import java.util.Collections;
       
     5 import java.util.LinkedHashMap;
       
     6 import java.util.LinkedList;
       
     7 import java.util.List;
       
     8 import java.util.Map;
       
     9 
       
    10 /**
       
    11  * A map of items, sorted by time of insertion (earliest first). 
       
    12  * Observers can be notified about insertions, deletions and changes (which don't change the order).
       
    13  * This is useful for e.g. the lists of current rooms and players, because it allows easy addition
       
    14  * and removal of entries on the one side, as well as reaction to these events by UI elements.
       
    15  */
       
    16 public class ObservableLinkedHashMap<K,V> {
       
    17 	private LinkedHashMap<K,V> map = new LinkedHashMap<K,V>();
       
    18 	private List<Observer<K,V>> observers = new LinkedList<Observer<K,V>>();
       
    19 	
       
    20 	public Collection<V> getValues() {
       
    21 		return Collections.unmodifiableCollection(map.values());
       
    22 	}
       
    23 	
       
    24 	public Map<K,V> getMap() {
       
    25 		return Collections.unmodifiableMap(map);
       
    26 	}
       
    27 	
       
    28 	public void observe(Observer<K,V> observer) {
       
    29 		observers.add(observer);
       
    30 	}
       
    31 	
       
    32 	public void unobserve(Observer<K,V> observer) {
       
    33 		observers.remove(observer);
       
    34 	}
       
    35 	
       
    36 	// TODO ugh
       
    37 	public void clear() {
       
    38 		while(!map.isEmpty()) {
       
    39 			remove(map.keySet().iterator().next());
       
    40 		}
       
    41 	}
       
    42 	
       
    43 	public void put(K key, V value) {
       
    44 		V oldValue = map.put(key, value);
       
    45 		Map<K,V> unmodifiableMap = Collections.unmodifiableMap(map);
       
    46 		if(oldValue != null) {
       
    47 			for(Observer<K,V> o : observers) {
       
    48 				o.itemReplaced(unmodifiableMap, key, oldValue, value);
       
    49 			}
       
    50 		} else {
       
    51 			for(Observer<K,V> o : observers) {
       
    52 				o.itemAdded(unmodifiableMap, key, value);
       
    53 			}
       
    54 		}
       
    55 	}
       
    56 	
       
    57 	public void remove(K key) {
       
    58 		V oldValue = map.remove(key);
       
    59 		if(oldValue != null) {
       
    60 			Map<K,V> unmodifiableMap = Collections.unmodifiableMap(map);
       
    61 			for(Observer<K,V> o : observers) {
       
    62 				o.itemRemoved(unmodifiableMap, key, oldValue);
       
    63 			}
       
    64 		}
       
    65 	}
       
    66 
       
    67 	public static interface Observer<K,V> {
       
    68 		void itemAdded(Map<K,V> map, K key, V value);
       
    69 		void itemRemoved(Map<K,V> map, K key, V oldValue);
       
    70 		void itemReplaced(Map<K,V> map, K key, V oldValue, V newValue);
       
    71 	}
       
    72 }