jdk/src/share/classes/sun/util/locale/StringTokenIterator.java
author okutsu
Thu, 14 Apr 2011 15:59:47 +0900
changeset 9224 75c0420badef
parent 6501 684810d882b3
permissions -rw-r--r--
7028818: (lc) Lazily initialize locale extension 7029740: (lc) New Locale class implementation doesn't follow the Java coding conventions 7032820: (lc) sun.util.locale.InternalLocaleBuilder.CaseInsensitiveChar.equals problems 7033503: (lc) Restore optimization code for Locale class initialization 7033504: (lc) incompatible behavior change for ja_JP_JP and th_TH_TH locales Reviewed-by: naoto
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     1
/*
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
     2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     4
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    10
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    15
 * accompanied this code).
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    16
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    20
 *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    23
 * questions.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    24
 */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    25
/*
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    26
 *******************************************************************************
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    27
 * Copyright (C) 2009, International Business Machines Corporation and         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    28
 * others. All Rights Reserved.                                                *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    29
 *******************************************************************************
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    30
 */
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    31
package sun.util.locale;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    32
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    33
public class StringTokenIterator {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    34
    private String text;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    35
    private String dlms;        // null if a single char delimiter
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    36
    private char delimiterChar; // delimiter if a single char delimiter
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    37
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    38
    private String token;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    39
    private int start;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    40
    private int end;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    41
    private boolean done;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    42
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    43
    public StringTokenIterator(String text, String dlms) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    44
        this.text = text;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    45
        if (dlms.length() == 1) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    46
            delimiterChar = dlms.charAt(0);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    47
        } else {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    48
            this.dlms = dlms;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    49
        }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    50
        setStart(0);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    51
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    52
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    53
    public String first() {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    54
        setStart(0);
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    55
        return token;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    56
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    57
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    58
    public String current() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    59
        return token;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    60
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    61
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    62
    public int currentStart() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    63
        return start;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    64
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    65
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    66
    public int currentEnd() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    67
        return end;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    68
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    69
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    70
    public boolean isDone() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    71
        return done;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    72
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    73
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    74
    public String next() {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    75
        if (hasNext()) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    76
            start = end + 1;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    77
            end = nextDelimiter(start);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    78
            token = text.substring(start, end);
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    79
        } else {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    80
            start = end;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    81
            token = null;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    82
            done = true;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    83
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    84
        return token;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    85
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    86
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    87
    public boolean hasNext() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    88
        return (end < text.length());
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    89
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    90
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    91
    public StringTokenIterator setStart(int offset) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    92
        if (offset > text.length()) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    93
            throw new IndexOutOfBoundsException();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    94
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    95
        start = offset;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    96
        end = nextDelimiter(start);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    97
        token = text.substring(start, end);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
    98
        done = false;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
    99
        return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   100
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   101
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   102
    public StringTokenIterator setText(String text) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   103
        this.text = text;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   104
        setStart(0);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   105
        return this;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   106
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   107
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   108
    private int nextDelimiter(int start) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   109
        int textlen = this.text.length();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   110
        if (dlms == null) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   111
            for (int idx = start; idx < textlen; idx++) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   112
                if (text.charAt(idx) == delimiterChar) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   113
                    return idx;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   114
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   115
            }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   116
        } else {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   117
            int dlmslen = dlms.length();
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   118
            for (int idx = start; idx < textlen; idx++) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   119
                char c = text.charAt(idx);
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   120
                for (int i = 0; i < dlmslen; i++) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   121
                    if (c == dlms.charAt(i)) {
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   122
                        return idx;
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   123
                    }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   124
                }
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   125
            }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   126
        }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6501
diff changeset
   127
        return textlen;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   128
    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents:
diff changeset
   129
}