001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.collection;
020
021import com.plotsquared.core.util.MathMan;
022
023import java.util.ArrayList;
024import java.util.Map;
025import java.util.Random;
026
027public class FlatRandomCollection<T> extends RandomCollection<T> {
028
029    private final T[] values;
030
031    @SuppressWarnings("unchecked")
032    public FlatRandomCollection(Map<T, Double> weights, Random random) {
033        super(weights, random);
034        int max = 0;
035        int[] counts = new int[weights.size()];
036        Double[] weightDoubles = weights.values().toArray(new Double[0]);
037        for (int i = 0; i < weightDoubles.length; i++) {
038            int weight = (int) (weightDoubles[i] * 100);
039            counts[i] = weight;
040            if (weight != (weightDoubles[i] * 100)) {
041                throw new IllegalArgumentException("Too small");
042            }
043            if (weight > max) {
044                max = weight;
045            }
046        }
047        int gcd = MathMan.gcd(counts);
048        if (max / gcd > 100000) {
049            throw new IllegalArgumentException("Too large");
050        }
051        ArrayList<T> parsed = new ArrayList<>();
052        for (Map.Entry<T, Double> entry : weights.entrySet()) {
053            int num = (int) (100 * entry.getValue());
054            for (int j = 0; j < num / gcd; j++) {
055                parsed.add(entry.getKey());
056            }
057        }
058        this.values = (T[]) parsed.toArray();
059    }
060
061    @Override
062    public T next() {
063        return values[random.nextInt(values.length)];
064    }
065
066}