StringTokenizer.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* StringTokenizer -- breaks a String into tokens
  2. Copyright (C) 1998, 1999, 2001, 2002, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util;
  32. /**
  33. * This class splits a string into tokens. The caller can set on which
  34. * delimiters the string should be split and if the delimiters should be
  35. * returned. This is much simpler than {@link java.io.StreamTokenizer}.
  36. *
  37. * <p>You may change the delimiter set on the fly by calling
  38. * nextToken(String). But the semantic is quite difficult; it even
  39. * depends on calling <code>hasMoreTokens()</code>. You should call
  40. * <code>hasMoreTokens()</code> before, otherwise the old delimiters
  41. * after the last token are candidates for being returned.
  42. *
  43. * <p>If you want to get the delimiters, you have to use the three argument
  44. * constructor. The delimiters are returned as token consisting of a
  45. * single character.
  46. *
  47. * @author Jochen Hoenicke
  48. * @author Warren Levy (warrenl@cygnus.com)
  49. * @see java.io.StreamTokenizer
  50. * @status updated to 1.4
  51. */
  52. public class StringTokenizer implements Enumeration<Object>
  53. {
  54. // WARNING: StringTokenizer is a CORE class in the bootstrap cycle. See the
  55. // comments in vm/reference/java/lang/Runtime for implications of this fact.
  56. /**
  57. * The position in the str, where we currently are.
  58. */
  59. private int pos;
  60. /**
  61. * The string that should be split into tokens.
  62. */
  63. private final String str;
  64. /**
  65. * The length of the string.
  66. */
  67. private final int len;
  68. /**
  69. * The string containing the delimiter characters.
  70. */
  71. private String delim;
  72. /**
  73. * Tells, if we should return the delimiters.
  74. */
  75. private final boolean retDelims;
  76. /**
  77. * Creates a new StringTokenizer for the string <code>str</code>,
  78. * that should split on the default delimiter set (space, tab,
  79. * newline, return and formfeed), and which doesn't return the
  80. * delimiters.
  81. *
  82. * @param str The string to split
  83. * @throws NullPointerException if str is null
  84. */
  85. public StringTokenizer(String str)
  86. {
  87. this(str, " \t\n\r\f", false);
  88. }
  89. /**
  90. * Create a new StringTokenizer, that splits the given string on
  91. * the given delimiter characters. It doesn't return the delimiter
  92. * characters.
  93. *
  94. * @param str the string to split
  95. * @param delim a string containing all delimiter characters
  96. * @throws NullPointerException if either argument is null
  97. */
  98. public StringTokenizer(String str, String delim)
  99. {
  100. this(str, delim, false);
  101. }
  102. /**
  103. * Create a new StringTokenizer, that splits the given string on
  104. * the given delimiter characters. If you set
  105. * <code>returnDelims</code> to <code>true</code>, the delimiter
  106. * characters are returned as tokens of their own. The delimiter
  107. * tokens always consist of a single character.
  108. *
  109. * @param str the string to split
  110. * @param delim a string containing all delimiter characters
  111. * @param returnDelims tells, if you want to get the delimiters
  112. * @throws NullPointerException if str or delim is null
  113. */
  114. public StringTokenizer(String str, String delim, boolean returnDelims)
  115. {
  116. len = str.length();
  117. this.str = str;
  118. this.delim = delim;
  119. this.retDelims = returnDelims;
  120. this.pos = 0;
  121. }
  122. /**
  123. * Tells if there are more tokens.
  124. *
  125. * @return true if the next call of nextToken() will succeed
  126. */
  127. public boolean hasMoreTokens()
  128. {
  129. if (! retDelims)
  130. {
  131. while (pos < len && delim.indexOf(str.charAt(pos)) >= 0)
  132. pos++;
  133. }
  134. return pos < len;
  135. }
  136. /**
  137. * Returns the nextToken, changing the delimiter set to the given
  138. * <code>delim</code>. The change of the delimiter set is
  139. * permanent, ie. the next call of nextToken(), uses the same
  140. * delimiter set.
  141. *
  142. * @param delim a string containing the new delimiter characters
  143. * @return the next token with respect to the new delimiter characters
  144. * @throws NoSuchElementException if there are no more tokens
  145. * @throws NullPointerException if delim is null
  146. */
  147. public String nextToken(String delim) throws NoSuchElementException
  148. {
  149. this.delim = delim;
  150. return nextToken();
  151. }
  152. /**
  153. * Returns the nextToken of the string.
  154. *
  155. * @return the next token with respect to the current delimiter characters
  156. * @throws NoSuchElementException if there are no more tokens
  157. */
  158. public String nextToken() throws NoSuchElementException
  159. {
  160. if (pos < len && delim.indexOf(str.charAt(pos)) >= 0)
  161. {
  162. if (retDelims)
  163. return str.substring(pos, ++pos);
  164. while (++pos < len && delim.indexOf(str.charAt(pos)) >= 0)
  165. ;
  166. }
  167. if (pos < len)
  168. {
  169. int start = pos;
  170. while (++pos < len && delim.indexOf(str.charAt(pos)) < 0)
  171. ;
  172. return str.substring(start, pos);
  173. }
  174. throw new NoSuchElementException();
  175. }
  176. /**
  177. * This does the same as hasMoreTokens. This is the
  178. * <code>Enumeration</code> interface method.
  179. *
  180. * @return true, if the next call of nextElement() will succeed
  181. * @see #hasMoreTokens()
  182. */
  183. public boolean hasMoreElements()
  184. {
  185. return hasMoreTokens();
  186. }
  187. /**
  188. * This does the same as nextTokens. This is the
  189. * <code>Enumeration</code> interface method.
  190. *
  191. * @return the next token with respect to the current delimiter characters
  192. * @throws NoSuchElementException if there are no more tokens
  193. * @see #nextToken()
  194. */
  195. public Object nextElement() throws NoSuchElementException
  196. {
  197. return nextToken();
  198. }
  199. /**
  200. * This counts the number of remaining tokens in the string, with
  201. * respect to the current delimiter set.
  202. *
  203. * @return the number of times <code>nextTokens()</code> will succeed
  204. * @see #nextToken()
  205. */
  206. public int countTokens()
  207. {
  208. int count = 0;
  209. int delimiterCount = 0;
  210. boolean tokenFound = false; // Set when a non-delimiter is found
  211. int tmpPos = pos;
  212. // Note for efficiency, we count up the delimiters rather than check
  213. // retDelims every time we encounter one. That way, we can
  214. // just do the conditional once at the end of the method
  215. while (tmpPos < len)
  216. {
  217. if (delim.indexOf(str.charAt(tmpPos++)) >= 0)
  218. {
  219. if (tokenFound)
  220. {
  221. // Got to the end of a token
  222. count++;
  223. tokenFound = false;
  224. }
  225. delimiterCount++; // Increment for this delimiter
  226. }
  227. else
  228. {
  229. tokenFound = true;
  230. // Get to the end of the token
  231. while (tmpPos < len
  232. && delim.indexOf(str.charAt(tmpPos)) < 0)
  233. ++tmpPos;
  234. }
  235. }
  236. // Make sure to count the last token
  237. if (tokenFound)
  238. count++;
  239. // if counting delmiters add them into the token count
  240. return retDelims ? count + delimiterCount : count;
  241. }
  242. } // class StringTokenizer