001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import java.util.Iterator;
024: import java.util.LinkedHashSet;
025: import java.util.List;
026: import java.util.Random;
027: import java.util.Set;
028:
029: /**
030: * <a href="Randomizer.java.html"><b><i>View Source</i></b></a>
031: *
032: * @author Brian Wing Shun Chan
033: *
034: */
035: public class Randomizer extends Random {
036:
037: public static Randomizer getInstance() {
038: return _instance;
039: }
040:
041: public Randomizer() {
042: super ();
043: }
044:
045: public Randomizer(long seed) {
046: super (seed);
047: }
048:
049: public int[] nextInt(int n, int size) {
050: if (size > n) {
051: size = n;
052: }
053:
054: Set set = new LinkedHashSet();
055:
056: for (int i = 0; i < size; i++) {
057: while (true) {
058: Integer value = new Integer(nextInt(n));
059:
060: if (!set.contains(value)) {
061: set.add(value);
062:
063: break;
064: }
065: }
066: }
067:
068: int[] array = new int[set.size()];
069:
070: Iterator itr = set.iterator();
071:
072: for (int i = 0; i < array.length; i++) {
073: array[i] = ((Integer) itr.next()).intValue();
074: }
075:
076: return array;
077: }
078:
079: public void randomize(char array[]) {
080: int length = array.length;
081:
082: for (int i = 0; i < length - 1; i++) {
083: int x = nextInt(length);
084: char y = array[i];
085:
086: array[i] = array[i + x];
087: array[i + x] = y;
088:
089: length--;
090: }
091: }
092:
093: public void randomize(int array[]) {
094: int length = array.length;
095:
096: for (int i = 0; i < length - 1; i++) {
097: int x = nextInt(length);
098: int y = array[i];
099:
100: array[i] = array[i + x];
101: array[i + x] = y;
102:
103: length--;
104: }
105: }
106:
107: public void randomize(List list) {
108: int size = list.size();
109:
110: for (int i = 0; i <= size; i++) {
111: int j = nextInt(size);
112: Object obj = list.get(i);
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: }
|