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