/* This file is part of Dynamic Surroundings, licensed under the MIT License (MIT).
 *
 * Copyright (c) OreCruncher
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.blockartistry.lib;

import java.util.Random;

import javax.annotation.Nonnull;

import org.blockartistry.lib.collections.ObjectArray;
import org.blockartistry.lib.random.XorShiftRandom;

public class WeightTable<T> extends ObjectArray<WeightTable.IItem<T>> {

	protected final Random RANDOM = XorShiftRandom.current();
	protected int totalWeight = 0;

	public static interface IItem<T> {

		int getWeight();

		T getItem();
	}

	public WeightTable() {
	}

	@SafeVarargs
	public WeightTable(@Nonnull final IEntrySource<T>... src) {
		for (int i = 0; i < src.length; i++)
			if (src[i].matches())
				this.add(src[i].getEntry());
	}

	@Override
	public boolean add(@Nonnull final WeightTable.IItem<T> entry) {
		assert entry != null;
		this.totalWeight += entry.getWeight();
		return super.add(entry);
	}

	@SuppressWarnings("unchecked")
	@Nonnull
	public T next() {
		if (this.totalWeight <= 0)
			return null;

		int targetWeight = this.RANDOM.nextInt(this.totalWeight);

		WeightTable.IItem<T> selected = null;
		int i = -1;
		do {
			selected = (WeightTable.IItem<T>) this.data[++i];
			targetWeight -= selected.getWeight();
		} while (targetWeight >= 0);

		return selected.getItem();
	}

	public static interface IEntrySource<T> {
		WeightTable.IItem<T> getEntry();

		boolean matches();
	}
}

@@ -1388,16 +1388,95 @@
 andom;%0A%0A
+/**%0A * Classic WeightTable for random weighted selection.%0A *%0A * @param %3CT%3E%0A */%0A
 public c
@@ -1533,16 +1533,26 @@
 e.IItem%3C
+? extends 
 T%3E%3E %7B%0A%0A%09
@@ -1561,16 +1561,23 @@
 otected 
+static 
 final Ra
@@ -1616,16 +1616,17 @@
 rent();%0A
+%0A
 %09protect
@@ -1718,32 +1718,136 @@
  getItem();%0A%09%7D%0A%0A
+%09public static interface IEntrySource%3CT%3E %7B%0A%09%09WeightTable.IItem%3CT%3E getEntry();%0A%0A%09%09boolean matches();%0A%09%7D%0A%0A
 %09public WeightTa
@@ -1920,16 +1920,26 @@
 ySource%3C
+? extends 
 T%3E... sr
@@ -1943,16 +1943,37 @@
  src) %7B%0A
+%09%09super(src.length);%0A
 %09%09for (i
@@ -2129,16 +2129,26 @@
 e.IItem%3C
+? extends 
 T%3E entry
@@ -2155,32 +2155,8 @@
 ) %7B%0A
-%09%09assert entry != null;%0A
 %09%09th
@@ -2351,21 +2351,16 @@
 eight = 
-this.
 RANDOM.n
@@ -2608,111 +2608,7 @@
 ;%0A%09%7D
-%0A%0A%09public static interface IEntrySource%3CT%3E %7B%0A%09%09WeightTable.IItem%3CT%3E getEntry();%0A%0A%09%09boolean matches();%0A%09%7D
 %0A%7D%0A

Tweak WeightTable to use static for random