2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2003, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* $RCSfile: StringSplitter.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.6 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2005/03/10 15:27:01 $ |
2 |
26 Feb 07 |
jari |
* $Author: braistedj $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
package org.tigr.util; |
2 |
26 Feb 07 |
jari |
13 |
|
2 |
26 Feb 07 |
jari |
14 |
import java.util.NoSuchElementException; |
2 |
26 Feb 07 |
jari |
15 |
|
2 |
26 Feb 07 |
jari |
16 |
public class StringSplitter { |
2 |
26 Feb 07 |
jari |
17 |
|
2 |
26 Feb 07 |
jari |
18 |
private String str; |
2 |
26 Feb 07 |
jari |
19 |
private char delimiter; |
2 |
26 Feb 07 |
jari |
20 |
|
2 |
26 Feb 07 |
jari |
21 |
private int curPosition; |
2 |
26 Feb 07 |
jari |
22 |
private int maxPosition; |
2 |
26 Feb 07 |
jari |
23 |
|
2 |
26 Feb 07 |
jari |
24 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs a <code>StringSplitter</code> with specified delimiter. |
2 |
26 Feb 07 |
jari |
26 |
*/ |
2 |
26 Feb 07 |
jari |
27 |
public StringSplitter(char delimiter) { |
2 |
26 Feb 07 |
jari |
28 |
init("", delimiter); |
2 |
26 Feb 07 |
jari |
29 |
} |
2 |
26 Feb 07 |
jari |
30 |
|
2 |
26 Feb 07 |
jari |
31 |
/** |
2 |
26 Feb 07 |
jari |
* Initializes this <code>StringSplitter</code> with a new data. |
2 |
26 Feb 07 |
jari |
33 |
*/ |
2 |
26 Feb 07 |
jari |
34 |
public void init(String str) { |
2 |
26 Feb 07 |
jari |
35 |
init(str, this.delimiter); |
2 |
26 Feb 07 |
jari |
36 |
} |
2 |
26 Feb 07 |
jari |
37 |
|
2 |
26 Feb 07 |
jari |
38 |
/** |
2 |
26 Feb 07 |
jari |
* Initializes this <code>StringSplitter</code> with a new data and delimeter. |
2 |
26 Feb 07 |
jari |
40 |
*/ |
2 |
26 Feb 07 |
jari |
41 |
private void init(String str, char delimiter) { |
2 |
26 Feb 07 |
jari |
42 |
this.str = str; |
2 |
26 Feb 07 |
jari |
43 |
this.delimiter = delimiter; |
2 |
26 Feb 07 |
jari |
44 |
curPosition = 0; |
2 |
26 Feb 07 |
jari |
45 |
maxPosition = str.length(); |
2 |
26 Feb 07 |
jari |
46 |
} |
2 |
26 Feb 07 |
jari |
47 |
|
2 |
26 Feb 07 |
jari |
48 |
/** |
2 |
26 Feb 07 |
jari |
* Skips ahead from startPos and returns the index of the next delimiter |
2 |
26 Feb 07 |
jari |
* character encountered, or maxPosition if no such delimiter is found. |
2 |
26 Feb 07 |
jari |
51 |
*/ |
2 |
26 Feb 07 |
jari |
52 |
private int scanToken(int startPos) { |
2 |
26 Feb 07 |
jari |
53 |
int position = startPos; |
2 |
26 Feb 07 |
jari |
54 |
while (position < maxPosition) { |
2 |
26 Feb 07 |
jari |
55 |
char c = str.charAt(position); |
2 |
26 Feb 07 |
jari |
56 |
if (c == delimiter) |
2 |
26 Feb 07 |
jari |
57 |
break; |
2 |
26 Feb 07 |
jari |
58 |
position++; |
2 |
26 Feb 07 |
jari |
59 |
} |
2 |
26 Feb 07 |
jari |
60 |
return position; |
2 |
26 Feb 07 |
jari |
61 |
} |
2 |
26 Feb 07 |
jari |
62 |
|
2 |
26 Feb 07 |
jari |
63 |
/** |
2 |
26 Feb 07 |
jari |
* Tests if there are more tokens available from this tokenizer's string. |
2 |
26 Feb 07 |
jari |
* If this method returns <tt>true</tt>, then a subsequent call to |
2 |
26 Feb 07 |
jari |
* <tt>nextToken</tt> with no argument will successfully return a token. |
2 |
26 Feb 07 |
jari |
67 |
* |
2 |
26 Feb 07 |
jari |
* @return <code>true</code> if and only if there is at least one token |
2 |
26 Feb 07 |
jari |
* in the string after the current position; <code>false</code> |
2 |
26 Feb 07 |
jari |
* otherwise. |
2 |
26 Feb 07 |
jari |
71 |
*/ |
2 |
26 Feb 07 |
jari |
72 |
public boolean hasMoreTokens() { |
2 |
26 Feb 07 |
jari |
73 |
return curPosition < maxPosition; |
2 |
26 Feb 07 |
jari |
74 |
} |
2 |
26 Feb 07 |
jari |
75 |
|
2 |
26 Feb 07 |
jari |
76 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the next token from this string tokenizer. |
2 |
26 Feb 07 |
jari |
78 |
* |
2 |
26 Feb 07 |
jari |
* @return the next token from this string tokenizer. |
2 |
26 Feb 07 |
jari |
* @exception NoSuchElementException if there are no more tokens in this |
2 |
26 Feb 07 |
jari |
* tokenizer's string. |
2 |
26 Feb 07 |
jari |
82 |
*/ |
2 |
26 Feb 07 |
jari |
83 |
public String nextToken() { |
2 |
26 Feb 07 |
jari |
84 |
if (curPosition >= maxPosition) |
2 |
26 Feb 07 |
jari |
85 |
throw new NoSuchElementException("There are no more tokens!"); |
2 |
26 Feb 07 |
jari |
86 |
int start = curPosition; |
2 |
26 Feb 07 |
jari |
87 |
curPosition = scanToken(curPosition); |
2 |
26 Feb 07 |
jari |
88 |
String result = str.substring(start, curPosition); |
2 |
26 Feb 07 |
jari |
89 |
curPosition++; |
2 |
26 Feb 07 |
jari |
90 |
return result; |
2 |
26 Feb 07 |
jari |
91 |
} |
2 |
26 Feb 07 |
jari |
92 |
|
2 |
26 Feb 07 |
jari |
93 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the number of tokens. |
2 |
26 Feb 07 |
jari |
95 |
*/ |
2 |
26 Feb 07 |
jari |
96 |
public int countTokens() { |
2 |
26 Feb 07 |
jari |
97 |
int count = 0; |
2 |
26 Feb 07 |
jari |
98 |
int pos = 0; |
2 |
26 Feb 07 |
jari |
99 |
while (pos < this.maxPosition) { |
2 |
26 Feb 07 |
jari |
100 |
if (str.charAt(pos) == this.delimiter) |
2 |
26 Feb 07 |
jari |
101 |
count++; |
2 |
26 Feb 07 |
jari |
102 |
pos++; |
2 |
26 Feb 07 |
jari |
103 |
} |
2 |
26 Feb 07 |
jari |
104 |
return count; |
2 |
26 Feb 07 |
jari |
105 |
} |
2 |
26 Feb 07 |
jari |
106 |
|
2 |
26 Feb 07 |
jari |
107 |
/** |
2 |
26 Feb 07 |
jari |
* Passes the given count of tokens. |
2 |
26 Feb 07 |
jari |
109 |
*/ |
2 |
26 Feb 07 |
jari |
110 |
public final void passTokens(int count) { |
2 |
26 Feb 07 |
jari |
111 |
for (int i = count; --i >= 0;) { |
2 |
26 Feb 07 |
jari |
112 |
curPosition = scanToken(curPosition); |
2 |
26 Feb 07 |
jari |
113 |
curPosition++; |
2 |
26 Feb 07 |
jari |
114 |
} |
2 |
26 Feb 07 |
jari |
115 |
} |
2 |
26 Feb 07 |
jari |
116 |
|
2 |
26 Feb 07 |
jari |
117 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the next token value as an integer. |
2 |
26 Feb 07 |
jari |
119 |
*/ |
2 |
26 Feb 07 |
jari |
120 |
public final int nextIntToken() { |
2 |
26 Feb 07 |
jari |
121 |
return nextIntToken(0); |
2 |
26 Feb 07 |
jari |
122 |
} |
2 |
26 Feb 07 |
jari |
123 |
|
2 |
26 Feb 07 |
jari |
124 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the next token value as an integer with specifed default value. |
2 |
26 Feb 07 |
jari |
126 |
*/ |
2 |
26 Feb 07 |
jari |
127 |
public final int nextIntToken(int defValue) { |
2 |
26 Feb 07 |
jari |
128 |
String token = nextToken(); |
2 |
26 Feb 07 |
jari |
129 |
try { |
2 |
26 Feb 07 |
jari |
130 |
return Integer.parseInt(token); |
2 |
26 Feb 07 |
jari |
131 |
} catch (NumberFormatException e) { |
2 |
26 Feb 07 |
jari |
132 |
return defValue; |
2 |
26 Feb 07 |
jari |
133 |
} |
2 |
26 Feb 07 |
jari |
134 |
} |
2 |
26 Feb 07 |
jari |
135 |
|
2 |
26 Feb 07 |
jari |
136 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the next token value as a long. |
2 |
26 Feb 07 |
jari |
138 |
*/ |
2 |
26 Feb 07 |
jari |
139 |
public final long nextLongToken() { |
2 |
26 Feb 07 |
jari |
140 |
return nextLongToken(0); |
2 |
26 Feb 07 |
jari |
141 |
} |
2 |
26 Feb 07 |
jari |
142 |
|
2 |
26 Feb 07 |
jari |
143 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the next token value as a double rounded to long. |
2 |
26 Feb 07 |
jari |
145 |
*/ |
2 |
26 Feb 07 |
jari |
146 |
public final long nextLongToken(boolean rounded) { |
2 |
26 Feb 07 |
jari |
147 |
return Math.round(nextDoubleToken(0)); |
2 |
26 Feb 07 |
jari |
148 |
} |
2 |
26 Feb 07 |
jari |
149 |
|
2 |
26 Feb 07 |
jari |
150 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the next token value as a long with specifed default value. |
2 |
26 Feb 07 |
jari |
152 |
*/ |
2 |
26 Feb 07 |
jari |
153 |
public final long nextLongToken(long defValue) { |
2 |
26 Feb 07 |
jari |
154 |
String token = nextToken(); |
2 |
26 Feb 07 |
jari |
155 |
try { |
2 |
26 Feb 07 |
jari |
156 |
return Long.parseLong(token); |
2 |
26 Feb 07 |
jari |
157 |
} catch (NumberFormatException e) { |
2 |
26 Feb 07 |
jari |
158 |
return defValue; |
2 |
26 Feb 07 |
jari |
159 |
} |
2 |
26 Feb 07 |
jari |
160 |
} |
2 |
26 Feb 07 |
jari |
161 |
|
2 |
26 Feb 07 |
jari |
162 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the next token value as a double with specifed default value. |
2 |
26 Feb 07 |
jari |
164 |
*/ |
2 |
26 Feb 07 |
jari |
165 |
public final double nextDoubleToken(double defValue) { |
2 |
26 Feb 07 |
jari |
166 |
String token = nextToken(); |
2 |
26 Feb 07 |
jari |
167 |
try { |
2 |
26 Feb 07 |
jari |
168 |
return Double.parseDouble(token); |
2 |
26 Feb 07 |
jari |
169 |
} catch (NumberFormatException e) { |
2 |
26 Feb 07 |
jari |
170 |
return defValue; |
2 |
26 Feb 07 |
jari |
171 |
} |
2 |
26 Feb 07 |
jari |
172 |
} |
2 |
26 Feb 07 |
jari |
173 |
|
2 |
26 Feb 07 |
jari |
174 |
/** |
2 |
26 Feb 07 |
jari |
* Returns the next token value as a float with specifed default value. |
2 |
26 Feb 07 |
jari |
176 |
*/ |
2 |
26 Feb 07 |
jari |
177 |
public final float nextFloatToken(float defValue) { |
2 |
26 Feb 07 |
jari |
178 |
String token = nextToken(); |
2 |
26 Feb 07 |
jari |
179 |
try { |
2 |
26 Feb 07 |
jari |
180 |
return Float.parseFloat(token); |
2 |
26 Feb 07 |
jari |
181 |
} catch (NumberFormatException e) { |
2 |
26 Feb 07 |
jari |
182 |
return defValue; |
2 |
26 Feb 07 |
jari |
183 |
} |
2 |
26 Feb 07 |
jari |
184 |
} |
2 |
26 Feb 07 |
jari |
185 |
} |