1+ /*
2+ * Copyright (c) 2020 Lewys Davies
3+ *
4+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5+ * of this software and associated documentation files (the "Software"), to deal
6+ * in the Software without restriction, including without limitation the rights
7+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+ * copies of the Software, and to permit persons to whom the Software is
9+ * furnished to do so, subject to the following conditions:
10+ *
11+ * The above copyright notice and this permission notice shall be included in all
12+ * copies or substantial portions of the Software.
13+ *
14+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+ * SOFTWARE.
21+ */
122package com .lewdev .probabilitylib ;
223
324import java .util .Comparator ;
425import java .util .Iterator ;
526import java .util .Objects ;
27+ import java .util .SplittableRandom ;
628import java .util .TreeSet ;
7- import java .util .concurrent .ThreadLocalRandom ;
829
930/**
1031 * ProbabilityCollection for retrieving random elements based on probability.
2849 * @param <E> Type of elements
2950 */
3051public class ProbabilityCollection <E > {
31-
32- protected final Comparator <ProbabilitySetElement <E >> comparator =
33- (o1 , o2 )-> Integer .compare (o1 .getIndex (), o2 .getIndex ());
3452
3553 private final TreeSet <ProbabilitySetElement <E >> collection ;
54+ private final SplittableRandom random = new SplittableRandom ();
3655
3756 private int totalProbability ;
3857
3958 /**
4059 * Construct a new Probability Collection
4160 */
4261 public ProbabilityCollection () {
43- this .collection = new TreeSet <>(this . comparator );
62+ this .collection = new TreeSet <>(Comparator . comparingInt ( ProbabilitySetElement :: getIndex ) );
4463 this .totalProbability = 0 ;
4564 }
4665
@@ -52,28 +71,28 @@ public int size() {
5271 }
5372
5473 /**
55- * @return Collection contains no elements
74+ * @return True if collection contains no elements, else False
5675 */
5776 public boolean isEmpty () {
5877 return this .collection .isEmpty ();
5978 }
6079
6180 /**
62- * @param object
63- * @return True if the collection contains the object, else False
64- * @throws IllegalArgumentException if object null
81+ * @param <E> object
82+ * @return True if collection contains the object, else False
83+ * @throws IllegalArgumentException if object is null
6584 */
6685 public boolean contains (E object ) {
6786 if (object == null ) {
68- throw new IllegalArgumentException ("Cannot check if null object is contained in a collection" );
87+ throw new IllegalArgumentException ("Cannot check if null object is contained in this collection" );
6988 }
7089
7190 return this .collection .stream ()
7291 .anyMatch (entry -> entry .getObject ().equals (object ));
7392 }
7493
7594 /**
76- * @return Iterator over collection
95+ * @return Iterator over this collection
7796 */
7897 public Iterator <ProbabilitySetElement <E >> iterator () {
7998 return this .collection .iterator ();
@@ -82,7 +101,7 @@ public Iterator<ProbabilitySetElement<E>> iterator() {
82101 /**
83102 * Add an object to this collection
84103 *
85- * @param object. Not null.
104+ * @param <E> object. Not null.
86105 * @param probability share. Must be greater than 0.
87106 *
88107 * @throws IllegalArgumentException if object is null
@@ -106,10 +125,10 @@ public void add(E object, int probability) {
106125 /**
107126 * Remove a object from this collection
108127 *
109- * @param object
128+ * @param <E> object
110129 * @return True if object was removed, else False.
111130 *
112- * @throws IllegalArgumentException if object null
131+ * @throws IllegalArgumentException if object is null
113132 */
114133 public boolean remove (E object ) {
115134 if (object == null ) {
@@ -141,11 +160,11 @@ public boolean remove(E object) {
141160 */
142161 public E get () {
143162 if (this .isEmpty ()) {
144- throw new IllegalStateException ("Cannot get an element out of a empty set " );
163+ throw new IllegalStateException ("Cannot get an element out of a empty collection " );
145164 }
146165
147166 ProbabilitySetElement <E > toFind = new ProbabilitySetElement <>(null , 0 );
148- toFind .setIndex (ThreadLocalRandom . current () .nextInt (1 , this .totalProbability + 1 ));
167+ toFind .setIndex (this . random .nextInt (1 , this .totalProbability + 1 ));
149168
150169 return Objects .requireNonNull (this .collection .floor (toFind ).getObject ());
151170 }
@@ -164,7 +183,7 @@ public final int getTotalProbability() {
164183 * We then only need to store the start index of each element,
165184 * as we make use of the TreeSet#floor
166185 */
167- private void updateIndexes () {
186+ private final void updateIndexes () {
168187 int previousIndex = 0 ;
169188
170189 for (ProbabilitySetElement <E > entry : this .collection ) {
@@ -190,7 +209,7 @@ final static class ProbabilitySetElement<T> {
190209 private int index ;
191210
192211 /**
193- * @param object
212+ * @param <T> object
194213 * @param probability
195214 */
196215 protected ProbabilitySetElement (T object , int probability ) {
@@ -199,7 +218,7 @@ protected ProbabilitySetElement(T object, int probability) {
199218 }
200219
201220 /**
202- * @return The actual object
221+ * @return <T> The actual object
203222 */
204223 public final T getObject () {
205224 return this .object ;
0 commit comments