/** * Creates an instance of {@link ObjectGenerator} which generates specified values distributed by their weights. * * @param pairs List of values with corresponding weights. * @param <T> Type instance of {@link ObjectGenerator} will generate. * @return An instance of {@link ObjectGenerator} which generates specified values distributed by their weights. */ public static <T> ObjectGenerator<T> weighted(List<WeightPair<T>> pairs) { return wrap(new WeightedValue<>(unwrapWeightPairs(pairs))); }
/** * Constructs discrete weighted value with specified <code>values</code> and <code>weights</code>. * * @param weightedValuePairs List of values with their corresponding weights. */ public WeightedValue(List<WeightedValuePair<T>> weightedValuePairs) { if (weightedValuePairs == null || weightedValuePairs.isEmpty()) { throw new IllegalArgumentException("List of weighted values cannot be null nor empty."); } this.enumeratedDistribution = new EnumeratedDistribution<>(mapToPairList(weightedValuePairs)); this.values = weightedValuePairs.stream().map(x -> x.getValue()).collect(Collectors.toList()); }
/** * Creates an instance of {@link ObjectGenerator} which generates specified values distributed by their weights. * * @param pairs List of values with corresponding weights. * @param <T> Type instance of {@link ObjectGenerator} will generate. * @return An instance of {@link ObjectGenerator} which generates specified values distributed by their weights. */ public static <T> ObjectGenerator<T> weighted(List<WeightPair<T>> pairs) { return wrap(new WeightedValue<>(unwrapWeightPairs(pairs))); }
/** * Constructs discrete weighted value with specified <code>values</code> and <code>weights</code>. * * @param weightedValuePairs List of values with their corresponding weights. */ public WeightedValue(List<WeightedValuePair<T>> weightedValuePairs) { if (weightedValuePairs == null || weightedValuePairs.isEmpty()) { throw new IllegalArgumentException("List of weighted values cannot be null nor empty."); } this.enumeratedDistribution = new EnumeratedDistribution<>(mapToPairList(weightedValuePairs)); this.values = weightedValuePairs.stream().map(x -> x.getValue()).collect(Collectors.toList()); }
/** * Weighted value definition. * * @return Weighted value definition rule. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public Rule weightedValue() { return Sequence(function("weighted", bracketList(weightedValuePair())), push(new WeightedValue((List) pop()))); }
/** * Weighted value definition. * * @return Weighted value definition rule. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public Rule weightedValue() { return Sequence(function("weighted", bracketList(weightedValuePair())), push(new WeightedValue((List) pop()))); }