1 /** 2 * The utillib library. 3 * More information is available at http://www.jinchess.com/. 4 * Copyright (C) 2002 Alexander Maryanovsky. 5 * All rights reserved. 6 * 7 * The utillib library is free software; you can redistribute 8 * it and/or modify it under the terms of the GNU Lesser General Public License 9 * as published by the Free Software Foundation; either version 2 of the 10 * License, or (at your option) any later version. 11 * 12 * The utillib library is distributed in the hope that it will 13 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public License 18 * along with utillib library; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 22 package free.util; 23 24 import java.util.Enumeration; 25 import java.util.NoSuchElementException; 26 27 28 /** 29 * An implementation of the Enumeration interface which enumerates a single 30 * item. Note: This class is not thread safe. 31 */ 32 33 public class SingleItemEnumeration implements Enumeration{ 34 35 36 /** 37 * The sole item. 38 */ 39 40 private Object item; 41 42 43 44 45 /** 46 * Becomes true when we've returned the sole item. 47 */ 48 49 private boolean done = false; 50 51 52 53 54 /** 55 * Creates a new SingleItemEnumeration which enumerates the specified item. 56 */ 57 SingleItemEnumeration(Object item)58 public SingleItemEnumeration(Object item){ 59 this.item = item; 60 } 61 62 63 64 65 /** 66 * Returns the sole item or throws a <code>NoSuchElementException</code>. 67 */ 68 nextElement()69 public Object nextElement(){ 70 if (!hasMoreElements()) 71 throw new NoSuchElementException(); 72 73 done = true; 74 75 Object item = this.item; 76 this.item = null; // We don't want to hold a reference to it any longer than we need. 77 return item; 78 } 79 80 81 82 /** 83 * Returns true if there are more elements in the enumeration. 84 */ 85 hasMoreElements()86 public boolean hasMoreElements(){ 87 return !done; 88 } 89 90 91 92 } 93