com.liferay.portal.kernel.util.Randomizer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of portal-service Show documentation
Show all versions of portal-service Show documentation
Contains interfaces for the portal services. Interfaces are only loaded by the global class loader and are shared by all plugins.
/**
* Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portal.kernel.util;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
/**
* @author Brian Wing Shun Chan
* @deprecated As of 6.2.0
*/
public class Randomizer extends Random {
public static Randomizer getInstance() {
return _instance;
}
public Randomizer() {
super();
}
public Randomizer(long seed) {
super(seed);
}
public int[] nextInt(int n, int size) {
if (size > n) {
size = n;
}
Set set = new LinkedHashSet();
for (int i = 0; i < size; i++) {
while (true) {
Integer value = new Integer(nextInt(n));
if (!set.contains(value)) {
set.add(value);
break;
}
}
}
int[] array = new int[set.size()];
Iterator itr = set.iterator();
for (int i = 0; i < array.length; i++) {
array[i] = itr.next().intValue();
}
return array;
}
public void randomize(char[] array) {
int length = array.length;
for (int i = 0; i < length - 1; i++) {
int x = nextInt(length);
char y = array[i];
array[i] = array[i + x];
array[i + x] = y;
length--;
}
}
public void randomize(int[] array) {
int length = array.length;
for (int i = 0; i < length - 1; i++) {
int x = nextInt(length);
int y = array[i];
array[i] = array[i + x];
array[i + x] = y;
length--;
}
}
public void randomize(List