1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.util.Iterator;
18  import java.util.LinkedHashSet;
19  import java.util.List;
20  import java.util.Random;
21  import java.util.Set;
22  
23  /**
24   * <a href="Randomizer.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Brian Wing Shun Chan
27   */
28  public class Randomizer extends Random {
29  
30      public static Randomizer getInstance() {
31          return _instance;
32      }
33  
34      public Randomizer() {
35          super();
36      }
37  
38      public Randomizer(long seed) {
39          super(seed);
40      }
41  
42      public int[] nextInt(int n, int size) {
43          if (size > n) {
44              size = n;
45          }
46  
47          Set<Integer> set = new LinkedHashSet<Integer>();
48  
49          for (int i = 0; i < size; i++) {
50              while (true) {
51                  Integer value = new Integer(nextInt(n));
52  
53                  if (!set.contains(value)) {
54                      set.add(value);
55  
56                      break;
57                  }
58              }
59          }
60  
61          int[] array = new int[set.size()];
62  
63          Iterator<Integer> itr = set.iterator();
64  
65          for (int i = 0; i < array.length; i++) {
66              array[i] = itr.next().intValue();
67          }
68  
69          return array;
70      }
71  
72      public void randomize(char array[]) {
73          int length = array.length;
74  
75          for (int i = 0; i < length - 1; i++) {
76              int x = nextInt(length);
77              char y = array[i];
78  
79              array[i] = array[i + x];
80              array[i + x] = y;
81  
82              length--;
83          }
84      }
85  
86      public void randomize(int array[]) {
87          int length = array.length;
88  
89          for (int i = 0; i < length - 1; i++) {
90              int x = nextInt(length);
91              int y = array[i];
92  
93              array[i] = array[i + x];
94              array[i + x] = y;
95  
96              length--;
97          }
98      }
99  
100     public void randomize(List<Object> list) {
101         int size = list.size();
102 
103         for (int i = 0; i <= size; i++) {
104             Object obj = list.get(i);
105 
106             int j = nextInt(size);
107 
108             list.set(i, list.get(i + j));
109             list.set(i + j, obj);
110 
111             size--;
112         }
113     }
114 
115     public void randomize(Object array[]) {
116         int length = array.length;
117 
118         for (int i = 0; i < length - 1; i++) {
119             int x = nextInt(length);
120             Object y = array[i];
121 
122             array[i] = array[i + x];
123             array[i + x] = y;
124 
125             length--;
126         }
127     }
128 
129     public String randomize(String s) {
130         if (s == null) {
131             return null;
132         }
133 
134         char[] array = s.toCharArray();
135 
136         randomize(array);
137 
138         return new String(array);
139     }
140 
141     private static Randomizer _instance = new Randomizer();
142 
143 }