home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / util / stringto.jav < prev    next >
Encoding:
Text File  |  1995-10-30  |  5.3 KB  |  202 lines

  1. /*
  2.  * @(#)StringTokenizer.java    1.13 95/08/10  
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.util;
  21.  
  22. import java.lang.*;
  23.  
  24. /**
  25.  * StringTokenizer is a class that controls simple linear tokenization
  26.  * of a String. The set of delimiters, which defaults to common 
  27.  * whitespace characters, may be specified at creation time or on a 
  28.  * per-token basis.<p>
  29.  *
  30.  * Example usage:
  31.  * <pre>
  32.  *    String s = "this is a test";
  33.  *    StringTokenizer st = new StringTokenizer(s);
  34.  *    while (st.hasMoreTokens()) {
  35.  *        println(st.nextToken());
  36.  *    }
  37.  * </pre>
  38.  * Prints the following on the console:
  39.  * <pre>
  40.  *    this
  41.  *    is
  42.  *    a
  43.  *    test
  44.  * </pre>
  45.  * @version     1.13, 08/10/95
  46.  */
  47. public
  48. class StringTokenizer implements Enumeration {
  49.     private int currentPosition;
  50.     private int maxPosition;
  51.     private String str;
  52.     private String delimiters;
  53.     private boolean retTokens;
  54.  
  55.     /**
  56.      * Constructs a StringTokenizer on the specified String, using the
  57.      * specified delimiter set.
  58.      * @param str       the input String
  59.      * @param delim        the delimiter String
  60.      * @param returnTokens returns delimiters as tokens or skip them
  61.      */
  62.     public StringTokenizer(String str, String delim, boolean returnTokens) {
  63.     currentPosition = 0;
  64.     this.str = str;
  65.     maxPosition = str.length();
  66.     delimiters = delim;
  67.     retTokens = returnTokens;
  68.     }
  69.  
  70.     /**
  71.      * Constructs a StringTokenizer on the specified String, using the
  72.      * specified delimiter set.
  73.      * @param str    the input String
  74.      * @param delim the delimiter String
  75.      */
  76.     public StringTokenizer(String str, String delim) {
  77.     this(str, delim, false);
  78.     }
  79.  
  80.     /**
  81.      * Constructs a StringTokenizer on the specified String, using the
  82.      * default delimiter set (which is " \t\n\r").
  83.      * @param str the String
  84.      */
  85.     public StringTokenizer(String str) {
  86.     this(str, " \t\n\r", false);
  87.     }
  88.  
  89.     /**
  90.      * Skips delimiters.
  91.      */
  92.     private void skipDelimiters() {
  93.     while (!retTokens &&
  94.            (currentPosition < maxPosition) &&
  95.            (delimiters.indexOf(str.charAt(currentPosition)) >= 0)) {
  96.         currentPosition++;
  97.     }
  98.     }
  99.  
  100.     /**
  101.      * Returns true if more tokens exist.
  102.      */
  103.     public boolean hasMoreTokens() {
  104.     skipDelimiters();
  105.     return (currentPosition < maxPosition);
  106.     }
  107.  
  108.     /**
  109.      * Returns the next token of the String.
  110.      * @exception NoSuchElementException If there are no more 
  111.      * tokens in the String.
  112.      */
  113.     public String nextToken() {
  114.     skipDelimiters();
  115.  
  116.     if (currentPosition >= maxPosition) {
  117.         throw new NoSuchElementException();
  118.     }
  119.  
  120.     int start = currentPosition;
  121.     while ((currentPosition < maxPosition) && 
  122.            (delimiters.indexOf(str.charAt(currentPosition)) < 0)) {
  123.         currentPosition++;
  124.     }
  125.     if (retTokens && (start == currentPosition) &&
  126.         (delimiters.indexOf(str.charAt(currentPosition)) >= 0)) {
  127.         currentPosition++;
  128.     }
  129.     return str.substring(start, currentPosition);
  130.     }
  131.  
  132.     /**
  133.      * Returns the next token, after switching to the new delimiter set.
  134.      * The new delimiter set remains the default after this call.
  135.      * @param delim the new delimiters
  136.      */
  137.     public String nextToken(String delim) {
  138.     delimiters = delim;
  139.     return nextToken();
  140.     }
  141.  
  142.     /**
  143.      * Returns true if the Enumeration has more elements.
  144.      */
  145.     public boolean hasMoreElements() {
  146.     return hasMoreTokens();
  147.     }
  148.  
  149.     /**
  150.      * Returns the next element in the Enumeration.
  151.      * @exception NoSuchElementException If there are no more elements 
  152.      * in the enumeration.
  153.      */
  154.     public Object nextElement() {
  155.     return nextToken();
  156.     }
  157.  
  158.     /**
  159.      * Returns the next number of tokens in the String using
  160.      * the current deliminter set.  This is the number of times
  161.      * nextToken() can return before it will generate an exception.
  162.      * Use of this routine to count the number of tokens is faster
  163.      * than repeatedly calling nextToken() because the substrings
  164.      * are not constructed and returned for each token.
  165.      */
  166.     public int countTokens() {
  167.     int count = 0;
  168.     int currpos = currentPosition;
  169.  
  170.     while (currpos < maxPosition) {
  171.         /*
  172.          * This is just skipDelimiters(); but it does not affect
  173.          * currentPosition.
  174.          */
  175.         while (!retTokens &&
  176.            (currpos < maxPosition) &&
  177.            (delimiters.indexOf(str.charAt(currpos)) >= 0)) {
  178.         currpos++;
  179.         }
  180.  
  181.         if (currpos >= maxPosition) {
  182.         break;
  183.         }
  184.  
  185.         int start = currpos;
  186.         while ((currpos < maxPosition) && 
  187.            (delimiters.indexOf(str.charAt(currpos)) < 0)) {
  188.         currpos++;
  189.         }
  190.         if (retTokens && (start == currpos) &&
  191.         (delimiters.indexOf(str.charAt(currpos)) >= 0)) {
  192.         currpos++;
  193.         }
  194.         count++;
  195.  
  196.     }
  197.     return count;
  198.     }
  199. }
  200.  
  201.  
  202.