--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.datastructures;\r
+\r
+import java.util.Collection;\r
+import java.util.Iterator;\r
+\r
+public class CollectionAdapter<Domain, Range, C extends Collection<Domain>> \r
+implements Collection<Range> {\r
+\r
+ protected Converter<Domain, Range> converter;\r
+ protected C collection; \r
+ \r
+ public CollectionAdapter(Converter<Domain, Range> converter, C collection) {\r
+ this.converter = converter;\r
+ this.collection = collection;\r
+ }\r
+ @Override\r
+ public boolean add(Range e) {\r
+ return collection.add(converter.convertFrom(e));\r
+ }\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public boolean addAll(Collection<? extends Range> c) {\r
+ return collection.addAll(new CollectionAdapter<Range, Domain, Collection<Range>>(inverseConverter(converter), (Collection<Range>)c));\r
+ }\r
+ @Override\r
+ public void clear() {\r
+ collection.clear();\r
+ }\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public boolean contains(Object o) {\r
+ return collection.contains(converter.convertFrom((Range)o));\r
+ }\r
+ @Override\r
+ public boolean containsAll(Collection<?> c) {\r
+ for(Object o : c)\r
+ if(!contains(o))\r
+ return false;\r
+ return true;\r
+ }\r
+ @Override\r
+ public boolean isEmpty() {\r
+ return collection.isEmpty();\r
+ }\r
+ @Override\r
+ public Iterator<Range> iterator() {\r
+ return new IteratorAdapter<Domain, Range, Iterator<Domain>>(converter, collection.iterator()); \r
+ }\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public boolean remove(Object o) {\r
+ return collection.remove(converter.convertFrom((Range)o));\r
+ }\r
+ @Override\r
+ public boolean removeAll(Collection<?> c) {\r
+ boolean removed = false;\r
+ for(Object o : c)\r
+ removed |= remove(o);\r
+ return removed;\r
+ }\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public boolean retainAll(Collection<?> c) {\r
+ // FIXME\r
+ return collection.retainAll(new CollectionAdapter<Range, Domain, Collection<Range>>(inverseConverter(converter), (Collection<Range>)c));\r
+ }\r
+ @Override\r
+ public int size() {\r
+ return collection.size();\r
+ }\r
+ @Override\r
+ public Object[] toArray() {\r
+ Object[] ret = new Object[collection.size()];\r
+ int i=0;\r
+ for(Domain d : collection)\r
+ ret[i++] = converter.convertTo(d);\r
+ return ret;\r
+ }\r
+ @Override\r
+ public <T> T[] toArray(T[] a) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+ \r
+ public static <D,R> Converter<R,D> inverseConverter(final Converter<D,R> converter) {\r
+ return new Converter<R,D>() {\r
+\r
+ @Override\r
+ public R convertFrom(D rangeElement) {\r
+ return converter.convertTo(rangeElement);\r
+ }\r
+\r
+ @Override\r
+ public D convertTo(R domainElement) {\r
+ return converter.convertFrom(domainElement);\r
+ }\r
+ \r
+ };\r
+ }\r
+ \r
+}\r