@Test public void testWeightedOrDefaultPassesThroughWeightedElements() { WeightedElement<String> firstManual = new WeightedElement<>("first", 0.2); WeightedElement<String> secondManual = new WeightedElement<>("second", 0.3); WeightedElement<String> thirdManual = new WeightedElement<>("third", 0.5); WeightedElement<String> first = new WeightedElement<>("first", 2); WeightedElement<String> second = new WeightedElement<>("second", 3); WeightedElement<String> third = new WeightedElement<>("third", 5); List<WeightedElement<String>> manualElements = Arrays.asList( firstManual, secondManual, thirdManual ); DistributedList<String> manualSet = new DistributedList<>(manualElements); List<WeightedElement<String>> elements = Arrays.asList(first, second, third); DistributedList<WeightedElement<String>> weightedSet = DistributedList.weightedOrDefault(elements); assertEquals(manualSet, weightedSet); }
public static <T> DistributedList<T> weightedOrDefault(final Collection<T> underlyingSet) { return new DistributedList<>( underlyingSet.stream() .map(element -> element instanceof WeightedElement ? (WeightedElement<T>) element : WeightedElement.withDefaultWeight(element) ) .collect(Collectors.toList())); }
DistributedList { public static <T> DistributedList<T> weightedOrDefault(final Collection<T> underlyingSet) { return new DistributedList<>( underlyingSet.stream() .map(element -> element instanceof WeightedElement ? (WeightedElement<T>) element : WeightedElement.withDefaultWeight(element) ) .collect(Collectors.toList())); } }
DistributedList { public static <T> DistributedList<T> weightedOrDefault(final Collection<T> underlyingSet) { return new DistributedList<>( underlyingSet.stream() .map(element -> element instanceof WeightedElement ? (WeightedElement<T>) element : WeightedElement.withDefaultWeight(element) ) .collect(Collectors.toList())); } DistributedList(final List<WeightedElement<T>> underlyingWeights); }
DistributedList { public static <T> DistributedList<T> weightedOrDefault(final Collection<T> underlyingSet) { return new DistributedList<>( underlyingSet.stream() .map(element -> element instanceof WeightedElement ? (WeightedElement<T>) element : WeightedElement.withDefaultWeight(element) ) .collect(Collectors.toList())); } DistributedList(final List<WeightedElement<T>> underlyingWeights); static DistributedList<T> singleton(final T element); static DistributedList<T> weightedOrDefault(final Collection<T> underlyingSet); static DistributedList<T> uniform(final Collection<T> underlyingSet); @SuppressWarnings("unchecked") static DistributedList<T> empty(); List<WeightedElement<T>> distributedList(); T pickRandomly(RandomNumberGenerator random); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); Stream<T> stream(); List<T> list(); boolean isEmpty(); }
DistributedList { public static <T> DistributedList<T> weightedOrDefault(final Collection<T> underlyingSet) { return new DistributedList<>( underlyingSet.stream() .map(element -> element instanceof WeightedElement ? (WeightedElement<T>) element : WeightedElement.withDefaultWeight(element) ) .collect(Collectors.toList())); } DistributedList(final List<WeightedElement<T>> underlyingWeights); static DistributedList<T> singleton(final T element); static DistributedList<T> weightedOrDefault(final Collection<T> underlyingSet); static DistributedList<T> uniform(final Collection<T> underlyingSet); @SuppressWarnings("unchecked") static DistributedList<T> empty(); List<WeightedElement<T>> distributedList(); T pickRandomly(RandomNumberGenerator random); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); Stream<T> stream(); List<T> list(); boolean isEmpty();  }