home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / text / StringCharacterIterator.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  6.6 KB  |  253 lines

  1. /*
  2.  * @(#)StringCharacterIterator.java    1.13 98/03/18
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. package java.text;
  32.  
  33. /**
  34.  * <code>StringCharacterIterator</code> implements the
  35.  * <code>CharacterIterater</code> protocol for a <code>String</code>.
  36.  * The <code>StringCharacterIterator</code> class iterates over the
  37.  * entire <code>String</code>.
  38.  *
  39.  * @see CharacterIterator
  40.  */
  41.  
  42. public final class StringCharacterIterator implements CharacterIterator, java.io.Serializable
  43. {
  44.     private String text;
  45.     private int begin;
  46.     private int end;
  47.     // invariant: begin <= pos <= end
  48.     private int pos;
  49.  
  50.     /**
  51.      * Constructs an iterator with an initial index of 0.
  52.      */
  53.     public StringCharacterIterator(String text)
  54.     {
  55.         this(text, 0);
  56.     }
  57.  
  58.     /**
  59.      * Constructs an iterator with the specified initial index.
  60.      *
  61.      * @param  text   The String to be iterated over
  62.      * @param  pos    Initial iterator position
  63.      */
  64.     public StringCharacterIterator(String text, int pos)
  65.     {
  66.     this(text, 0, text.length(), pos);
  67.     }
  68.  
  69.     /**
  70.      * Constructs an iterator over the given range of the given string, with the
  71.      * index set at the specified position.
  72.      *
  73.      * @param  text   The String to be iterated over
  74.      * @param  begin  Index of the first character
  75.      * @param  end    Index of the character following the last character
  76.      * @param  pos    Initial iterator position
  77.      */
  78.     public StringCharacterIterator(String text, int begin, int end, int pos) {
  79.         if (text == null)
  80.             throw new NullPointerException();
  81.         this.text = text;
  82.  
  83.     if (begin < 0 || begin > end || end > text.length())
  84.         throw new IllegalArgumentException("Invalid substring range");
  85.  
  86.         if (pos < begin || pos > end)
  87.             throw new IllegalArgumentException("Invalid position");
  88.  
  89.         this.begin = begin;
  90.         this.end = end;
  91.         this.pos = pos;
  92.     }
  93.  
  94.  
  95.     /**
  96.      * Implements CharacterIterator.first() for String.
  97.      * @see CharacterIterator#first
  98.      */
  99.     public char first()
  100.     {
  101.         pos = begin;
  102.         return current();
  103.     }
  104.  
  105.     /**
  106.      * Implements CharacterIterator.last() for String.
  107.      * @see CharacterIterator#last
  108.      */
  109.     public char last()
  110.     {
  111.         if (end != begin) {
  112.             pos = end - 1;
  113.         } else {
  114.             pos = end;
  115.         }
  116.         return current();
  117.      }
  118.  
  119.     /**
  120.      * Implements CharacterIterator.setIndex() for String.
  121.      * @see CharacterIterator#setIndex
  122.      */
  123.     public char setIndex(int p)
  124.     {
  125.     if (p < begin || p > end)
  126.             throw new IllegalArgumentException("Invalid index");
  127.         pos = p;
  128.         return current();
  129.     }
  130.  
  131.     /**
  132.      * Implements CharacterIterator.current() for String.
  133.      * @see CharacterIterator#current
  134.      */
  135.     public char current()
  136.     {
  137.         if (pos >= begin && pos < end) {
  138.             return text.charAt(pos);
  139.         }
  140.         else {
  141.             return DONE;
  142.         }
  143.     }
  144.  
  145.     /**
  146.      * Implements CharacterIterator.next() for String.
  147.      * @see CharacterIterator#next
  148.      */
  149.     public char next()
  150.     {
  151.         if (pos < end - 1) {
  152.             pos++;
  153.             return text.charAt(pos);
  154.         }
  155.         else {
  156.             pos = end;
  157.             return DONE;
  158.         }
  159.     }
  160.  
  161.     /**
  162.      * Implements CharacterIterator.previous() for String.
  163.      * @see CharacterIterator#previous
  164.      */
  165.     public char previous()
  166.     {
  167.         if (pos > begin) {
  168.             pos--;
  169.             return text.charAt(pos);
  170.         }
  171.         else {
  172.             return DONE;
  173.         }
  174.     }
  175.  
  176.     /**
  177.      * Implements CharacterIterator.getBeginIndex() for String.
  178.      * @see CharacterIterator#getBeginIndex
  179.      */
  180.     public int getBeginIndex()
  181.     {
  182.         return begin;
  183.     }
  184.  
  185.     /**
  186.      * Implements CharacterIterator.getEndIndex() for String.
  187.      * @see CharacterIterator#getEndIndex
  188.      */
  189.     public int getEndIndex()
  190.     {
  191.         return end;
  192.     }
  193.  
  194.     /**
  195.      * Implements CharacterIterator.getIndex() for String.
  196.      * @see CharacterIterator#getIndex
  197.      */
  198.     public int getIndex()
  199.     {
  200.         return pos;
  201.     }
  202.  
  203.     /**
  204.      * Compares the equality of two StringCharacterIterator objects.
  205.      * @param obj the StringCharacterIterator object to be compared with.
  206.      * @return true if the given obj is the same as this
  207.      * StringCharacterIterator object; false otherwise.
  208.      */
  209.     public boolean equals(Object obj)
  210.     {
  211.         if (this == obj)
  212.             return true;
  213.         if (!(obj instanceof StringCharacterIterator))
  214.             return false;
  215.  
  216.         StringCharacterIterator that = (StringCharacterIterator) obj;
  217.  
  218.         if (hashCode() != that.hashCode())
  219.             return false;
  220.         if (!text.equals(that.text))
  221.             return false;
  222.         if (pos != that.pos || begin != that.begin || end != that.end)
  223.             return false;
  224.         return true;
  225.     }
  226.  
  227.     /**
  228.      * Computes a hashcode for this iterator.
  229.      * @return A hash code
  230.      */
  231.     public int hashCode()
  232.     {
  233.         return text.hashCode() ^ pos ^ begin ^ end;
  234.     }
  235.  
  236.     /**
  237.      * Creates a copy of this iterator.
  238.      * @return A copy of this
  239.      */
  240.     public Object clone()
  241.     {
  242.         try {
  243.             StringCharacterIterator other
  244.             = (StringCharacterIterator) super.clone();
  245.             return other;
  246.         }
  247.         catch (CloneNotSupportedException e) {
  248.             throw new InternalError();
  249.         }
  250.     }
  251.  
  252. }
  253.