MATSIM
IdentifiableArrayMap.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2012 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.core.utils.collections;
21 
22 import org.matsim.api.core.v01.Id;
24 
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.Iterator;
28 import java.util.LinkedHashSet;
29 import java.util.Map;
30 import java.util.NoSuchElementException;
31 import java.util.Set;
32 
41 public class IdentifiableArrayMap<S, T extends Identifiable<S>> implements Map<Id<S>, T> {
42 
43  private final static Identifiable[] EMPTY = new Identifiable[0];
44 
45  private T[] data = (T[]) EMPTY;
46 
47  @Override
48  public int size() {
49  return this.data.length;
50  }
51 
52  @Override
53  public boolean isEmpty() {
54  return this.data.length == 0;
55  }
56 
57  @Override
58  public boolean containsKey(final Object key) {
59  for (Identifiable<S> o : this.data) {
60  if (o.getId().equals(key)) {
61  return true;
62  }
63  }
64  return false;
65  }
66 
67  @Override
68  public boolean containsValue(final Object value) {
69  for (Identifiable<S> o : this.data) {
70  if (o.equals(value)) {
71  return true;
72  }
73  }
74  return false;
75  }
76 
77  @Override
78  public T get(final Object key) {
79  for (Identifiable<S> o : this.data) {
80  if (o.getId().equals(key)) {
81  return (T) o;
82  }
83  }
84  return null;
85  }
86 
87  public T put(final T value) {
88  return put(value.getId(), value);
89  }
90 
91  @Override
92  public T put(final Id<S> key, final T value) {
93  for (int i = 0; i < this.data.length; i++) {
94  T old = this.data[i];
95  if (old.getId().equals(key)) {
96  this.data[i] = value;
97  return old;
98  }
99  }
100  this.data = Arrays.copyOf(this.data, this.data.length + 1);
101  this.data[this.data.length - 1] = value;
102  return null;
103  }
104 
105  @Override
106  public T remove(final Object key) {
107  for (int i = 0; i < this.data.length; i++) {
108  T old = this.data[i];
109  if (old.getId().equals(key)) {
110 
111  Identifiable<S>[] tmp = new Identifiable[this.data.length - 1];
112  if (i > 0) {
113  System.arraycopy(this.data, 0, tmp, 0, i);
114  }
115  if (i + 1 < this.data.length) {
116  System.arraycopy(this.data, i + 1, tmp, i, this.data.length - 1 - i);
117  }
118  this.data = (T[]) tmp;
119 
120  return old;
121  }
122  }
123  return null;
124  }
125 
126  @Override
127  public void putAll(final Map<? extends Id<S>, ? extends T> m) {
128  for (T t : m.values()) {
129  put(t.getId(), t);
130  }
131  }
132 
133  @Override
134  public void clear() {
135  this.data = (T[]) new Identifiable[0];
136  }
137 
138  @Override
139  public Set<Id<S>> keySet() {
140  Set<Id<S>> ids = new LinkedHashSet<Id<S>>();
141  for (Identifiable<S> o : this.data) {
142  ids.add(o.getId());
143  }
144  return ids;
145  }
146 
147  @Override
148  public Collection<T> values() {
149  return new ArrayCollection<T>(this.data);
150  }
151 
152  @Override
153  public Set<java.util.Map.Entry<Id<S>, T>> entrySet() {
154  Set<Map.Entry<Id<S>, T>> entries = new LinkedHashSet<Map.Entry<Id<S>, T>>();
155  for (Identifiable<S> o : this.data) {
156  entries.add(new Entry<S, T>((T) o));
157  }
158  return entries;
159  }
160 
161  private static class Entry<S, T extends Identifiable<S>> implements Map.Entry<Id<S>, T> {
162 
163  private final T t;
164 
165  public Entry(final T t) {
166  this.t = t;
167  }
168 
169  @Override
170  public Id<S> getKey() {
171  return this.t.getId();
172  }
173 
174  @Override
175  public T getValue() {
176  return this.t;
177  }
178 
179  @Override
180  public T setValue(final T value) {
181  throw new UnsupportedOperationException();
182  }
183 
184  }
185 
193  private static class ArrayCollection<A> implements Collection<A> {
194 
195  private final A[] data;
196 
197  public ArrayCollection(final A[] data) {
198  this.data = data;
199  }
200 
201  @Override
202  public int size() {
203  return this.data.length;
204  }
205 
206  @Override
207  public boolean isEmpty() {
208  return this.data.length == 0;
209  }
210 
211  @Override
212  public boolean contains(Object o) {
213  for (A t : this.data) {
214  if (t == null) {
215  return o == null;
216  }
217  if (t.equals(o)) {
218  return true;
219  }
220  }
221  return false;
222  }
223 
224  @Override
225  public Iterator<A> iterator() {
226  return new ArrayIterator<A>(data);
227  }
228 
229  @Override
230  public Object[] toArray() {
231  return this.data.clone();
232  }
233 
234  @Override
235  public <TT> TT[] toArray(final TT[] a) {
236  TT[] dest = a;
237  if (a.length != this.data.length) {
238  dest = Arrays.copyOf(a, this.data.length);
239  }
240  System.arraycopy(this.data, 0, dest, 0, this.data.length);
241  return dest;
242  }
243 
244  @Override
245  public boolean add(A e) {
246  throw new UnsupportedOperationException();
247  }
248 
249  @Override
250  public boolean remove(Object o) {
251  throw new UnsupportedOperationException();
252  }
253 
254  @Override
255  public boolean containsAll(Collection<?> c) {
256  for (Object o : c) {
257  boolean isPartOf = this.contains(o);
258  if (!isPartOf) {
259  return false;
260  }
261  }
262  return true;
263  }
264 
265  @Override
266  public boolean addAll(Collection<? extends A> c) {
267  throw new UnsupportedOperationException();
268  }
269 
270  @Override
271  public boolean removeAll(Collection<?> c) {
272  throw new UnsupportedOperationException();
273  }
274 
275  @Override
276  public boolean retainAll(Collection<?> c) {
277  throw new UnsupportedOperationException();
278  }
279 
280  @Override
281  public void clear() {
282  throw new UnsupportedOperationException();
283  }
284  }
285 
286  private static class ArrayIterator<B> implements Iterator<B> {
287 
288  private final B[] data;
289  private int pos = 0;
290 
291  public ArrayIterator(final B[] data) {
292  this.data = data;
293  }
294 
295  @Override
296  public boolean hasNext() {
297  return this.pos < data.length;
298  }
299 
300  @Override
301  public B next() {
302  if (this.pos < data.length) {
303  B t = data[this.pos];
304  this.pos++;
305  return t;
306  }
307  throw new NoSuchElementException();
308  }
309 
310  @Override
311  public void remove() {
312  throw new UnsupportedOperationException();
313  }
314 
315  }
316 }
Entry(final T t)
Id< S > getKey()
final T t
T setValue(final T value)
void putAll(final Map<? extends Id< S >, ? extends T > m)
T getValue()
Set< java.util.Map.Entry< Id< S >, T > > entrySet()