Type-safety!
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4089 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -153,7 +153,7 @@ public class RandomUtil
|
|||||||
* @return a randomly selected item or null if the array is null or of
|
* @return a randomly selected item or null if the array is null or of
|
||||||
* length zero.
|
* length zero.
|
||||||
*/
|
*/
|
||||||
public static Object pickRandom (Object[] values)
|
public static <T> T pickRandom (T[] values)
|
||||||
{
|
{
|
||||||
return (values == null || values.length == 0) ? null :
|
return (values == null || values.length == 0) ? null :
|
||||||
values[getInt(values.length)];
|
values[getInt(values.length)];
|
||||||
@@ -170,7 +170,7 @@ public class RandomUtil
|
|||||||
* @return a randomly selected item or null if the array is null, of
|
* @return a randomly selected item or null if the array is null, of
|
||||||
* length zero or contains only the skip item.
|
* length zero or contains only the skip item.
|
||||||
*/
|
*/
|
||||||
public static Object pickRandom (Object[] values, Object skip)
|
public static <T> T pickRandom (T[] values, T skip)
|
||||||
{
|
{
|
||||||
if (values == null || values.length < 2) {
|
if (values == null || values.length < 2) {
|
||||||
return null;
|
return null;
|
||||||
@@ -187,7 +187,7 @@ public class RandomUtil
|
|||||||
/**
|
/**
|
||||||
* Picks a random object from the supplied {@link Collection}.
|
* Picks a random object from the supplied {@link Collection}.
|
||||||
*/
|
*/
|
||||||
public static Object pickRandom (Collection values)
|
public static <T> T pickRandom (Collection<T> values)
|
||||||
{
|
{
|
||||||
return pickRandom(values.iterator(), values.size());
|
return pickRandom(values.iterator(), values.size());
|
||||||
}
|
}
|
||||||
@@ -197,7 +197,7 @@ public class RandomUtil
|
|||||||
*
|
*
|
||||||
* @return a randomly selected item.
|
* @return a randomly selected item.
|
||||||
*/
|
*/
|
||||||
public static Object pickRandom (List values)
|
public static <T> T pickRandom (List<T> values)
|
||||||
{
|
{
|
||||||
int size = values.size();
|
int size = values.size();
|
||||||
if (size == 0) {
|
if (size == 0) {
|
||||||
@@ -214,7 +214,7 @@ public class RandomUtil
|
|||||||
*
|
*
|
||||||
* @return a randomly selected item.
|
* @return a randomly selected item.
|
||||||
*/
|
*/
|
||||||
public static Object pickRandom (List values, Object skip)
|
public static <T> T pickRandom (List<T> values, T skip)
|
||||||
{
|
{
|
||||||
return pickRandom(values, skip, rand);
|
return pickRandom(values, skip, rand);
|
||||||
}
|
}
|
||||||
@@ -226,7 +226,7 @@ public class RandomUtil
|
|||||||
*
|
*
|
||||||
* @return a randomly selected item.
|
* @return a randomly selected item.
|
||||||
*/
|
*/
|
||||||
public static Object pickRandom (List values, Object skip, Random r)
|
public static <T> T pickRandom (List<T> values, T skip, Random r)
|
||||||
{
|
{
|
||||||
int size = values.size();
|
int size = values.size();
|
||||||
if (size < 2) {
|
if (size < 2) {
|
||||||
@@ -236,7 +236,7 @@ public class RandomUtil
|
|||||||
|
|
||||||
int pick = r.nextInt(size - 1);
|
int pick = r.nextInt(size - 1);
|
||||||
for (int ii = 0; ii < size; ii++) {
|
for (int ii = 0; ii < size; ii++) {
|
||||||
Object val = values.get(ii);
|
T val = values.get(ii);
|
||||||
if ((val != skip) && (pick-- == 0)) {
|
if ((val != skip) && (pick-- == 0)) {
|
||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
@@ -253,14 +253,14 @@ public class RandomUtil
|
|||||||
* @exception NoSuchElementException thrown if the iterator provides
|
* @exception NoSuchElementException thrown if the iterator provides
|
||||||
* fewer than <code>count</code> elements.
|
* fewer than <code>count</code> elements.
|
||||||
*/
|
*/
|
||||||
public static Object pickRandom (Iterator iter, int count)
|
public static <T> T pickRandom (Iterator<T> iter, int count)
|
||||||
{
|
{
|
||||||
if (count < 1) {
|
if (count < 1) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Must have at least one element [count=" + count + "]");
|
"Must have at least one element [count=" + count + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
Object value = iter.next();
|
T value = iter.next();
|
||||||
for (int ii = 0, ll = getInt(count); ii < ll; ii++) {
|
for (int ii = 0, ll = getInt(count); ii < ll; ii++) {
|
||||||
value = iter.next();
|
value = iter.next();
|
||||||
}
|
}
|
||||||
@@ -279,7 +279,7 @@ public class RandomUtil
|
|||||||
* @exception NoSuchElementException thrown if the iterator provides
|
* @exception NoSuchElementException thrown if the iterator provides
|
||||||
* fewer than <code>count</code> elements.
|
* fewer than <code>count</code> elements.
|
||||||
*/
|
*/
|
||||||
public static Object pickRandom (Iterator iter, int count, Object skip)
|
public static <T> T pickRandom (Iterator<T> iter, int count, T skip)
|
||||||
{
|
{
|
||||||
if (count < 2) {
|
if (count < 2) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
@@ -287,7 +287,7 @@ public class RandomUtil
|
|||||||
}
|
}
|
||||||
|
|
||||||
int index = getInt(count-1);
|
int index = getInt(count-1);
|
||||||
Object value = null;
|
T value = null;
|
||||||
do {
|
do {
|
||||||
value = iter.next();
|
value = iter.next();
|
||||||
if (value == skip) {
|
if (value == skip) {
|
||||||
|
|||||||
Reference in New Issue
Block a user