EmptySet.java
/*******************************************************************************
* Copyright (c) 2013 Stephen F. Siegel, University of Delaware.
*
* This file is part of SARL.
*
* SARL is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* SARL is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with SARL. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package edu.udel.cis.vsl.sarl.util;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
/** An immutable, empty set. */
public class EmptySet<E> implements Set<E> {
@Override
public int size() {
return 0;
}
@Override
public boolean isEmpty() {
return true;
}
@Override
public boolean contains(Object o) {
return false;
}
@Override
public Iterator<E> iterator() {
return new EmptyIterator<E>();
}
@Override
public Object[] toArray() {
return new Object[0];
}
@Override
public <T> T[] toArray(T[] a) {
if (a.length == 0)
return a;
return Arrays.copyOf(a, 0);
}
@Override
public boolean add(E e) {
throw new UnsupportedOperationException("This is an immutable set");
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException("This is an immutable set");
}
@Override
public boolean containsAll(Collection<?> c) {
return c.isEmpty();
}
@Override
public boolean addAll(Collection<? extends E> c) {
throw new UnsupportedOperationException("This is an immutable set");
}
@Override
public boolean retainAll(Collection<?> c) {
throw new UnsupportedOperationException("This is an immutable set");
}
@Override
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException("This is an immutable set");
}
@Override
public void clear() {
throw new UnsupportedOperationException("This is an immutable set");
}
}