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