Source for org.jfree.util.ReadOnlyIterator

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * ---------------------
  28:  * ReadOnlyIterator.java
  29:  * ---------------------
  30:  * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: ReadOnlyIterator.java,v 1.2 2005/10/18 13:24:19 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------------------------
  39:  * 18-Jul-2003 : Initial version
  40:  *
  41:  */
  42: 
  43: package org.jfree.util;
  44: 
  45: import java.util.Iterator;
  46: 
  47: /**
  48:  * Protects an given iterator by preventing calls to remove().
  49:  *
  50:  * @author Thomas Morgner
  51:  */
  52: public class ReadOnlyIterator implements Iterator {
  53: 
  54:     /** The base iterator which we protect. */
  55:     private Iterator base;
  56: 
  57:     /**
  58:      * Creates a new read-only iterator for the given iterator.
  59:      *
  60:      * @param it the iterator.
  61:      */
  62:     public ReadOnlyIterator(final Iterator it) {
  63:         if (it == null) {
  64:             throw new NullPointerException("Base iterator is null.");
  65:         }
  66:         this.base = it;
  67:     }
  68: 
  69:     /**
  70:      * Returns <tt>true</tt> if the iteration has more elements. (In other
  71:      * words, returns <tt>true</tt> if <tt>next</tt> would return an element
  72:      * rather than throwing an exception.)
  73:      *
  74:      * @return <tt>true</tt> if the iterator has more elements.
  75:      */
  76:     public boolean hasNext() {
  77:         return this.base.hasNext();
  78:     }
  79: 
  80:     /**
  81:      * Returns the next element in the iteration.
  82:      * Throws NoSuchElementException when iteration has no more elements.
  83:      * 
  84:      * @return the next element in the iteration.
  85:      */
  86:     public Object next() {
  87:         return this.base.next();
  88:     }
  89: 
  90:     /**
  91:      * Throws <code>UnsupportedOperationException</code>.
  92:      */
  93:     public void remove() {
  94:         throw new UnsupportedOperationException();
  95:     }
  96: }