001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.util.streams.CharAlphabet
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.util.streams;
023:
024: /**
025: * A looping alphabet, returning characters.
026: *
027: * The alphabet loops over a list of characters. The alphabet-object is used
028: * by looping readers, which in turn is used for testing methods requiring
029: * streaming inputs.
030: *
031: * The following alphabets have been defined:
032: * <ul><li><em>Modern latin, lowercase</em> ; letters a - z (26)
033: * <li><em>Norwegian/Danish, lowercase</em> ; letters a - z, plus three
034: * additional letters (29)
035: * <li><em>Tamil</em> ; 46 Tamil letters from UNICODE U0B80
036: * <li><em>CJK subset</em> ; 12 letter from UNICODE CJK U4E00
037: * </ul>
038: */
039: public class CharAlphabet {
040:
041: /** Modern latin, lowercase; a - z, 26 letters */
042: public static char[] MODERNLATINLOWER = { 'a', 'b', 'c', 'd', 'e',
043: 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
044: 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
045:
046: /** Norwegian/Danish alphabet, lowercase; 29 letters */
047: public static char[] NO_DK_LOWER = { 'a', 'b', 'c', 'd', 'e', 'f',
048: 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
049: 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\u00E6', '\u00F8',
050: '\u00E5' };
051:
052: /** Subset of Tamil alphabet; 46 letters, UNICODE U0B80 */
053: public static char[] TAMIL = { '\u0B85', '\u0B86', '\u0B87',
054: '\u0B88', '\u0B89', '\u0B8A', '\u0B8E', '\u0B8F', '\u0B90',
055: '\u0B92', '\u0B93', '\u0B94', '\u0B95', '\u0B99', '\u0B9A',
056: '\u0B9C', '\u0B9E', '\u0B9F', '\u0BA3', '\u0BA4', '\u0BA8',
057: '\u0BA9', '\u0BAA', '\u0BAE', '\u0BAF', '\u0BB0', '\u0BB1',
058: '\u0BB2', '\u0BB3', '\u0BB4', '\u0BB5', '\u0BB6', '\u0BB7',
059: '\u0BB8', '\u0BB9', '\u0BBE', '\u0BBF', '\u0BC0', '\u0BC1',
060: '\u0BC2', '\u0BC6', '\u0BC7', '\u0BC8', '\u0BCA', '\u0BCB',
061: '\u0BCC' };
062:
063: /** CJK subset; 12 letters, UNICODE U4E00 */
064: public static char[] CJKSUBSET = { '\u4E00', '\u4E01', '\u4E02',
065: '\u4E03', '\u4E04', '\u4E05', '\u4E06', '\u4E07', '\u4E08',
066: '\u4E09', '\u4E0A', '\u4E0B' };
067:
068: /**
069: * Get a modern latin lowercase alphabet.
070: */
071: public static CharAlphabet modernLatinLowercase() {
072: return new CharAlphabet("Modern latin lowercase",
073: CharAlphabet.MODERNLATINLOWER);
074: }
075:
076: /**
077: * Get a CJK subset alphabet.
078: */
079: public static CharAlphabet cjkSubset() {
080: return new CharAlphabet("CJK subset", CharAlphabet.CJKSUBSET);
081: }
082:
083: /** Name of the alphabet. */
084: private final String name;
085: /** Characters in the alphabet. */
086: private final char[] chars;
087: /** Number of characters in the alphabet. */
088: private final int charCount;
089: /** Current offset into the alphabet/character array. */
090: private int off = 0;
091:
092: /**
093: * Create an alphabet with the given name and characters.
094: *
095: * @param name name of the alphabet
096: * @param chars characters in the alphabet.
097: */
098: private CharAlphabet(String name, char[] chars) {
099: this .name = name;
100: this .chars = chars;
101: this .charCount = chars.length;
102: }
103:
104: /**
105: * Return the name of the alphabet.
106: */
107: public String getName() {
108: return this .name;
109: }
110:
111: /**
112: * Return the number of characters in the alphabet.
113: */
114: public int charCount() {
115: return this .charCount;
116: }
117:
118: /**
119: * Return the next char as an <code>integer</code>.
120: *
121: * @return the next character in the alphabet as an <code>integer</code>
122: */
123: public int nextCharAsInt() {
124: if (off >= charCount) {
125: off = 0;
126: }
127: return (int) chars[off++];
128: }
129:
130: /**
131: * Return the next char.
132: *
133: * @return the next character in the alphabet
134: */
135: public char nextChar() {
136: if (off >= charCount) {
137: off = 0;
138: }
139: return chars[off++];
140: }
141:
142: /**
143: * Compute the next character to read after reading the specified number
144: * of characters.
145: *
146: * Besides from returning the index, the internal state of
147: * the alphabet is updated.
148: *
149: * @param charsRead the number of characters read
150: * @return the index of the next character
151: */
152: public int nextCharToRead(int charsRead) {
153: off = (off + (charsRead % charCount)) % charCount;
154: return off;
155: }
156:
157: /**
158: * Reset the alphabet, the next character returned will be the first
159: * character in the alphabet.
160: */
161: public void reset() {
162: off = 0;
163: }
164: } // Enc class CharAlphabet
|