View Javadoc

1   /*
2    ORG Usurper is a random value object generator library 
3    Copyright (C) 2007  Pierre-Antoine Grégoire
4    
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9    
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package org.org.usurper.utils;
20  
21  import java.util.Date;
22  
23  /**
24   * The Class RandomUtils provides a series of simple Random generators.<br>
25   * It is freely and obviously inspired by code from commons-lang.
26   */
27  public class RandomUtils {
28  
29      @SuppressWarnings("unused")
30      private RandomUtils() {
31      }
32  
33      /**
34       * Next boolean.
35       * 
36       * @return the boolean
37       */
38      public static Boolean nextBoolean() {
39          return Boolean.valueOf(Math.random() > 0.5);
40      }
41  
42      /**
43       * Next byte.
44       * 
45       * @return the byte
46       */
47      public static Byte nextByte() {
48          return Byte.valueOf(("" + Math.random() * Integer.MAX_VALUE).substring(0, 1));
49      }
50  
51      public static Integer nextInteger() {
52          return Integer.valueOf((int) Math.random() * Integer.MAX_VALUE);
53      }
54  
55      /**
56       * Next character.
57       * 
58       * @return the character
59       */
60      public static Character nextCharacter() {
61          int end = 'z' + 1;
62          int start = ' ';
63  
64          StringBuilder buffer = new StringBuilder();
65          int gap = end - start;
66  
67          buffer.append((char) ((int) (Math.random() * gap) + start));
68          return Character.valueOf((buffer.toString().charAt(0)));
69      }
70  
71      /**
72       * Next character.
73       * 
74       * @return the character
75       */
76      public static String nextString(Integer length) {
77          int end = 'z' + 1;
78          int start = ' ';
79  
80          StringBuilder buffer = new StringBuilder();
81          int gap = end - start;
82          for (int i = 0; i < length; i++) {
83              char ch = (char) ((int) (Math.random() * gap) + start);
84              if (Character.isLetter(ch)) {
85                  buffer.append(ch);
86              } else {
87                  i--;
88              }
89          }
90  
91          return buffer.toString();
92      }
93  
94      public static Long nextLong() {
95          return Long.valueOf((long) (Math.random() * Long.MAX_VALUE));
96      }
97  
98      public static Date nextDate() {
99          long date = RandomUtils.nextLong();
100         long maxDate = new Date().getTime();
101         return new Date(date > maxDate ? maxDate : date);
102     }
103 
104     public static Short nextShort() {
105         return Short.valueOf((short) (Math.random() * Short.MAX_VALUE));
106     }
107 
108     public static Double nextDouble() {
109         return Double.valueOf((double) (Math.random() * Double.MAX_VALUE));
110     }
111 }