File: CharAlphabet.java

package info (click to toggle)
derby 10.14.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 78,896 kB
  • sloc: java: 691,930; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 60
file content (200 lines) | stat: -rw-r--r-- 6,315 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*

   Derby - Class org.apache.derbyTesting.functionTests.util.streams.CharAlphabet

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to you under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

package org.apache.derbyTesting.functionTests.util.streams;

/**
 * A looping alphabet, returning characters.
 *
 * The alphabet loops over a list of characters. The alphabet-object is used
 * by looping readers, which in turn is used for testing methods requiring
 * streaming inputs.
 *
 * The following alphabets have been defined:
 * <ul><li><em>Modern latin, lowercase</em> ; letters a - z (26)
 *     <li><em>Norwegian/Danish, lowercase</em> ; letters a - z, plus three
 *         additional letters (29)
 *     <li><em>Tamil</em> ; 46 Tamil letters from UNICODE U0B80
 *     <li><em>CJK subset</em> ; 12 letter from UNICODE CJK U4E00 
 * </ul>
 */
public class CharAlphabet {
    
    /** Modern latin, lowercase; a - z, 26 letters */
    public static char[] MODERNLATINLOWER = {
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
        };

    /** Norwegian/Danish alphabet, lowercase; 29 letters */
    public static char[] NO_DK_LOWER = {
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            '\u00E6', '\u00F8', '\u00E5'
        };

    /** Subset of Tamil alphabet; 46 letters, UNICODE U0B80 */
    public static char[] TAMIL = {
            '\u0B85', '\u0B86', '\u0B87', '\u0B88', '\u0B89', '\u0B8A',
            '\u0B8E', '\u0B8F', '\u0B90', '\u0B92', '\u0B93', '\u0B94',
            '\u0B95', '\u0B99', '\u0B9A', '\u0B9C', '\u0B9E', '\u0B9F',
            '\u0BA3', '\u0BA4', '\u0BA8', '\u0BA9', '\u0BAA', '\u0BAE',
            '\u0BAF', '\u0BB0', '\u0BB1', '\u0BB2', '\u0BB3', '\u0BB4',
            '\u0BB5', '\u0BB6', '\u0BB7', '\u0BB8', '\u0BB9', '\u0BBE',
            '\u0BBF', '\u0BC0', '\u0BC1', '\u0BC2', '\u0BC6', '\u0BC7',
            '\u0BC8', '\u0BCA', '\u0BCB', '\u0BCC'
        };

    /** CJK subset; 12 letters, UNICODE U4E00 */
    public static char[] CJKSUBSET = {
            '\u4E00', '\u4E01', '\u4E02', '\u4E03', '\u4E04', '\u4E05',
            '\u4E06', '\u4E07', '\u4E08', '\u4E09', '\u4E0A', '\u4E0B'
        };

    /**
     * Get a modern latin lowercase alphabet.
     */
    public static CharAlphabet modernLatinLowercase() {
        return new CharAlphabet("Modern latin lowercase",
                                CharAlphabet.MODERNLATINLOWER);
    }

    /**
     * Get a CJK subset alphabet.
     */
    public static CharAlphabet cjkSubset() {
        return new CharAlphabet("CJK subset",
                                CharAlphabet.CJKSUBSET);
    }

    /**
     * Get a Tamil alphabet
     */
    public static CharAlphabet tamil() {
        return new CharAlphabet("Tamil", CharAlphabet.TAMIL);
    }

    /**
     * Get an alphabet consisting of a single character.
     */
    public static CharAlphabet singleChar(char ch) {
        return new CharAlphabet("Single char: " + ch, new char[] { ch });
    }

    /** Name of the alphabet. */
    private final String name;
    /** Characters in the alphabet. */
    private final char[] chars;
    /** Number of characters in the alphabet. */
    private final int charCount;
    /** Current offset into the alphabet/character array. */
    private int off = 0;
    
    /**
     * Create an alphabet with the given name and characters.
     *
     * @param name name of the alphabet
     * @param chars characters in the alphabet.
     */
    private CharAlphabet(String name, char[] chars) {
        this.name = name;
        this.chars = chars;
        this.charCount = chars.length;
    }

    /**
     * Return the name of the alphabet.
     */
    public String getName() {
        return this.name;
    }

    /**
     * Return the number of characters in the alphabet.
     */
    public int charCount() {
        return this.charCount;
    }

    /**
     * Return the next char as an <code>integer</code>.
     *
     * @return the next character in the alphabet as an <code>integer</code>
     */
    public int nextCharAsInt() {
        if (off >= charCount) {
            off = 0;
        }
        return (int)chars[off++];
    }

    /**
     * Return the next char.
     *
     * @return the next character in the alphabet
     */
    public char nextChar() {
        if (off >= charCount) {
            off = 0;
        }
        return chars[off++];
    }

    /**
     * Compute the next character to read after reading the specified number
     * of characters. 
     *
     * Besides from returning the index, the internal state of
     * the alphabet is updated.
     *
     * @param charsRead the number of characters read
     * @return the index of the next character
     */
    public int nextCharToRead(int charsRead) {
        off = (off + (charsRead % charCount)) % charCount;
        return off;
    }

    /**
     * Reset the alphabet, the next character returned will be the first
     * character in the alphabet.
     */
    public void reset() {
        off = 0;
    }

    /**
     * Returns a clone of the alphabet.
     *
     * @return A clone.
     */
    public CharAlphabet getClone() {
        return new CharAlphabet(name, chars);
    }

    /**
     * Returns a friendlier textual representation of the alphabet.
     */
    public String toString() {
        return (name + "@" + hashCode() + "(charCount=" + charCount + ")");
    }

} // Enc class CharAlphabet