KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > test > RandomCharDescriptor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.lexer.test;
21
22 import java.util.Random JavaDoc;
23
24 public final class RandomCharDescriptor {
25
26     public static RandomCharDescriptor singleChar(char ch, double ratio) {
27         return new RandomCharDescriptor(new char[] {ch}, ratio);
28     }
29
30     public static RandomCharDescriptor charRange(char firstChar, char lastChar, double ratio) {
31         char[] chars = new char[lastChar - firstChar + 1];
32         int index = 0;
33         while (firstChar <= lastChar) {
34             chars[index++] = firstChar++;
35         }
36         return new RandomCharDescriptor(chars, ratio);
37     }
38     
39     public static RandomCharDescriptor chars(char[] chars, double ratio) {
40         return new RandomCharDescriptor(chars, ratio);
41     }
42
43     public static RandomCharDescriptor accepted(Acceptor acceptor, double ratio) {
44         return new RandomCharDescriptor(acceptedChars(acceptor), ratio);
45     }
46     
47
48     public static RandomCharDescriptor union(RandomCharDescriptor descriptor1,
49     RandomCharDescriptor descriptor2, double ratio) {
50         return new RandomCharDescriptor(mergeChars(descriptor1.chars, descriptor2.chars), ratio);
51     }
52
53     public static RandomCharDescriptor digit(double ratio) {
54         return charRange('0', '9', ratio);
55     }
56     
57     public static RandomCharDescriptor letter(double ratio) {
58         return union(letterLowercase(ratio), letterUppercase(ratio), ratio);
59     }
60     
61     public static RandomCharDescriptor letterLowercase(double ratio) {
62         return charRange('a', 'z', ratio);
63     }
64     
65     public static RandomCharDescriptor letterUppercase(double ratio) {
66         return charRange('A', 'Z', ratio);
67     }
68     
69     public static RandomCharDescriptor space(double ratio) {
70         return singleChar(' ', ratio);
71     }
72     
73     public static RandomCharDescriptor cr(double ratio) {
74         return singleChar('\r', ratio);
75     }
76     
77     public static RandomCharDescriptor lf(double ratio) {
78         return singleChar('\n', ratio);
79     }
80     
81     public static RandomCharDescriptor whitespace(double ratio) {
82         return chars(new char[] { ' ', '\t', '\r', '\n' }, ratio);
83     }
84     
85     public static RandomCharDescriptor letterUnicode(double ratio) {
86         return accepted(new Acceptor() {
87             public boolean isAccepted(char ch) {
88                 return Character.isLetter(ch);
89             }
90         }, ratio);
91     }
92     
93     public static RandomCharDescriptor digitUnicode(double ratio) {
94         return accepted(new Acceptor() {
95             public boolean isAccepted(char ch) {
96                 return Character.isDigit(ch);
97             }
98         }, ratio);
99     }
100
101     public static RandomCharDescriptor whitespaceUnicode(double ratio) {
102         return accepted(new Acceptor() {
103             public boolean isAccepted(char ch) {
104                 return Character.isWhitespace(ch);
105             }
106         }, ratio);
107     }
108     
109     public static RandomCharDescriptor javaIdentifierStart(double ratio) {
110         return accepted(new Acceptor() {
111             public boolean isAccepted(char ch) {
112                 return Character.isJavaIdentifierStart(ch);
113             }
114         }, ratio);
115     }
116
117     public static RandomCharDescriptor javaIdentifierPart(double ratio) {
118         return accepted(new Acceptor() {
119             public boolean isAccepted(char ch) {
120                 return Character.isJavaIdentifierPart(ch);
121             }
122         }, ratio);
123     }
124
125     public static RandomCharDescriptor anyChar(double ratio) {
126         return accepted(new Acceptor() {
127             public boolean isAccepted(char ch) {
128                 return true;
129             }
130         }, ratio);
131     }
132
133     private char[] chars;
134     
135     private double ratio;
136     
137     private RandomCharDescriptor(char[] chars, double ratio) {
138         this.chars = chars;
139         this.ratio = ratio;
140     }
141     
142     /**
143      * Return random char from the chars defined in this descriptor.
144      */

145     public char randomChar(Random JavaDoc random) {
146         return chars[random.nextInt(chars.length)];
147     }
148
149     public double ratio() {
150         return ratio;
151     }
152     
153     private static char[] mergeChars(char[] chars1, char[] chars2) {
154         char[] chars = new char[chars1.length + chars2.length];
155         System.arraycopy(chars1, 0, chars, 0, chars1.length);
156         System.arraycopy(chars2, 0, chars, chars1.length, chars2.length);
157         return chars;
158     }
159     
160     private static char[] acceptedChars(Acceptor acceptor) {
161         int count = 0;
162         // Cannot use for(;;) 0 <= ch <= Character.MAX_VALUE => would never break
163
char ch = 0;
164         do {
165             if (acceptor.isAccepted(ch)) {
166                 count++;
167             }
168             ch++;
169         } while (ch != 0);
170
171         char chars[] = new char[count];
172         count = 0;
173         ch = 0;
174         do {
175             if (acceptor.isAccepted(ch)) {
176                 chars[count++] = ch;
177             }
178             ch++;
179         } while (ch != 0);
180
181         return chars;
182     }
183
184     public interface Acceptor {
185         
186         boolean isAccepted(char ch);
187
188     }
189     
190 }
Popular Tags