X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.utils.datastructures%2Fsrc%2Forg%2Fsimantics%2Futils%2Fdatastructures%2FCollectionAdapter.java;fp=bundles%2Forg.simantics.utils.datastructures%2Fsrc%2Forg%2Fsimantics%2Futils%2Fdatastructures%2FCollectionAdapter.java;h=8fadea4336c0fd24b54a2c41261855df259fce24;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/CollectionAdapter.java b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/CollectionAdapter.java new file mode 100644 index 000000000..8fadea433 --- /dev/null +++ b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/CollectionAdapter.java @@ -0,0 +1,111 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.utils.datastructures; + +import java.util.Collection; +import java.util.Iterator; + +public class CollectionAdapter> +implements Collection { + + protected Converter converter; + protected C collection; + + public CollectionAdapter(Converter converter, C collection) { + this.converter = converter; + this.collection = collection; + } + @Override + public boolean add(Range e) { + return collection.add(converter.convertFrom(e)); + } + @SuppressWarnings("unchecked") + @Override + public boolean addAll(Collection c) { + return collection.addAll(new CollectionAdapter>(inverseConverter(converter), (Collection)c)); + } + @Override + public void clear() { + collection.clear(); + } + @SuppressWarnings("unchecked") + @Override + public boolean contains(Object o) { + return collection.contains(converter.convertFrom((Range)o)); + } + @Override + public boolean containsAll(Collection c) { + for(Object o : c) + if(!contains(o)) + return false; + return true; + } + @Override + public boolean isEmpty() { + return collection.isEmpty(); + } + @Override + public Iterator iterator() { + return new IteratorAdapter>(converter, collection.iterator()); + } + @SuppressWarnings("unchecked") + @Override + public boolean remove(Object o) { + return collection.remove(converter.convertFrom((Range)o)); + } + @Override + public boolean removeAll(Collection c) { + boolean removed = false; + for(Object o : c) + removed |= remove(o); + return removed; + } + @SuppressWarnings("unchecked") + @Override + public boolean retainAll(Collection c) { + // FIXME + return collection.retainAll(new CollectionAdapter>(inverseConverter(converter), (Collection)c)); + } + @Override + public int size() { + return collection.size(); + } + @Override + public Object[] toArray() { + Object[] ret = new Object[collection.size()]; + int i=0; + for(Domain d : collection) + ret[i++] = converter.convertTo(d); + return ret; + } + @Override + public T[] toArray(T[] a) { + throw new UnsupportedOperationException(); + } + + public static Converter inverseConverter(final Converter converter) { + return new Converter() { + + @Override + public R convertFrom(D rangeElement) { + return converter.convertTo(rangeElement); + } + + @Override + public D convertTo(R domainElement) { + return converter.convertFrom(domainElement); + } + + }; + } + +}