root/trunk/ProjectFortress/src/com/sun/fortress/useful/UsefulPLT.java @ 3134

Revision 3134, 5.0 KB (checked in by dr2chase, 12 months ago)

[useful] factored some 3rd-party dependent code into separate class

Line 
1/*******************************************************************************
2    Copyright 2008 Sun Microsystems, Inc.,
3    4150 Network Circle, Santa Clara, California 95054, U.S.A.
4    All rights reserved.
5
6    U.S. Government Rights - Commercial software.
7    Government users are subject to the Sun Microsystems, Inc. standard
8    license agreement and applicable provisions of the FAR and its supplements.
9
10    Use is subject to license terms.
11
12    This distribution may include materials developed by third parties.
13
14    Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
15    trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
16 ******************************************************************************/
17
18package com.sun.fortress.useful;
19
20import java.util.Collection;
21import java.util.Collections;
22import java.util.Iterator;
23import java.util.Map;
24import java.util.Set;
25import java.util.Map.Entry;
26
27import edu.rice.cs.plt.collect.ConsList;
28import edu.rice.cs.plt.collect.ImmutableRelation;
29import edu.rice.cs.plt.collect.IndexedRelation;
30import edu.rice.cs.plt.collect.Relation;
31import edu.rice.cs.plt.iter.IterUtil;
32
33/**
34 * It is helpful for Useful to not depend on external libraries.
35 * Those useful methods that do, were moved here.
36 * @author dr2chase
37 */
38
39public class UsefulPLT {
40    /**
41     * Returns an immutable {@code Relation} with the same contents as the given
42     * {@code Map}.
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     * Does the given ConsList contain the given element?
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, Set<V> vs) { return error(); }
112                public Set<V> removeItem(K k, V v) { return error(); }
113                };
114        }
115       
116
117
118}
Note: See TracBrowser for help on using the browser.