]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/CollectionAdapter.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / CollectionAdapter.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.datastructures;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.Iterator;\r
16 \r
17 public class CollectionAdapter<Domain, Range, C extends Collection<Domain>> \r
18 implements Collection<Range> {\r
19 \r
20     protected Converter<Domain, Range> converter;\r
21     protected C                        collection;  \r
22       \r
23     public CollectionAdapter(Converter<Domain, Range> converter, C collection) {\r
24         this.converter = converter;\r
25         this.collection = collection;\r
26     }\r
27     @Override\r
28     public boolean add(Range e) {\r
29         return collection.add(converter.convertFrom(e));\r
30     }\r
31     @SuppressWarnings("unchecked")\r
32     @Override\r
33     public boolean addAll(Collection<? extends Range> c) {\r
34         return collection.addAll(new CollectionAdapter<Range, Domain, Collection<Range>>(inverseConverter(converter), (Collection<Range>)c));\r
35     }\r
36     @Override\r
37     public void clear() {\r
38         collection.clear();\r
39     }\r
40     @SuppressWarnings("unchecked")\r
41     @Override\r
42     public boolean contains(Object o) {\r
43         return collection.contains(converter.convertFrom((Range)o));\r
44     }\r
45     @Override\r
46     public boolean containsAll(Collection<?> c) {\r
47         for(Object o : c)\r
48             if(!contains(o))\r
49                 return false;\r
50         return true;\r
51     }\r
52     @Override\r
53     public boolean isEmpty() {\r
54         return collection.isEmpty();\r
55     }\r
56     @Override\r
57     public Iterator<Range> iterator() {\r
58         return new IteratorAdapter<Domain, Range, Iterator<Domain>>(converter, collection.iterator());                \r
59     }\r
60     @SuppressWarnings("unchecked")\r
61     @Override\r
62     public boolean remove(Object o) {\r
63         return collection.remove(converter.convertFrom((Range)o));\r
64     }\r
65     @Override\r
66     public boolean removeAll(Collection<?> c) {\r
67         boolean removed = false;\r
68         for(Object o : c)\r
69             removed |= remove(o);\r
70         return removed;\r
71     }\r
72     @SuppressWarnings("unchecked")\r
73     @Override\r
74     public boolean retainAll(Collection<?> c) {\r
75         // FIXME\r
76         return collection.retainAll(new CollectionAdapter<Range, Domain, Collection<Range>>(inverseConverter(converter), (Collection<Range>)c));\r
77     }\r
78     @Override\r
79     public int size() {\r
80         return collection.size();\r
81     }\r
82     @Override\r
83     public Object[] toArray() {\r
84         Object[] ret = new Object[collection.size()];\r
85         int i=0;\r
86         for(Domain d : collection)\r
87             ret[i++] = converter.convertTo(d);\r
88         return ret;\r
89     }\r
90     @Override\r
91     public <T> T[] toArray(T[] a) {\r
92         throw new UnsupportedOperationException();\r
93     }\r
94     \r
95     public static <D,R> Converter<R,D> inverseConverter(final Converter<D,R> converter) {\r
96         return new Converter<R,D>() {\r
97 \r
98             @Override\r
99             public R convertFrom(D rangeElement) {\r
100                 return converter.convertTo(rangeElement);\r
101             }\r
102 \r
103             @Override\r
104             public D convertTo(R domainElement) {\r
105                 return converter.convertFrom(domainElement);\r
106             }\r
107             \r
108         };\r
109     }\r
110     \r
111 }\r