📄 singleton.java
字号:
package org.jutil.java.collections;import java.util.Set;import java.util.AbstractList;import java.util.Iterator;import java.util.Collection;import java.util.NoSuchElementException;import java.lang.reflect.Array;/** * @path $Source: /cvsroot/org-jutil/jutil.org/src/org/jutil/java/collections/Singleton.java,v $ * @version $Revision: 1.7 $ * @date $Date: 2002/07/21 17:56:59 $ * @state $State: Exp $ * @author Jan Dockx * @release $Name: $ */final public class Singleton extends AbstractList implements Set { /* The revision of this class */ public final static String CVS_REVISION ="$Revision: 1.7 $"; /** * The only element contained in this Singleton. */ private Object _onlyElement; /** * Initialize a new Singleton containing the given element. * * @param onlyElement * The only element of this Singleton. */ /*@ @ public behavior @ @ post getOnlyElement() == onlyElement; @*/ public Singleton(Object onlyElement) { _onlyElement = onlyElement; } /** * Return the only element in this Singleton. */ public Object getOnlyElement() { return _onlyElement; } /** * See superclass */ /*@ @ also public behavior @ @ post \result == 1; @*/ public int size() { return 1; } /** * See superclass */ /*@ @ also public behavior @ @ post \result == false; @*/ public boolean isEmpty() { return false; } /** * See superclass */ /*@ @ also public behavior @ @ post \result == @ ((getOnlyElement() == null) && (o == null)) || @ getOnlyElement().equals(o); @*/ public boolean contains(Object o) { return ((_onlyElement == null) && (o == null)) || _onlyElement.equals(o); } /** * See superclass */ /*@ @ also public behavior @ @ post \result instanceof SingletonIterator; @ post \result != null; @*/ public Iterator iterator() { return new SingletonIterator(); } public class SingletonIterator implements Iterator { /*@ @ public model boolean onlyElementNotReturnedYet; @ //FIXME initially onlyElementNotReturnedYet == true; @ depends onlyElementNotReturnedYet <- $calledOnce; @ represents onlyElementNotReturnedYet <- ! $calledOnce; @*/ /** * A boolean indicating whether or not the only element in an Singleton * has been visited. */ private boolean $calledOnce = false; /** * See superclass */ /*@ @ also public behavior @ @ post \result == onlyElementNotReturnedYet; @*/ public boolean hasNext() { return (! $calledOnce); } /** * See superclass */ /*@ @ also public behavior @ @ post \result == getOnlyElement(); @ post onlyElementNotReturnedYet == false; @ signals (NoSuchElementException) (\old(! onlyElementNotReturnedYet)); @*/ public Object next() { if ($calledOnce) { throw new NoSuchElementException(); } $calledOnce = true; return _onlyElement; } /** * See superclass */ /*@ @ also public behavior @ @ post false; @ signals (UnsupportedOperationException) true; @*/ public void remove() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } } /** * See superclass */ /*@ @ also public behavior @ @ post \result.length == 1; @ post \result[0] == getOnlyElement(); @*/ public Object[] toArray() { Object[] result = {_onlyElement}; return result; } /** * See superclass */ /*@ @ also public behavior @ @ post \result.getClass().getComponentType() == a.getClass().getComponentType(); @ post \result[0] == getOnlyElement(); @ post (a.length >= 1) ==> (\result == a); @ post (a.length < 1) ==> (\result.length == 1); @ signals (NullPointerException) a == null; @ signals (ArrayStoreException) @ (getOnlyElement() != null) && @ (! a.getClass().isInstance(getOnlyElement())); @*/ public Object[] toArray(Object[] a) { if (a.length < 1) { // NullPointerException, right were we want it a = (Object[]) Array.newInstance(a.getClass().getComponentType(), 1); } a[0] = _onlyElement; // ArrayStoreException, right were we want it return a; } /** * See superclass */ /*@ @ also public behavior @ @ pre index == 1; @ @ post \result == getOnlyElement(); @*/ public Object get(int index) { return _onlyElement; } // Modification Operations /** * See superclass */ /*@ @ also public behavior @ @ post false; @ signals (UnsupportedOperationException) true; @*/ public boolean add(Object o) throws UnsupportedOperationException { throw new UnsupportedOperationException(); } /** * See superclass */ /*@ @ also public behavior @ @ post false; @ signals (UnsupportedOperationException) true; @*/ public boolean remove(Object o) throws UnsupportedOperationException { throw new UnsupportedOperationException(); } // Bulk Operations /** * See superclass */ /*@ @ also public behavior @ @ post false; @ signals (UnsupportedOperationException) true; @*/ public boolean addAll(Collection c) throws UnsupportedOperationException { throw new UnsupportedOperationException(); } /** * See superclass */ /*@ @ also public behavior @ @ post false; @ signals (UnsupportedOperationException) true; @*/ public boolean retainAll(Collection c) throws UnsupportedOperationException { throw new UnsupportedOperationException(); } /** * See superclass */ /*@ @ also public behavior @ @ post false; @ signals (UnsupportedOperationException) true; @*/ public boolean removeAll(Collection c) throws UnsupportedOperationException { throw new UnsupportedOperationException(); } /** * See superclass */ /*@ @ also public behavior @ @ post false; @ signals (UnsupportedOperationException) true; @*/ public void clear() throws UnsupportedOperationException { throw new UnsupportedOperationException(); } /** * See superclass */ /*@ @ also public behavior @ @ post false; @ signals (UnsupportedOperationException) true; @*/ public Object set(int index, Object object) throws UnsupportedOperationException { throw new UnsupportedOperationException(); }}/*<copyright>Copyright (C) 1997-2001. This software is copyrighted by the people and entities mentioned after the "@author" tags above, on behalf of the JUTIL.ORG Project. The copyright is dated by the dates after the "@date" tags above. All rights reserved.This software is published under the terms of the JUTIL.ORG SoftwareLicense version 1.1 or later, a copy of which has been included withthis distribution in the LICENSE file, which can also be found athttp://org-jutil.sourceforge.net/LICENSE. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the JUTIL.ORG Software License for more details. For more information,please see http://org-jutil.sourceforge.net/</copyright>/*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -