All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.osgi.internal.serviceregistry.ShrinkableCollection Maven / Gradle / Ivy

There is a newer version: 1.9.22.1
Show newest version
/*******************************************************************************
 * Copyright (c) 2008, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.osgi.internal.serviceregistry;

import java.util.*;

/**
 * A Shrinkable Collection. This class provides a wrapper for a list of collections
 * that allows items to be removed from the wrapped collections (shrinking) but
 * does not allow items to be added to the wrapped collections.
 *
 * 

* The collections must act as sets in that each collection in the list * must not have two entries which are equal. * *

* All the optional Collection operations except * add and addAll are supported. Attempting to add to the * collection will result in an UnsupportedOperationException. * */ public class ShrinkableCollection implements Collection { private final Collection collection; private final List> list; public ShrinkableCollection(Collection c) { if (c == null) { throw new NullPointerException(); } List> empty = Collections.> emptyList(); list = empty; collection = c; } public ShrinkableCollection(Collection c1, Collection c2) { list = new ArrayList<>(2); list.add(c1); list.add(c2); collection = initComposite(list); } public ShrinkableCollection(List> l) { list = new ArrayList<>(l); collection = initComposite(list); } private static Collection initComposite(List> collections) { int size = 0; for (Collection c : collections) { assert verifyNoDuplicates(c); size += c.size(); } Collection result = new ArrayList<>(size); for (Collection c : collections) { for (E e : c) { if (!result.contains(e)) { result.add(e); } } } return result; } private static boolean verifyNoDuplicates(Collection c) { for (E e : c) { int count = 0; for (E f : c) { if (e == null) { if (f == null) { count++; } } else { if (e.equals(f)) { count++; } } } if (count != 1) { return false; } } return true; } @Override public boolean add(E e) { throw new UnsupportedOperationException(); } @Override public boolean addAll(Collection c) { throw new UnsupportedOperationException(); } @Override public void clear() { collection.clear(); for (Collection c : list) { c.clear(); } } @Override public boolean contains(Object o) { return collection.contains(o); } @Override public boolean containsAll(Collection c) { return collection.containsAll(c); } @Override public boolean isEmpty() { return collection.isEmpty(); } @Override public Iterator iterator() { @SuppressWarnings("unchecked") final Iterator iter = (Iterator) collection.iterator(); final List> collections = list; if (collections.isEmpty()) { return iter; } return new Iterator() { private E last; @Override public boolean hasNext() { return iter.hasNext(); } @Override public E next() { last = iter.next(); return last; } @Override public void remove() { iter.remove(); for (Collection c : collections) { c.remove(last); } } }; } @Override public boolean remove(Object o) { final boolean result = collection.remove(o); if (result) { for (Collection c : list) { c.remove(o); } } return result; } @Override public boolean removeAll(Collection c) { final boolean result = collection.removeAll(c); if (result) { for (Collection cc : list) { cc.removeAll(c); } } return result; } @Override public boolean retainAll(Collection c) { final boolean result = collection.retainAll(c); if (result) { for (Collection cc : list) { cc.retainAll(c); } } return result; } @Override public int size() { return collection.size(); } @Override public Object[] toArray() { return collection.toArray(); } @Override public T[] toArray(T[] var0) { return collection.toArray(var0); } @Override public String toString() { return collection.toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy