1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class Randomizer extends Random {
37  
38      public static Randomizer getInstance() {
39          return _instance;
40      }
41  
42      public Randomizer() {
43          super();
44      }
45  
46      public Randomizer(long seed) {
47          super(seed);
48      }
49  
50      public int[] nextInt(int n, int size) {
51          if (size > n) {
52              size = n;
53          }
54  
55          Set<Integer> set = new LinkedHashSet<Integer>();
56  
57          for (int i = 0; i < size; i++) {
58              while (true) {
59                  Integer value = new Integer(nextInt(n));
60  
61                  if (!set.contains(value)) {
62                      set.add(value);
63  
64                      break;
65                  }
66              }
67          }
68  
69          int[] array = new int[set.size()];
70  
71          Iterator<Integer> itr = set.iterator();
72  
73          for (int i = 0; i < array.length; i++) {
74              array[i] = itr.next().intValue();
75          }
76  
77          return array;
78      }
79  
80      public void randomize(char array[]) {
81          int length = array.length;
82  
83          for (int i = 0; i < length - 1; i++) {
84              int x = nextInt(length);
85              char y = array[i];
86  
87              array[i] = array[i + x];
88              array[i + x] = y;
89  
90              length--;
91          }
92      }
93  
94      public void randomize(int array[]) {
95          int length = array.length;
96  
97          for (int i = 0; i < length - 1; i++) {
98              int x = nextInt(length);
99              int y = array[i];
100 
101             array[i] = array[i + x];
102             array[i + x] = y;
103 
104             length--;
105         }
106     }
107 
108     public void randomize(List<Object> list) {
109         int size = list.size();
110 
111         for (int i = 0; i <= size; i++) {
112             Object obj = list.get(i);
113 
114             int j = nextInt(size);
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 }