blob: 9ab7cc76a764eb13f9aa74a48dc259a7f98205f8 [file] [log] [blame]
/*******************************************************************************
* 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 <T>
*
*/
public class SmallTreeSet2<T> implements Set<T> {
TreeSet<T> set = null;
T o = null;
public int size() {
if (set == null) {
return o == null ? 0 : 1;
} else
return set.size();
}
public boolean isEmpty() {
if (set == null)
return o == null;
else
return set.isEmpty();
}
public boolean contains(Object arg0) {
if (set == null) {
if (o != null)
return arg0.equals(o);
else
return false;
} else
return set.contains(arg0);
}
/*
* class OneIterator<E> implements Iterator { E o; OneIterator(E o) {
* this.o=o; } public boolean hasNext() { return o!=null;} public E next() {
* E ret=o; o=null; return ret;} public void remove() {} }
*/
public Iterator<T> iterator() {
if (set == null) {
return new OneIterator<T>(o);
} else
return set.iterator();
}
public Object[] toArray() {
if (set == null) {
if (o == null) {
return new Object[0];
} else {
Object[] ret = new Object[1];
ret[0] = o;
return ret;
}
} else
return set.toArray();
}
public <K> K[] toArray(K[] arg0) {
/*
* if(set==null) { if(o!=null) { Object x=o; arg0[0]=(K)x; } return
* arg0; } else return set.toArray(arg0);
*/
return null;
}
public boolean add(T arg0) {
if (set == null) {
if (o == null) {
o = arg0;
return true;
} else {
if (o.equals(arg0)) {
return false;
} else {
set = new TreeSet<T>();
set.add(o);
o = null;
return set.add(arg0);
}
}
} else
return set.add(arg0);
}
public boolean remove(Object arg0) {
if (set == null) {
if (o != null && o.equals(arg0)) {
o = null;
return true;
} else
return false;
} else {
boolean ret = set.remove(arg0);
if (set.size() == 0) {
set = null;
} else if (set.size() == 1) {
o = set.iterator().next();
set = null;
}
return ret;
}
}
public boolean containsAll(Collection arg0) {
if (set == null) {
if (o != null) {
if (arg0.size() == 1) {
return arg0.iterator().next().equals(o);
} else
return false;
} else {
return arg0.size() == 0;
}
} else {
return set.containsAll(arg0);
}
}
public boolean addAll(Collection<? extends T> arg0) {
if (set == null) {
if (arg0.size() > 1) {
set = new TreeSet<T>();
return set.addAll(arg0);
} else if (arg0.size() == 1) {
o = arg0.iterator().next();
return true;
} else
return false;
} else {
return set.addAll(arg0);
}
}
public boolean retainAll(Collection arg0) {
throw new UnsupportedOperationException();
}
public boolean removeAll(Collection arg0) {
throw new UnsupportedOperationException();
/*
* 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) {
o = null;
return;
} else
set.clear();
}
public boolean equals(Object o) {
if (o == null) {
return set == null;
} if (o instanceof Set<?>) {
Set<?> ob = (Set<?>) o;
if (set == null) {
return ob.size() == 0;
} else if (set == null) {
if (ob.size() == 1)
return ob.iterator().next().equals(o);
else
return false;
} else
return set.equals(ob);
} else {
return false;
}
}
}