jdk源码阅读之-2.30.StringTokenizer·4

Posted Share猿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdk源码阅读之-2.30.StringTokenizer·4相关的知识,希望对你有一定的参考价值。

小猿“思前享后”为大家分享优质内容!————Share猿

1.1.StringTokenizer

  StringTokenizer是字符串分隔解析类型。

1.1.1.Element
    //当前位置
    private int currentPosition;    //新位置
    private int newPosition;    //最大位置
    private int maxPosition;    private String str;    private String delimiters;    //是否返回分隔符
    private boolean retDelims;    private boolean delimsChanged;    /**     * maxDelimCodePoint stores the value of the delimiter character with the     * highest value. It is used to optimize the detection of delimiter     * characters.     *     * It is unlikely to provide any optimization benefit in the     * hasSurrogates case because most string characters will be     * smaller than the limit, but we keep it so that the two code     * paths remain similar.     */
    private int maxDelimCodePoint;    /**     * If delimiters include any surrogates (including surrogate     * pairs), hasSurrogates is true and the tokenizer uses the     * different code path. This is because String.indexOf(int)     * doesn't handle unpaired surrogates as a single character.     */
    private boolean hasSurrogates = false;    /**     * When hasSurrogates is true, delimiters are converted to code     * points and isDelimiter(int) is used to determine if the given     * codepoint is a delimiter.     */
    private int[] delimiterCodePoints;
1.1.2.Constructors
版本 构造体 释义
1.0 StringTokenizer(String str) 只有一个字符串表示,以“空格”、“制表符(‘\t’)”、“换行符(‘\n’)”、“回车符(‘\r’)”这几种来解析一个新的StringTokenizer对象,并且默认不返回分隔符。
1.0 StringTokenizer(String str, String delim) 构造一个用来解析str的StringTokenizer对象,并提供一个指定的分隔符
1.0 StringTokenizer(String str, String delim, boolean returnDelims) 构造一个用来解析str的StringTokenizer对象,并提供一个指定的分隔符,同时,指定是否返回分隔符。
1.1.3.Method
1.1.3.1.private int skipDelimiters(int startPos)
/**
     * Skips delimiters starting from the specified position. If retDelims
     * is false, returns the index of the first non-delimiter character at or
     * after startPos. If retDelims is true, startPos is returned.
     */    private int skipDelimiters(int startPos) {        if (delimiters == null)
            throw new NullPointerException();        int position = startPos;        while (!retDelims && position < maxPosition) {
            if (!hasSurrogates) {
                char c = str.charAt(position);                if ((c > maxDelimCodePoint) || (delimiters.indexOf(c) < 0))
                    break;                position++;
            } else {                int c = str.codePointAt(position);                if ((c > maxDelimCodePoint) || !isDelimiter(c)) {
                    break;
                }                position += Character.charCount(c);
            }
        }        return position;
    }

  这个函数就是通过传入position判断是否是字符串的最后一个值,如果是返回原值,如果不是返回新的值。

1.1.3.2private int scanToken(int startPos)
   /**
     * Skips ahead from startPos and returns the index of the next delimiter
     * character encountered, or maxPosition if no such delimiter is found.
     */    private int scanToken(int startPos) {        int position = startPos;        while (position < maxPosition) {            if (!hasSurrogates) {
                char c = str.charAt(position);                if ((c <= maxDelimCodePoint) && (delimiters.indexOf(c) >= 0))
                    break;                position++;
            } else {                int c = str.codePointAt(position);                if ((c <= maxDelimCodePoint) && isDelimiter(c))
                    break;                position += Character.charCount(c);
            }
        }        if (retDelims && (startPos == position)) {            if (!hasSurrogates) {
                char c = str.charAt(position);                if ((c <= maxDelimCodePoint) && (delimiters.indexOf(c) >= 0))                    position++;
            } else {                int c = str.codePointAt(position);                if ((c <= maxDelimCodePoint) && isDelimiter(c))                    position += Character.charCount(c);
            }
        }        return position;
    }

  看这个方法我们就可以看出这个函数的作用,scan,浏览字符串,跟上面的方法基本相同,不同的是这个方法在下面又加了一段逻辑,就是当位置相等的时候继续返回,这样就可以返回最后一个字符。

1.1.3.3.private boolean isDelimiter(int codePoint)
 private boolean isDelimiter(int codePoint) {        for (int i = 0; i < delimiterCodePoints.length; i++) {            if (delimiterCodePoints[i] == codePoint) {                return true;
            }
        }        return false;
    }

  这个函数是用来判断该代码点是否是一个分隔符。

