View Javadoc

1   package cz.cuni.amis.utils.collections;
2   
3   import java.util.Collection;
4   import java.util.Iterator;
5   
6   import cz.cuni.amis.utils.maps.AbstractLazyMap;
7   
8   /**
9    * Read-only Collection used for lazy implementation of Map.values().
10   * The items in this collection are backed by the map and Map.get() is used if an object is required.
11   * @author srlok
12   *
13   */
14  public class LazyMapValuesCollection<VALUE,KEY> implements Collection<VALUE> {
15  
16  	private AbstractLazyMap<KEY,VALUE> map;
17  	
18  	public AbstractLazyMap<KEY,VALUE> getBaseMap()
19  	{
20  		return map;
21  	}
22  	
23  	public LazyMapValuesCollection(AbstractLazyMap<KEY,VALUE> sourceMap)
24  	{
25  		map = sourceMap;
26  	}
27  	
28  	@Override
29  	public boolean add(VALUE e) {
30  		return false;
31  	}
32  
33  	@Override
34  	public boolean addAll(Collection<? extends VALUE> c) {		
35  		return false;
36  	}
37  
38  	@Override
39  	public void clear() {
40  	}
41  
42  	@Override
43  	/**
44  	 * Warning! may have - O(N) complexity!
45  	 */
46  	public boolean contains(Object o) {
47  		for ( Object key : map.keySet() )
48  		{
49  			if ( map.get(key).equals(o))
50  			{
51  				return true;
52  			}
53  		}
54  		return false;
55  	}
56  
57  	@Override
58  	/**
59  	 * Warning! O(N)*M complexity!
60  	 */
61  	public boolean containsAll(Collection<?> c) {
62  		for ( Object o : c)
63  		{
64  			if ( !contains(o))
65  			{
66  				return false;
67  			}
68  		}
69  		return true;
70  	}
71  
72  	@Override
73  	public boolean isEmpty() {
74  		return map.isEmpty();
75  	}
76  
77  	@Override
78  	public Iterator<VALUE> iterator() {
79  		return new Iterator<VALUE>()
80  		{
81  			Iterator<KEY> it = null;
82  			KEY value = null;
83  			
84  			{
85  				it = getBaseMap().keySet().iterator();
86  			}
87  			
88  			@Override
89  			public boolean hasNext() {
90  				return it.hasNext();
91  			}
92  
93  			@Override
94  			public VALUE next() {
95  				value = it.next();
96  				return getBaseMap().get(value);
97  			}
98  
99  			@Override
100 			public void remove() {
101 				getBaseMap().remove(value);
102 				
103 			}
104 			
105 		};
106 	}
107 
108 	@Override
109 	public boolean remove(Object o) {
110 		return false;
111 	}
112 
113 	@Override
114 	public boolean removeAll(Collection<?> c) {
115 		return false;
116 	}
117 
118 	@Override
119 	public boolean retainAll(Collection<?> c) {
120 		return false;
121 	}
122 
123 	@Override
124 	public int size() {
125 		return map.size();
126 	}
127 
128 	@Override
129 	public Object[] toArray() {
130 		Object[] arr = new Object[ map.size()];
131 		int i = 0;
132 		for ( Object key : map.keySet() )
133 		{
134 			arr[i] = map.get(key);
135 		}
136 		return arr;
137 	}
138 
139 	@Override
140 	public <T> T[] toArray(T[] a) {
141 		Object[] arr = new Object[ map.size()];
142 		int i = 0;
143 		for ( Object key : map.keySet() )
144 		{
145 			arr[i] = map.get(key);
146 		}
147 		return (T[])arr;
148 	}
149 
150 }