KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > streams > CharAlphabet


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.util.streams.CharAlphabet
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.util.streams;
23
24 /**
25  * A looping alphabet, returning characters.
26  *
27  * The alphabet loops over a list of characters. The alphabet-object is used
28  * by looping readers, which in turn is used for testing methods requiring
29  * streaming inputs.
30  *
31  * The following alphabets have been defined:
32  * <ul><li><em>Modern latin, lowercase</em> ; letters a - z (26)
33  * <li><em>Norwegian/Danish, lowercase</em> ; letters a - z, plus three
34  * additional letters (29)
35  * <li><em>Tamil</em> ; 46 Tamil letters from UNICODE U0B80
36  * <li><em>CJK subset</em> ; 12 letter from UNICODE CJK U4E00
37  * </ul>
38  */

39 public class CharAlphabet {
40     
41     /** Modern latin, lowercase; a - z, 26 letters */
42     public static char[] MODERNLATINLOWER = {
43             'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
44             'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
45         };
46
47     /** Norwegian/Danish alphabet, lowercase; 29 letters */
48     public static char[] NO_DK_LOWER = {
49             'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
50             'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
51             '\u00E6', '\u00F8', '\u00E5'
52         };
53
54     /** Subset of Tamil alphabet; 46 letters, UNICODE U0B80 */
55     public static char[] TAMIL = {
56             '\u0B85', '\u0B86', '\u0B87', '\u0B88', '\u0B89', '\u0B8A',
57             '\u0B8E', '\u0B8F', '\u0B90', '\u0B92', '\u0B93', '\u0B94',
58             '\u0B95', '\u0B99', '\u0B9A', '\u0B9C', '\u0B9E', '\u0B9F',
59             '\u0BA3', '\u0BA4', '\u0BA8', '\u0BA9', '\u0BAA', '\u0BAE',
60             '\u0BAF', '\u0BB0', '\u0BB1', '\u0BB2', '\u0BB3', '\u0BB4',
61             '\u0BB5', '\u0BB6', '\u0BB7', '\u0BB8', '\u0BB9', '\u0BBE',
62             '\u0BBF', '\u0BC0', '\u0BC1', '\u0BC2', '\u0BC6', '\u0BC7',
63             '\u0BC8', '\u0BCA', '\u0BCB', '\u0BCC'
64         };
65
66     /** CJK subset; 12 letters, UNICODE U4E00 */
67     public static char[] CJKSUBSET = {
68             '\u4E00', '\u4E01', '\u4E02', '\u4E03', '\u4E04', '\u4E05',
69             '\u4E06', '\u4E07', '\u4E08', '\u4E09', '\u4E0A', '\u4E0B'
70         };
71
72     /**
73      * Get a modern latin lowercase alphabet.
74      */

75     public static CharAlphabet modernLatinLowercase() {
76         return new CharAlphabet("Modern latin lowercase",
77                                 CharAlphabet.MODERNLATINLOWER);
78     }
79
80     /**
81      * Get a CJK subset alphabet.
82      */

83     public static CharAlphabet cjkSubset() {
84         return new CharAlphabet("CJK subset",
85                                 CharAlphabet.CJKSUBSET);
86     }
87
88     /** Name of the alphabet. */
89     private final String JavaDoc name;
90     /** Characters in the alphabet. */
91     private final char[] chars;
92     /** Number of characters in the alphabet. */
93     private final int charCount;
94     /** Current offset into the alphabet/character array. */
95     private int off = 0;
96     
97     /**
98      * Create an alphabet with the given name and characters.
99      *
100      * @param name name of the alphabet
101      * @param chars characters in the alphabet.
102      */

103     private CharAlphabet(String JavaDoc name, char[] chars) {
104         this.name = name;
105         this.chars = chars;
106         this.charCount = chars.length;
107     }
108
109     /**
110      * Return the name of the alphabet.
111      */

112     public String JavaDoc getName() {
113         return this.name;
114     }
115
116     /**
117      * Return the number of characters in the alphabet.
118      */

119     public int charCount() {
120         return this.charCount;
121     }
122
123     /**
124      * Return the next char as an <code>integer</code>.
125      *
126      * @return the next character in the alphabet as an <code>integer</code>
127      */

128     public int nextCharAsInt() {
129         if (off >= charCount) {
130             off = 0;
131         }
132         return (int)chars[off++];
133     }
134
135     /**
136      * Return the next char.
137      *
138      * @return the next character in the alphabet
139      */

140     public char nextChar() {
141         if (off >= charCount) {
142             off = 0;
143         }
144         return chars[off++];
145     }
146
147     /**
148      * Compute the next character to read after reading the specified number
149      * of characters.
150      *
151      * Besides from returning the index, the internal state of
152      * the alphabet is updated.
153      *
154      * @param charsRead the number of characters read
155      * @return the index of the next character
156      */

157     public int nextCharToRead(int charsRead) {
158         off = (off + (charsRead % charCount)) % charCount;
159         return off;
160     }
161
162     /**
163      * Reset the alphabet, the next character returned will be the first
164      * character in the alphabet.
165      */

166     public void reset() {
167         off = 0;
168     }
169 } // Enc class CharAlphabet
170
Popular Tags