1.1.3.4.hasMoreTokens()
    /**
     * Tests if there are more tokens available from this tokenizer's string.
     * If this method returns <tt>true</tt>, then a subsequent call to
     * <tt>nextToken</tt> with no argument will successfully return a token.
     *
     * @return  <code>true</code> if and only if there is at least one token
     *          in the string after the current position; <code>false</code>
     *          otherwise.
     */
    public boolean hasMoreTokens() {
        /*
         * Temporarily store this position and use it in the following
         * nextToken() method only if the delimiters haven't been changed in
         * that nextToken() invocation.
         */
        newPosition = skipDelimiters(currentPosition);        return (newPosition < maxPosition);
    }

  这个函数用来判断是否有下一个分隔符。

1.1.3.5.public String nextToken()
   /**
     * Returns the next token from this string tokenizer.
     *
     * @return     the next token from this string tokenizer.
     * @exception  NoSuchElementException  if there are no more tokens in this
     *               tokenizer's string.
     */
    public String nextToken() {
        /*
         * If next position already computed in hasMoreElements() and
         * delimiters have changed between the computation and this invocation,
         * then use the computed value.
         */

        currentPosition = (newPosition >= 0 && !delimsChanged) ?
            newPosition : skipDelimiters(currentPosition);

        /* Reset these anyway */
        delimsChanged = false;
        newPosition = -1;        if (currentPosition >= maxPosition)
            throw new NoSuchElementException();        int start = currentPosition;
        currentPosition = scanToken(currentPosition);
        return str.substring(start, currentPosition);
    }

  返回分隔符前面的字符串。

1.1.3.6.public String nextToken(String delim)
    /**
     * Returns the next token in this string tokenizer's string. First,
     * the set of characters considered to be delimiters by this
     * <tt>StringTokenizer</tt> object is changed to be the characters in
     * the string <tt>delim</tt>. Then the next token in the string
     * after the current position is returned. The current position is
     * advanced beyond the recognized token.  The new delimiter set
     * remains the default after this call.
     *
     * @param      delim   the new delimiters.
     * @return     the next token, after switching to the new delimiter set.
     * @exception  NoSuchElementException  if there are no more tokens in this
     *               tokenizer's string.
     * @exception NullPointerException if delim is <CODE>null</CODE>
     */    public String nextToken(String delim) {
        delimiters = delim;

        /* delimiter string specified, so set the appropriate flag. */
        delimsChanged = true;

        setMaxDelimCodePoint();
        return nextToken();
    }

  返回被指定分隔符分割后的字符串。

1.1.3.7.public boolean hasMoreElements()
    /**
     * Returns the same value as the <code>hasMoreTokens</code>
     * method. It exists so that this class can implement the     * <code>Enumeration</code> interface.     *
     * @return  <code>true</code> if there are more tokens;
     *          <code>false</code> otherwise.     * @see     java.util.Enumeration
     * @see     java.util.StringTokenizer#hasMoreTokens()
     */
    public boolean hasMoreElements() {
        return hasMoreTokens();
    }

  感觉这个方法和hasMoreTokens没什么太大的区别,没看懂。

1.1.3.8. public Object nextElement()
    /**
     * Returns the same value as the <code>nextToken</code> method,     * except that its declared return value is <code>Object</code> rather than     * <code>String</code>. It exists so that this class can implement the     * <code>Enumeration</code> interface.     *
     * @return     the next token in the string.
     * @exception  NoSuchElementException  if there are no more tokens in this
     *               tokenizer's string.     * @see        java.util.Enumeration
     * @see        java.util.StringTokenizer#nextToken()
     */
    public Object nextElement() {
        return nextToken();
    }

  跟nextToken有什么区别??

1.1.3.9. public int countTokens()
    /**
     * Calculates the number of times that this tokenizer's
     * <code>nextToken</code> method can be called before it generates an
     * exception. The current position is not advanced.
     *
     * @return  the number of tokens remaining in the string using the current
     *          delimiter set.
     * @see     java.util.StringTokenizer#nextToken()
     */
    public int countTokens() {        int count = 0;        int currpos = currentPosition;        while (currpos < maxPosition) {
            currpos = skipDelimiters(currpos);            if (currpos >= maxPosition)                break;
            currpos = scanToken(currpos);
            count++;
        }        return count;
    }

 &emsp;计数器。

1.1.3.Question
1.1.3.1.Question1??????

  文档中有写的不合适的地方,烦请联系我的微信(nongjiachuangzhan)或QQ(330334064)帮忙指正,共同完善文档,谢谢!

相关链接:

【1】Java(TM) EE 8 Specification APIs·Oracle


文档会不定期的更新和修改,最新文档请点击↓↓↓↓阅读原文↓↓↓↓


以上是关于jdk源码阅读之-2.30.StringTokenizer·4的主要内容,如果未能解决你的问题,请参考以下文章

JDK1.8源码分析02之阅读源码顺序

1.4JDK源码阅读之AbstractStringBuilder

JDK源码阅读之Collection

JDK源码阅读之BufferedInputStream

源码阅读系列JDK 8 ConcurrentHashMap 源码分析之 由transfer引发的bug

1.2JDK源码阅读之Object