~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to j2me/java/util/StringTokenizer.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package java.util;
 
2
 
 
3
import java.util.Enumeration;
 
4
import java.util.NoSuchElementException;
 
5
 
 
6
public class StringTokenizer
 
7
    implements Enumeration
 
8
{
 
9
    private String s;
 
10
    private String delims;
 
11
    private boolean retDelims;
 
12
    private int     maxPos;
 
13
 
 
14
    private int pos;
 
15
 
 
16
    public StringTokenizer(String s, String delims)
 
17
    {
 
18
        this(s, delims, false);
 
19
    }
 
20
 
 
21
    public StringTokenizer(String s, String delims, boolean retDelims)
 
22
    {
 
23
        this.s = s;
 
24
        this.delims = delims;
 
25
        this.retDelims = retDelims;
 
26
        this.maxPos = s.length();
 
27
    }
 
28
 
 
29
    public boolean hasMoreTokens()
 
30
    {
 
31
        if (retDelims)
 
32
        {
 
33
            return pos < maxPos; 
 
34
        }
 
35
        else
 
36
        {
 
37
            int next = pos;
 
38
            while (next < maxPos && isDelim(next))
 
39
            {
 
40
                next++;
 
41
            }
 
42
 
 
43
            return next < maxPos;
 
44
        }
 
45
    }
 
46
 
 
47
    public String nextToken()
 
48
    {
 
49
        String tok;
 
50
 
 
51
        if (pos == maxPos)
 
52
        {
 
53
            throw new NoSuchElementException("no more tokens");
 
54
        }
 
55
 
 
56
        if (retDelims)
 
57
        {
 
58
            if (isDelim(pos))
 
59
            {
 
60
                tok = s.substring(pos, pos + 1);
 
61
                pos++;
 
62
 
 
63
                return tok;
 
64
            }
 
65
        }
 
66
 
 
67
        while (pos < maxPos && isDelim(pos))
 
68
        {
 
69
            pos++;
 
70
        }
 
71
 
 
72
        int start = pos;
 
73
        
 
74
        while (pos < maxPos && !isDelim(pos))
 
75
        {
 
76
            pos++;
 
77
        }
 
78
 
 
79
        if (pos < maxPos)
 
80
        {
 
81
            tok = s.substring(start, pos);
 
82
        }
 
83
        else
 
84
        {
 
85
            tok = s.substring(start);
 
86
        }
 
87
 
 
88
        return tok;
 
89
    }
 
90
 
 
91
    public boolean hasMoreElements()
 
92
    {
 
93
        return hasMoreTokens();
 
94
    }
 
95
 
 
96
    public Object nextElement()
 
97
    {
 
98
        return nextToken();
 
99
    }
 
100
 
 
101
    private boolean isDelim(int index)
 
102
    {
 
103
        char c = s.charAt(index);
 
104
 
 
105
        for (int i = 0; i != delims.length(); i++)
 
106
        {
 
107
            if (delims.charAt(i) == c)
 
108
            {
 
109
                return true;
 
110
            }
 
111
        }
 
112
 
 
113
        return false;
 
114
    }
 
115
}