KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > justobjects > pushlet > util > Rand


1 package nl.justobjects.pushlet.util;
2
3 import java.io.File JavaDoc;
4 import java.io.FileOutputStream JavaDoc;
5 import java.io.PrintWriter JavaDoc;
6 import java.util.Random JavaDoc;
7
8 /**
9  * Randomizing routines.
10  *
11  * @version $Id: Rand.java,v 1.2 2004/09/03 22:35:38 justb Exp $
12  * @author Just van den Broecke
13  */

14 public class Rand {
15     private static char CHARS[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'y', 'z'};
16     private static char NON_VOWELS[] = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'z'};
17     private static char VOWELS[] = {'a', 'e', 'i', 'o', 'u', 'y'};
18     private static Random JavaDoc random = new Random JavaDoc();
19
20     public static char randomChar() {
21         return CHARS[randomInt(0, CHARS.length - 1)];
22     }
23
24     public static char randomVowel() {
25         return VOWELS[randomInt(0, VOWELS.length - 1)];
26     }
27
28     public static char randomNonVowel() {
29         return NON_VOWELS[randomInt(0, NON_VOWELS.length - 1)];
30     }
31
32     public static File JavaDoc randomTempDir() throws Exception JavaDoc {
33         File JavaDoc file = new File JavaDoc(System.getProperty("java.io.tmpdir") + File.separator + "oasetest" + File.separator + randomString(12));
34         file.mkdirs();
35         file.deleteOnExit();
36         return file;
37     }
38
39     public static File JavaDoc randomTempFile() throws Exception JavaDoc {
40         File JavaDoc file = new File JavaDoc(System.getProperty("java.io.tmpdir") + File.separator + "oase-" + randomString(6));
41         file.createNewFile();
42         file.deleteOnExit();
43         return file;
44     }
45
46     public static File JavaDoc randomBinaryFile(int aSize) throws Exception JavaDoc {
47         File JavaDoc file = randomTempFile();
48         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file);
49         fos.write(randomBytes(aSize));
50         fos.close();
51         return file;
52     }
53
54     public static File JavaDoc randomTextFile(int aSize) throws Exception JavaDoc {
55         File JavaDoc file = randomTempFile();
56         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(new FileOutputStream JavaDoc(file));
57         pw.write(randomString(aSize));
58         pw.close();
59         return file;
60     }
61
62     public static byte[] randomBytes(int aSize) {
63         return randomBlob(aSize);
64     }
65
66     public static byte[] randomBlob(int aSize) {
67         byte[] retval = new byte[aSize];
68         for (int i = 0; i < retval.length; i++) {
69             retval[i] = randomByte();
70         }
71         return retval;
72     }
73
74     public static byte randomByte() {
75         return (byte) random.nextInt();
76     }
77
78     public static double randomDouble() {
79         return random.nextLong();
80     }
81
82
83     public static int randomInt() {
84         return random.nextInt();
85     }
86
87     public static int randomInt(int min, int max) {
88         return (int) ((Math.random() * (double) (max + 1 - min)) + min);
89     }
90
91     public static long randomLong() {
92         return random.nextLong();
93     }
94
95     public static long randomLong(long min, long max) {
96         return (long) ((Math.random() * (double) (max + 1L - min)) + min);
97     }
98
99     public static String JavaDoc randomName(int aLength) {
100         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(aLength);
101         for (int i = 0; i < aLength; i++) {
102             sb.append(i % 2 == 0 ? randomNonVowel() : randomVowel());
103         }
104         return sb.toString();
105     }
106
107     public static void randomSleep(long min, long max) {
108         try {
109             Thread.sleep(randomLong(min, max));
110         } catch (InterruptedException JavaDoc ie) {
111
112         }
113     }
114
115     public static String JavaDoc randomString(int aLength) {
116         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(aLength);
117         for (int i = 0; i < aLength; i++) {
118             sb.append(randomChar());
119         }
120         return sb.toString();
121     }
122
123     public static String JavaDoc randomString() {
124         return "" + randomLong();
125     }
126
127
128 }
129
130 /*
131  * $Log: Rand.java,v $
132  * Revision 1.2 2004/09/03 22:35:38 justb
133  * Almost complete rewrite, just checking in now
134  *
135  * Revision 1.1 2004/03/10 12:21:27 justb
136  * *** empty log message ***
137  *
138  *
139  */
Popular Tags