| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | package com.sun.fortress.useful; |
|---|
| 19 | |
|---|
| 20 | import java.util.Collection; |
|---|
| 21 | import java.util.Collections; |
|---|
| 22 | import java.util.Iterator; |
|---|
| 23 | import java.util.Map; |
|---|
| 24 | import java.util.Set; |
|---|
| 25 | import java.util.Map.Entry; |
|---|
| 26 | |
|---|
| 27 | import edu.rice.cs.plt.collect.ConsList; |
|---|
| 28 | import edu.rice.cs.plt.collect.ImmutableRelation; |
|---|
| 29 | import edu.rice.cs.plt.collect.IndexedRelation; |
|---|
| 30 | import edu.rice.cs.plt.collect.Relation; |
|---|
| 31 | import edu.rice.cs.plt.iter.IterUtil; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | public class UsefulPLT { |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | public static <K,V> Relation<K,V> relation(Map<? extends K, ? extends V> map) { |
|---|
| 45 | IndexedRelation<K,V> result = new IndexedRelation<K,V>(); |
|---|
| 46 | for( Map.Entry<? extends K, ? extends V> entry : map.entrySet() ) { |
|---|
| 47 | result.add(entry.getKey(), entry.getValue()); |
|---|
| 48 | } |
|---|
| 49 | return new ImmutableRelation<K,V>(result); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | public static <T> boolean consListContains(T item, ConsList<? extends T> list) { |
|---|
| 56 | Iterator<? extends T> iter = list.iterator(); |
|---|
| 57 | while( iter.hasNext() ) { |
|---|
| 58 | T t = iter.next(); |
|---|
| 59 | if( t.equals(item) ) |
|---|
| 60 | return true; |
|---|
| 61 | } |
|---|
| 62 | return false; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | public static <K,V> IMultiMap<K,V> shrinkMultiMap(IMultiMap<K,V> map) { |
|---|
| 66 | if( map.isEmpty() ) |
|---|
| 67 | return emptyMultiMap(); |
|---|
| 68 | else if( map.size() == 1 ) { |
|---|
| 69 | Map.Entry<K,Set<V>> r = IterUtil.first(map.entrySet()); |
|---|
| 70 | if( r.getValue().isEmpty() ) { |
|---|
| 71 | return singletonMultiMap(r.getKey(), Collections.<V>emptySet()); |
|---|
| 72 | } |
|---|
| 73 | else if( r.getValue().size() == 1 ) { |
|---|
| 74 | return singletonMultiMap(r.getKey(), IterUtil.first(r.getValue())); |
|---|
| 75 | } |
|---|
| 76 | else { |
|---|
| 77 | return singletonMultiMap(r.getKey(), r.getValue()); |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | else |
|---|
| 81 | return map; |
|---|
| 82 | } |
|---|
| 83 | public static <K,V> IMultiMap<K,V> emptyMultiMap() { |
|---|
| 84 | return (IMultiMap<K,V>)IMultiMap.EMPTY_MULTIMAP; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | public static <K,V> IMultiMap<K,V> singletonMultiMap(final K key, final V value) { |
|---|
| 88 | return singletonMultiMap(key, Collections.singleton(value)); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | public static <K,V> IMultiMap<K,V> singletonMultiMap(final K key, final Set<V> value) { |
|---|
| 92 | return new IMultiMap<K,V>() { |
|---|
| 93 | private final Map<K,Set<V>> singleton = Collections.singletonMap(key, value); |
|---|
| 94 | private <T> T error() { throw new IllegalStateException("Singleton IMultiMap is immutable."); } |
|---|
| 95 | public void clear() { singleton.clear(); } |
|---|
| 96 | public boolean containsKey(Object key) { return singleton.containsKey(key); } |
|---|
| 97 | public boolean containsValue(Object value) { return singleton.containsValue(value); } |
|---|
| 98 | public Set<Entry<K, Set<V>>> entrySet() { return singleton.entrySet(); } |
|---|
| 99 | public boolean equals(Object o) { return singleton.equals(o); } |
|---|
| 100 | public Set<V> get(Object key) { return singleton.get(key); } |
|---|
| 101 | public int hashCode() { return singleton.hashCode(); } |
|---|
| 102 | public boolean isEmpty() { return false; } |
|---|
| 103 | public Set<K> keySet() { return singleton.keySet(); } |
|---|
| 104 | public Set<V> put(K key, Set<V> value) { return singleton.put(key, value); } |
|---|
| 105 | public void putAll(Map<? extends K, ? extends Set<V>> t) { singleton.putAll(t); } |
|---|
| 106 | public Set<V> remove(Object key) { return singleton.remove(key); } |
|---|
| 107 | public int size() { return 1; } |
|---|
| 108 | public Collection<Set<V>> values() { return singleton.values(); } |
|---|
| 109 | public void addInverse(Map<V, K> m) { error(); } |
|---|
| 110 | public Set<V> putItem(K k, V v) { return error(); } |
|---|
| 111 | public Set<V> putItems(K k, Collection<V> vs) { return error(); } |
|---|
| 112 | public Set<V> removeItem(K k, V v) { return error(); } |
|---|
| 113 | public Set<V> putKey(K k) {return error();} |
|---|
| 114 | }; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | } |
|---|