KickJava   Java API By Example, From Geeks To Geeks.

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


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 class RandomTextProvider {
25
26     private static final RandomCharDescriptor[] EMPTY_DESCRIPTORS = {};
27
28     private static final FixedTextDescriptor[] EMPTY_FIXED_TEXTS = {};
29
30     private RandomCharDescriptor[] randomCharDescriptors;
31
32     private FixedTextDescriptor[] fixedTexts;
33
34     private double ratioSum;
35
36     private double fixedTextsRatioSum;
37
38     public RandomTextProvider(RandomCharDescriptor[] randomCharDescriptors) {
39         this(randomCharDescriptors, null);
40     }
41
42     public RandomTextProvider(RandomCharDescriptor[] randomCharDescriptors,
43     FixedTextDescriptor[] fixedTexts) {
44         if (randomCharDescriptors == null) {
45             randomCharDescriptors = EMPTY_DESCRIPTORS;
46         }
47         if (fixedTexts == null) {
48             fixedTexts = EMPTY_FIXED_TEXTS;
49         }
50         this.randomCharDescriptors = randomCharDescriptors;
51         this.fixedTexts = fixedTexts;
52         
53         // Compute sum of ratios of all random char descriptors
54
for (int i = 0; i < randomCharDescriptors.length; i++) {
55             ratioSum += randomCharDescriptors[i].ratio();
56         }
57         for (int i = 0; i < fixedTexts.length; i++) {
58             fixedTextsRatioSum += fixedTexts[i].ratio();
59         }
60     }
61     
62     public char randomChar(Random JavaDoc random) {
63         double r = random.nextDouble() * ratioSum;
64         for (int i = 0; i < randomCharDescriptors.length; i++) {
65             RandomCharDescriptor descriptor = randomCharDescriptors[i];
66             if ((r -= descriptor.ratio()) < 0) {
67                 return descriptor.randomChar(random);
68             }
69         }
70         // Internal error - randomCharAvailable() needs to be checked
71
throw new IllegalStateException JavaDoc("No random char descriptions available");
72     }
73     
74     public boolean randomCharAvailable() {
75         return (randomCharDescriptors.length > 0);
76     }
77     
78     /**
79      *
80      * @return non-empty random string with length less or equal to maxTextLength.
81      */

82     public String JavaDoc randomText(Random JavaDoc random, int maxTextLength) {
83         if (randomCharAvailable()) {
84             int len = random.nextInt(maxTextLength);
85             StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
86             while (--len >= 0) {
87                 sb.append(randomChar(random));
88             }
89             return sb.toString();
90         } else {
91             return "";
92         }
93     }
94     
95     public String JavaDoc randomFixedText(Random JavaDoc random) {
96         double r = random.nextDouble() * fixedTextsRatioSum;
97         for (int i = 0; i < fixedTexts.length; i++) {
98             FixedTextDescriptor fixedText = fixedTexts[i];
99             if ((r -= fixedText.ratio()) < 0) {
100                 return fixedText.text();
101             }
102         }
103         return ""; // no fixed texts available
104
}
105
106 }
Popular Tags