| /******************************************************************************* |
| * Copyright (c) 2004-2008 Andras Schmidt, Andras Balogh, Istvan Rath and Daniel Varro |
| * 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: |
| * Andras Schmidt, Andras Balogh, Istvan Rath - initial API and implementation |
| *******************************************************************************/ |
| |
| package org.eclipse.viatra2.core.simple; |
| |
| import java.util.Collection; |
| import java.util.Iterator; |
| import java.util.Set; |
| import java.util.TreeSet; |
| |
| /** |
| * @author Andras Schmidt |
| * @param <E> |
| * |
| */ |
| public class SmallTreeSet<E> implements Set<E> { |
| TreeSet<E> set = null; |
| |
| public int size() { |
| if (set == null) { |
| return 0; |
| } else |
| return set.size(); |
| } |
| |
| public boolean isEmpty() { |
| if (set == null) |
| return true; |
| else |
| return set.isEmpty(); |
| |
| } |
| |
| public boolean contains(Object arg0) { |
| if (set == null) |
| return false; |
| else |
| return set.contains(arg0); |
| } |
| |
| class NullIterator<F> implements Iterator<F> { |
| public boolean hasNext() { |
| return false; |
| } |
| |
| public F next() { |
| return null; |
| } |
| |
| public void remove() { |
| } |
| } |
| |
| public Iterator<E> iterator() { |
| if (set == null) |
| return new NullIterator<E>(); |
| else |
| return set.iterator(); |
| } |
| |
| public Object[] toArray() { |
| if (set == null) |
| return new Object[0]; |
| else |
| return set.toArray(); |
| } |
| |
| public <T> T[] toArray(T[] arg0) { |
| if (set == null) |
| return arg0; |
| else |
| return set.toArray(arg0); |
| } |
| |
| public boolean add(E arg0) { |
| if (set == null) { |
| set = new TreeSet<E>(); |
| return set.add(arg0); |
| } else |
| return set.add(arg0); |
| } |
| |
| public boolean remove(Object arg0) { |
| if (set == null) |
| return false; |
| else { |
| boolean ret = set.remove(arg0); |
| if (set.size() == 0) |
| set = null; |
| return ret; |
| } |
| } |
| |
| public boolean containsAll(Collection arg0) { |
| if (set == null) { |
| return arg0.size() == 0; |
| } else { |
| return set.containsAll(arg0); |
| } |
| } |
| |
| public boolean addAll(Collection<? extends E> arg0) { |
| if (set == null) { |
| if (arg0.size() != 0) { |
| set = new TreeSet<E>(); |
| return set.addAll(arg0); |
| } else |
| return false; |
| } else { |
| return set.addAll(arg0); |
| } |
| } |
| |
| public boolean retainAll(Collection arg0) { |
| throw new UnsupportedOperationException(); |
| } |
| |
| public boolean removeAll(Collection arg0) { |
| if (set == null) { |
| return false; |
| } else { |
| boolean ret = set.removeAll(arg0); |
| if (set.size() == 0) |
| set = null; |
| return ret; |
| } |
| } |
| |
| public void clear() { |
| if (set == null) |
| return; |
| else |
| set.clear(); |
| } |
| |
| public boolean equals(Object o) { |
| if (o instanceof Set) { |
| Set ob = (Set) o; |
| if (set == null) { |
| return ob.size() == 0; |
| } else |
| return set.equals(ob); |
| } else { |
| return false; |
| } |
| } |
| } |