Coverage Report - org.org.usurper.utils.RandomUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
RandomUtils
93 %
25/27
88 %
7/8
0
 
 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  0
     private RandomUtils() {
 31  0
     }
 32  
 
 33  
     /**
 34  
      * Next boolean.
 35  
      * 
 36  
      * @return the boolean
 37  
      */
 38  
     public static Boolean nextBoolean() {
 39  767
         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  285
         return Byte.valueOf(("" + Math.random() * Integer.MAX_VALUE).substring(0, 1));
 49  
     }
 50  
 
 51  
     public static Integer nextInteger() {
 52  1663
         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  285
         int end = 'z' + 1;
 62  285
         int start = ' ';
 63  
 
 64  285
         StringBuilder buffer = new StringBuilder();
 65  285
         int gap = end - start;
 66  
 
 67  285
         buffer.append((char) ((int) (Math.random() * gap) + start));
 68  285
         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  712
         int end = 'z' + 1;
 78  712
         int start = ' ';
 79  
 
 80  712
         StringBuilder buffer = new StringBuilder();
 81  712
         int gap = end - start;
 82  13258
         for (int i = 0; i < length; i++) {
 83  12546
             char ch = (char) ((int) (Math.random() * gap) + start);
 84  12546
             if (Character.isLetter(ch)) {
 85  7120
                 buffer.append(ch);
 86  
             } else {
 87  5426
                 i--;
 88  
             }
 89  
         }
 90  
 
 91  712
         return buffer.toString();
 92  
     }
 93  
 
 94  
     public static Long nextLong() {
 95  1668
         return Long.valueOf((long) (Math.random() * Long.MAX_VALUE));
 96  
     }
 97  
 
 98  
     public static Date nextDate() {
 99  904
         long date = RandomUtils.nextLong();
 100  904
         long maxDate = new Date().getTime();
 101  904
         return new Date(date > maxDate ? maxDate : date);
 102  
     }
 103  
 
 104  
     public static Short nextShort() {
 105  764
         return Short.valueOf((short) (Math.random() * Short.MAX_VALUE));
 106  
     }
 107  
 
 108  
     public static Double nextDouble() {
 109  802
         return Double.valueOf((double) (Math.random() * Double.MAX_VALUE));
 110  
     }
 111  
 }