Coverage Report - no.geosoft.cc.util.SmartTokenizer
 
Classes in this File Line Coverage Branch Coverage Complexity
SmartTokenizer
0%
0/30
0%
0/14
1.857
 
 1  
 
 2  
 /*
 3  
  * This code is free software; you can redistribute it and/or
 4  
  * modify it under the terms of the GNU Lesser General Public 
 5  
  * License as published by the Free Software Foundation; either 
 6  
  * version 2.1 of the License, or (at your option) any later version.
 7  
  *
 8  
  * This code is distributed in the hope that it will be useful,
 9  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 11  
  * GNU Lesser General Public License for more details.
 12  
  *
 13  
  * You should have received a copy of the GNU Lesser General Public 
 14  
  * License along with this program; if not, write to the Free 
 15  
  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 
 16  
  * MA  02111-1307, USA.
 17  
  */
 18  
 package no.geosoft.cc.util;
 19  
 
 20  
 
 21  
 
 22  
 import java.util.ArrayList;
 23  
 
 24  
 
 25  
 
 26  
 /**
 27  
  * A StringTokenizer class that handle empty tokens.
 28  
  * 
 29  
  * @author <a href="mailto:info@geosoft.no">GeoSoft</a>
 30  
  */   
 31  
 public class SmartTokenizer 
 32  
 {
 33  
   private ArrayList <String>tokens_;
 34  
   private int       current_;
 35  
   
 36  
 
 37  
   
 38  
   public SmartTokenizer (String string, String delimiter)
 39  0
   {
 40  0
     tokens_  = new ArrayList <String> ();
 41  0
     current_ = 0;
 42  
     
 43  0
     java.util.StringTokenizer tokenizer =
 44  
       new java.util.StringTokenizer (string, delimiter, true);
 45  
 
 46  0
     boolean wasDelimiter = true;
 47  0
     boolean isDelimiter  = false;    
 48  
     
 49  0
     while (tokenizer.hasMoreTokens()) {
 50  0
       String token = tokenizer.nextToken();
 51  
 
 52  0
       isDelimiter = token.equals (delimiter);
 53  
       
 54  0
       if (wasDelimiter)
 55  0
         tokens_.add (isDelimiter ? "" : token);
 56  0
       else if (!isDelimiter)
 57  0
         tokens_.add (token);
 58  
 
 59  0
       wasDelimiter = isDelimiter;
 60  0
     }
 61  
 
 62  0
     if (isDelimiter) tokens_.add ("");
 63  0
   }
 64  
 
 65  
 
 66  
   public int countTokens()
 67  
   {
 68  0
     return tokens_.size();
 69  
   }
 70  
 
 71  
   
 72  
   public boolean hasMoreTokens()
 73  
   {
 74  0
     return current_ < tokens_.size();
 75  
   }
 76  
 
 77  
 
 78  
   public boolean hasMoreElements()
 79  
   {
 80  0
     return hasMoreTokens();
 81  
   }
 82  
 
 83  
 
 84  
   public Object nextElement()
 85  
   {
 86  0
     return nextToken();
 87  
   }
 88  
 
 89  
 
 90  
   public String nextToken()
 91  
   {
 92  0
     String token = (String) tokens_.get (current_);
 93  0
     current_++;
 94  0
     return token;
 95  
   }
 96  
   
 97  
   
 98  
 
 99  
   /**
 100  
    * Testing this class.
 101  
    * 
 102  
    * @param args  Not used.
 103  
    */
 104  
   public static void main (String args[])
 105  
   {
 106  0
     SmartTokenizer t = new SmartTokenizer ("This,is,a,,test,", ",");
 107  0
     while (t.hasMoreTokens()) {
 108  0
       String token = t.nextToken();
 109  0
       System.out.println ("#" + token + "#");
 110  0
     }
 111  0
   }
 112  
 }