X-Git-Url: https://gerrit.simantics.org/r/gitweb?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=6b9193fde792ecbfd5f1e0e7ff1a1a268403ea1c;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=8fadea4336c0fd24b54a2c41261855df259fce24;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git 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 index 8fadea433..6b9193fde 100644 --- 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 @@ -1,111 +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); - } - - }; - } - -} +/******************************************************************************* + * 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); + } + + }; + } + +}