1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  *
  26  * @summary utilities class supporting common operation for tests.
  27  * @(#)StringUtilities.java
  28  * @author Tristan Yan
  29  * @version 1.0
  30  */
  31 
  32 import java.util.Random;
  33 
  34 public class StringUtilities {
  35     private final static Random RANDOM =  new Random(System.currentTimeMillis());
  36 
  37     public static String randomString(int max_length, int min_length){
  38         return randomAscii(min_length + RANDOM.nextInt(max_length - min_length));
  39     }
  40 
  41     public static String random(int count, int start, int end, boolean letters,
  42             boolean numbers, char[] chars, Random rnd) {
  43         if (count == 0) {
  44             return "";
  45         } else if (count < 0) {
  46             throw new IllegalArgumentException("Requested random string length " + count + " is less than 0.");
  47         }
  48         if ((start == 0) && (end == 0)) {
  49             end = 'z' + 1;
  50             start = ' ';
  51             if (!letters && !numbers) {
  52                 start = 0;
  53                 end = Integer.MAX_VALUE;
  54             }
  55         }
  56 
  57         char[] buffer = new char[count];
  58         int gap = end - start;
  59 
  60         while (count-- != 0) {
  61             char ch;
  62             if (chars == null) {
  63                 ch = (char) (rnd.nextInt(gap) + start);
  64             } else {
  65                 ch = chars[rnd.nextInt(gap) + start];
  66             }
  67             if ((letters && Character.isLetter(ch))
  68                 || (numbers && Character.isDigit(ch))
  69                 || (!letters && !numbers))
  70             {
  71                 if(ch >= 56320 && ch <= 57343) {
  72                     if(count == 0) {
  73                         count++;
  74                     } else {
  75                         // low surrogate, insert high surrogate after putting it in
  76                         buffer[count] = ch;
  77                         count--;
  78                         buffer[count] = (char) (55296 + rnd.nextInt(128));
  79                     }
  80                 } else if(ch >= 55296 && ch <= 56191) {
  81                     if(count == 0) {
  82                         count++;
  83                     } else {
  84                         // high surrogate, insert low surrogate before putting it in
  85                         buffer[count] = (char) (56320 + rnd.nextInt(128));
  86                         count--;
  87                         buffer[count] = ch;
  88                     }
  89                 } else if(ch >= 56192 && ch <= 56319) {
  90                     // private high surrogate, no effing clue, so skip it
  91                     count++;
  92                 } else {
  93                     buffer[count] = ch;
  94                 }
  95             } else {
  96                 count++;
  97             }
  98         }
  99         return new String(buffer);
 100     }
 101     public static String random(int count) {
 102         return random(count, false, false);
 103     }
 104 
 105     public static String randomAscii(int count) {
 106         return random(count, 32, 127, false, false);
 107     }
 108 
 109     public static String randomAlphabetic(int count) {
 110         return random(count, true, false);
 111     }
 112 
 113     public static String randomAlphanumeric(int count) {
 114         return random(count, true, true);
 115     }
 116 
 117     public static String randomNumeric(int count) {
 118         return random(count, false, true);
 119     }
 120 
 121     public static String random(int count, boolean letters, boolean numbers) {
 122         return random(count, 0, 0, letters, numbers);
 123     }
 124 
 125     public static String random(int count, int start, int end, boolean letters, boolean numbers) {
 126         return random(count, start, end, letters, numbers, null, RANDOM);
 127     }
 128 
 129     public static String random(int count, int start, int end, boolean letters, boolean numbers, char[] chars) {
 130         return random(count, start, end, letters, numbers, chars, RANDOM);
 131     }
 132 }