001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2005, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.util;
018:
019: // J2SE dependencies
020: import java.util.AbstractSet;
021: import java.util.Iterator;
022: import java.util.NoSuchElementException;
023:
024: /**
025: * A mutable set containing only one element. This set can't contains null element.
026: *
027: * @since 2.1
028: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/metadata/src/main/java/org/geotools/util/Singleton.java $
029: * @version $Id: Singleton.java 24435 2007-02-16 03:32:10Z desruisseaux $
030: * @author Martin Desruisseaux
031: *
032: * @deprecated Not used anymore in the Geotools code base. If needed, use a small
033: * {@link java.util.ArrayList} instead.
034: */
035: public class Singleton extends AbstractSet {
036: /**
037: * The element, or {@code null} if this set is empty.
038: */
039: private Object element;
040:
041: /**
042: * Creates a initially empty singleton.
043: */
044: public Singleton() {
045: }
046:
047: /**
048: * Returns 1 if this singleton contains an element, or 0 otherwise.
049: */
050: public int size() {
051: return (element != null) ? 1 : 0;
052: }
053:
054: /**
055: * Returns {@code true} if this singleton contains no elements.<p>
056: */
057: public boolean isEmpty() {
058: return element == null;
059: }
060:
061: /**
062: * Returns the element in this singleton.
063: *
064: * @return The singleton element (never null).
065: * @throws NoSuchElementException if this singleton is empty.
066: */
067: public Object get() throws NoSuchElementException {
068: if (element == null) {
069: throw new NoSuchElementException();
070: }
071: return element;
072: }
073:
074: /**
075: * Returns {@code true} if this singleton contains the specified element.
076: */
077: public boolean contains(final Object object) {
078: return element != null && element.equals(object);
079: }
080:
081: /**
082: * Adds the specified element to this set if it is not already present.
083: * If this set already contains an other element, an exception is thrown.
084: *
085: * @throws NullPointerException if the argument is null.
086: * @throws IllegalArgumentException if this set already contains an other element.
087: */
088: public boolean add(final Object object) {
089: if (object == null) {
090: throw new NullPointerException();
091: }
092: if (element == null) {
093: element = object;
094: return true;
095: }
096: if (element.equals(object)) {
097: element = object;
098: return false;
099: }
100: throw new IllegalArgumentException();
101: }
102:
103: /**
104: * Removes the specified element from this singleton, if it is present.
105: */
106: public boolean remove(final Object object) {
107: if (element != null && element.equals(object)) {
108: element = null;
109: return true;
110: }
111: return false;
112: }
113:
114: /**
115: * Removes the element from this singleton.
116: */
117: public void clear() {
118: element = null;
119: }
120:
121: /**
122: * Returns an iterator over the element of this singleton.
123: */
124: public Iterator iterator() {
125: return new Iter();
126: }
127:
128: /**
129: * The iterator for this singleton.
130: */
131: private final class Iter implements Iterator {
132: /**
133: * {@code false} if this iterator is done.
134: */
135: private boolean hasNext;
136:
137: /**
138: * Construct a new iterator.
139: */
140: public Iter() {
141: hasNext = (element != null);
142: }
143:
144: /**
145: * Returns {@code true} if there is more element to return.
146: */
147: public boolean hasNext() {
148: return hasNext;
149: }
150:
151: /**
152: * Returns the next element.
153: */
154: public Object next() {
155: if (!hasNext) {
156: throw new NoSuchElementException();
157: }
158: hasNext = false;
159: return element;
160: }
161:
162: /**
163: * Remove the last element in this set.
164: */
165: public void remove() {
166: if (hasNext) {
167: throw new IllegalStateException();
168: }
169: element = null;
170: }
171: }
172: }
|