1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2 // Distributable under LGPL license. See terms of license at gnu.org.
3 package nl.justobjects.pushlet.util;
4 
5 import java.io.File;
6 import java.io.FileOutputStream;
7 import java.io.PrintWriter;
8 import java.util.Random;
9 
10/**
11 * Randomizing routines.
12 *
13 * @author Just van den Broecke
14 * @version $Id: Rand.java,v 1.4 2007/12/07 12:57:40 justb Exp $
15 */
16public class Rand {
17    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'};
18    private static char NON_VOWELS[] = {'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'z'};
19    private static char VOWELS[] = {'a', 'e', 'i', 'o', 'u', 'y'};
20    private static Random random = new Random();
21
22    public static char randomChar() {
23        return CHARS[randomInt(0, CHARS.length - 1)];
24    }
25
26    public static char randomVowel() {
27        return VOWELS[randomInt(0, VOWELS.length - 1)];
28    }
29
30    public static char randomNonVowel() {
31        return NON_VOWELS[randomInt(0, NON_VOWELS.length - 1)];
32    }
33
34    public static File randomTempDir() throws Exception {
35        File file = new File(System.getProperty("java.io.tmpdir") + File.separator + "oasetest" + File.separator + randomString(12));
36        file.mkdirs();
37        file.deleteOnExit();
38        return file;
39    }
40
41    public static File randomTempFile() throws Exception {
42        File file = new File(System.getProperty("java.io.tmpdir") + File.separator + "oase-" + randomString(6));
43        file.createNewFile();
44        file.deleteOnExit();
45        return file;
46    }
47
48    public static File randomBinaryFile(int aSize) throws Exception {
49        File file = randomTempFile();
50        FileOutputStream fos = new FileOutputStream(file);
51        fos.write(randomBytes(aSize));
52        fos.close();
53        return file;
54    }
55
56    public static File randomTextFile(int aSize) throws Exception {
57        File file = randomTempFile();
58        PrintWriter pw = new PrintWriter(new FileOutputStream(file));
59        pw.write(randomString(aSize));
60        pw.close();
61        return file;
62    }
63
64    public static byte[] randomBytes(int aSize) {
65        return randomBlob(aSize);
66    }
67
68    public static byte[] randomBlob(int aSize) {
69        byte[] retval = new byte[aSize];
70        for (int i = 0; i < retval.length; i++) {
71            retval[i] = randomByte();
72        }
73        return retval;
74    }
75
76    public static byte randomByte() {
77        return (byte) random.nextInt();
78    }
79
80    public static double randomDouble() {
81        return random.nextLong();
82    }
83
84
85    public static int randomInt() {
86        return random.nextInt();
87    }
88
89    public static int randomInt(int min, int max) {
90        return (int) ((Math.random() * (double) (max + 1 - min)) + min);
91    }
92
93    public static long randomLong() {
94        return random.nextLong();
95    }
96
97    public static long randomLong(long min, long max) {
98        return (long) ((Math.random() * (double) (max + 1L - min)) + min);
99    }
00
01    public static String randomName(int aLength) {
02        StringBuffer sb = new StringBuffer(aLength);
03        for (int i = 0; i < aLength; i++) {
04            sb.append(i % 2 == 0 ? randomNonVowel() : randomVowel());
05        }
06        return sb.toString();
07    }
08
09    public static void randomSleep(long min, long max) {
10        try {
11            Thread.sleep(randomLong(min, max));
12        } catch (InterruptedException ie) {
13
14        }
15    }
16
17    public static String randomString(int aLength) {
18        StringBuffer sb = new StringBuffer(aLength);
19        for (int i = 0; i < aLength; i++) {
20            sb.append(randomChar());
21        }
22        return sb.toString();
23    }
24
25    public static String randomString() {
26        return "" + randomLong();
27    }
28
29
30}
31
32/*
33 * $Log: Rand.java,v $
34 * Revision 1.4  2007/12/07 12:57:40  justb
35 * added log4j and make it the default logging method
36 *
37 * Revision 1.3  2007/11/23 21:10:17  justb
38 * add hooks for custom logging (you can override DefaultLogger in pushlet.properties)
39 *
40 * Revision 1.2  2004/09/03 22:35:38  justb
41 * Almost complete rewrite, just checking in now
42 *
43 * Revision 1.1  2004/03/10 12:21:27  justb
44 * *** empty log message ***
45 *
46 *
47 */