1   /**
2    * Copyright (c) 2000-2008 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<Integer> set = new LinkedHashSet<Integer>();
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<Integer> itr = set.iterator();
73  
74          for (int i = 0; i < array.length; i++) {
75              array[i] = 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<Object> list) {
110         int size = list.size();
111 
112         for (int i = 0; i <= size; i++) {
113             Object obj = list.get(i);
114 
115             int j = nextInt(size);
116 
117             list.set(i, list.get(i + j));
118             list.set(i + j, obj);
119 
120             size--;
121         }
122     }
123 
124     public void randomize(Object array[]) {
125         int length = array.length;
126 
127         for (int i = 0; i < length - 1; i++) {
128             int x = nextInt(length);
129             Object y = array[i];
130 
131             array[i] = array[i + x];
132             array[i + x] = y;
133 
134             length--;
135         }
136     }
137 
138     public String randomize(String s) {
139         if (s == null) {
140             return null;
141         }
142 
143         char[] array = s.toCharArray();
144 
145         randomize(array);
146 
147         return new String(array);
148     }
149 
150     private static Randomizer _instance = new Randomizer();
151 
152 }