1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import java.util.Iterator;
26  import java.util.LinkedHashSet;
27  import java.util.List;
28  import java.util.Random;
29  import java.util.Set;
30  
31  /**
32   * <a href="Randomizer.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class Randomizer extends Random {
38  
39      public static Randomizer getInstance() {
40          return _instance;
41      }
42  
43      public Randomizer() {
44          super();
45      }
46  
47      public Randomizer(long seed) {
48          super(seed);
49      }
50  
51      public int[] nextInt(int n, int size) {
52          if (size > n) {
53              size = n;
54          }
55  
56          Set set = new LinkedHashSet();
57  
58          for (int i = 0; i < size; i++) {
59              while (true) {
60                  Integer value = new Integer(nextInt(n));
61  
62                  if (!set.contains(value)) {
63                      set.add(value);
64  
65                      break;
66                  }
67              }
68          }
69  
70          int[] array = new int[set.size()];
71  
72          Iterator itr = set.iterator();
73  
74          for (int i = 0; i < array.length; i++) {
75              array[i] = ((Integer)itr.next()).intValue();
76          }
77  
78          return array;
79      }
80  
81      public void randomize(char array[]) {
82          int length = array.length;
83  
84          for(int i = 0; i < length - 1; i++) {
85              int x = nextInt(length);
86              char y = array[i];
87  
88              array[i] = array[i + x];
89              array[i + x] = y;
90  
91              length--;
92          }
93      }
94  
95      public void randomize(int array[]) {
96          int length = array.length;
97  
98          for(int i = 0; i < length - 1; i++) {
99              int x = nextInt(length);
100             int y = array[i];
101 
102             array[i] = array[i + x];
103             array[i + x] = y;
104 
105             length--;
106         }
107     }
108 
109     public void randomize(List list) {
110         int size = list.size();
111 
112         for(int i = 0; i <= size; i++) {
113             int j = nextInt(size);
114             Object obj = list.get(i);
115 
116             list.set(i, list.get(i + j));
117             list.set(i + j, obj);
118 
119             size--;
120         }
121     }
122 
123     public void randomize(Object array[]) {
124         int length = array.length;
125 
126         for(int i = 0; i < length - 1; i++) {
127             int x = nextInt(length);
128             Object y = array[i];
129 
130             array[i] = array[i + x];
131             array[i + x] = y;
132 
133             length--;
134         }
135     }
136 
137     public String randomize(String s) {
138         if (s == null) {
139             return null;
140         }
141 
142         char[] array = s.toCharArray();
143 
144         randomize(array);
145 
146         return new String(array);
147     }
148 
149     private static Randomizer _instance = new Randomizer();
150 
151 }