Skip to content

Commit 2375f68

Browse files
committed
cleanups
1 parent 265b685 commit 2375f68

File tree

12 files changed

+103
-174
lines changed

12 files changed

+103
-174
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6161
* `Minimizer` no longer provides a `getInstance()` method but can be instantiated directly.
6262
* The `OneSEVPA` interface has been generalized to an arbitrary (k-)`SEVPA` interface. The old `OneSEVPA` specialization is still available and unchanged.
6363
* The `OneSEVPAUtils` class has been merged into the `OneSEVPAs` class.
64+
* The `RandomUtil` class has been made a factory (non-instantiable, only static methods) and its methods now require the random object as first parameter.
6465
* AutomataLib classes no longer implement `Serializable`. We never fully supported the semantics of the interface and never intended to do so. In fact, the old approach failed miserably if any class was involved where we missed an "implements Serializable" statement. In order to prevent confusion by promising false contracts, implementing this markup interface has been removed. Serialization should now be done in user-land via one of the many external (and more optimizable) serialization frameworks such as FST, XStream, etc.
6566
* `ShortestPaths` now offers fewer but less confusing methods. Previously there were methods such as `shortestPath` that took an initial node and multiple target nodes which much better fits to the idea of computing `shortestPath*s*` rather than any shortest path to one of the target nodes. The old behavior can still be replicated with the generic `Predicate`-based versions.
6667
* `StrictPriorityQueue` is now package-private as it is only meant for internal use.
@@ -73,6 +74,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
7374
* Removed `AbstractCompactNPGraph`, use `AbstractCompactGraph` instead.
7475
* Removed `AbstractCompactSimpleGraph`. All functionality is provided in `CompactSimpleGraph`.
7576
* Removed `CmpUtil#safeComparator`. Use `Comparators#nullsFirst` or `Comparators#nullsLast` instead.
77+
* Removed `DelegateVisualizationHelper` without replacement. Instead, directly override/extend the `VisualizationHelper` you want to delegate to.
7678
* Removed the DFS-specific `DFSTraversalVisitor` (and related classes) without replacement. Client-code that relied on this class can re-implement the functionality by providing an own implementation of the more general `GraphTraversalVisitor`. See the changes on the `DFSExample` for reference.
7779
* Removed (unused) `DisjointSetForestInt` class without replacement.
7880
* Removed non-static methods on `RandomAutomata` factory (including the `getInstance()` method).

api/src/main/java/net/automatalib/graph/helper/SimpleNodeIDs.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public int getNodeId(N node) {
5252

5353
@Override
5454
public N getNode(int id) {
55+
if (id < 0 || id >= nodes.size()) {
56+
throw new IllegalArgumentException();
57+
}
5558
return nodes.get(id);
5659
}
5760
}

api/src/main/java/net/automatalib/visualization/helper/DelegateVisualizationHelper.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

api/src/main/java/net/automatalib/visualization/package-info.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
/**
18-
* This package (including sub-packages) contains several approaches for the visualization of the different types of
19-
* automata, transition systems, and graphs supported by AutomataLib.
18+
* This package contains several approaches for the visualization of the different types of automata, transition
19+
* systems, and graphs supported by AutomataLib.
2020
*/
2121
package net.automatalib.visualization;

commons/util/src/main/java/net/automatalib/common/util/random/RandomUtil.java

Lines changed: 15 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,13 @@
2323

2424
import org.checkerframework.checker.nullness.qual.Nullable;
2525

26-
public class RandomUtil {
26+
public final class RandomUtil {
2727

28-
private final Random random;
29-
30-
public RandomUtil() {
31-
this(new Random());
32-
}
33-
34-
public RandomUtil(Random random) {
35-
this.random = random;
28+
private RandomUtil() {
29+
// prevent instantiation
3630
}
3731

38-
public <T> @Nullable T choose(T[] array) {
39-
return choose(array, random);
40-
}
41-
42-
public static <T> @Nullable T choose(T[] array, Random rand) {
32+
public static <T> @Nullable T choose(Random rand, T[] array) {
4333
int len = array.length;
4434
if (len == 0) {
4535
return null;
@@ -48,11 +38,7 @@ public RandomUtil(Random random) {
4838
return array[idx];
4939
}
5040

51-
public <T> @Nullable T choose(List<? extends T> list) {
52-
return choose(list, random);
53-
}
54-
55-
public static <T> @Nullable T choose(List<? extends T> list, Random rand) {
41+
public static <T> @Nullable T choose(Random rand, List<? extends T> list) {
5642
int size = list.size();
5743
if (size == 0) {
5844
return null;
@@ -61,49 +47,25 @@ public RandomUtil(Random random) {
6147
return list.get(idx);
6248
}
6349

64-
public Random getRandom() {
65-
return random;
66-
}
67-
68-
/**
69-
* Sample a specified number of distinct integers from a specified range.
70-
* <p>
71-
* The implementation is based on Floyd's <a href="https://doi.org/10.1145/30401.315746">Algorithm F2</a>. Note that
72-
* this algorithm ensures equal probability of each integer within in the specified range to appear in the returned
73-
* array but no equal probability of their order.
74-
*
75-
* @param num
76-
* number of integers to sample
77-
* @param min
78-
* lower bound (inclusive) of sampled values
79-
* @param max
80-
* upper bound (exclusive) of samples values
81-
*
82-
* @return an array of distinct integers sampled from the specified range
83-
*/
84-
public int[] distinctIntegers(int num, int min, int max) {
85-
return distinctIntegers(num, min, max, random);
86-
}
87-
8850
/**
8951
* Sample a specified number of distinct integers from a specified range.
9052
* <p>
9153
* The implementation is based on Floyd's <a href="https://doi.org/10.1145/30401.315746">Algorithm F2</a>. Note that
9254
* this algorithm ensures equal probability of each integer within in the specified range to appear in the returned
9355
* array but no equal probability of their order.
9456
*
57+
* @param rand
58+
* the random instance for generating numbers
9559
* @param num
9660
* number of integers to sample
9761
* @param min
9862
* lower bound (inclusive) of sampled values
9963
* @param max
10064
* upper bound (exclusive) of samples values
101-
* @param rand
102-
* the random instance for generating numbers
10365
*
10466
* @return an array of distinct integers sampled from the specified range
10567
*/
106-
public static int[] distinctIntegers(int num, int min, int max, Random rand) {
68+
public static int[] distinctIntegers(Random rand, int num, int min, int max) {
10769
int range = max - min;
10870
int size = Math.min(num, range);
10971

@@ -122,19 +84,11 @@ public static int[] distinctIntegers(int num, int min, int max, Random rand) {
12284
return result;
12385
}
12486

125-
public int[] distinctIntegers(int num, int max) {
126-
return distinctIntegers(num, max, random);
127-
}
128-
129-
public static int[] distinctIntegers(int num, int max, Random rand) {
130-
return distinctIntegers(num, 0, max, rand);
131-
}
132-
133-
public <T> List<T> sample(List<? extends T> list, int num) {
134-
return sample(list, num, random);
87+
public static int[] distinctIntegers(Random rand, int num, int max) {
88+
return distinctIntegers(rand, num, 0, max);
13589
}
13690

137-
public static <T> List<T> sample(List<? extends T> list, int num, Random rand) {
91+
public static <T> List<T> sample(Random rand, List<? extends T> list, int num) {
13892
if (list.isEmpty()) {
13993
return Collections.emptyList();
14094
}
@@ -155,41 +109,23 @@ public static <T> List<T> sample(List<? extends T> list, int num, Random rand) {
155109
* this algorithm ensures equal probability of each element within in the specified list to appear in the returned
156110
* list but no equal probability of their order.
157111
*
112+
* @param rand
113+
* the random instance for generating numbers
158114
* @param list
159115
* the list to sample elements from
160116
* @param num
161117
* number of integers to sample
162118
*
163119
* @return a list of distinct elements sampled from the specified list
164120
*/
165-
public <T> List<T> sampleUnique(List<? extends T> list, int num) {
166-
return sampleUnique(list, num, random);
167-
}
168-
169-
/**
170-
* Sample a specified number of elements from specified list.
171-
* <p>
172-
* The implementation is based on Floyd's <a href="https://doi.org/10.1145/30401.315746">Algorithm F2</a>. Note that
173-
* this algorithm ensures equal probability of each element within in the specified list to appear in the returned
174-
* list but no equal probability of their order.
175-
*
176-
* @param list
177-
* the list to sample elements from
178-
* @param num
179-
* number of integers to sample
180-
* @param rand
181-
* the random instance for generating numbers
182-
*
183-
* @return a list of distinct elements sampled from the specified list
184-
*/
185-
public static <T> List<T> sampleUnique(List<? extends T> list, int num, Random rand) {
121+
public static <T> List<T> sampleUnique(Random rand, List<? extends T> list, int num) {
186122
int elems = list.size();
187123

188124
if (elems == 0) {
189125
return Collections.emptyList();
190126
}
191127

192-
int[] indices = distinctIntegers(num, elems, rand);
128+
int[] indices = distinctIntegers(rand, num, elems);
193129
List<T> result = new ArrayList<>(indices.length);
194130

195131
for (int index : indices) {

commons/util/src/test/java/net/automatalib/common/util/random/RandomUtilTest.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,93 +31,91 @@ public class RandomUtilTest {
3131

3232
private static final int HIGH = 10;
3333

34-
private RandomUtil util;
35-
34+
private Random random;
3635
private List<Integer> list;
3736
private Integer[] array;
3837

3938
@BeforeClass
4039
public void setUp() {
41-
this.util = new RandomUtil(new Random(42));
40+
this.random = new Random(42);
4241
this.list = CollectionsUtil.intRange(0, HIGH, 1);
4342
this.array = IntStream.range(0, HIGH).boxed().toArray(Integer[]::new);
4443
}
4544

4645
@Test
4746
public void testChooseArray() {
48-
Assert.assertNull(util.choose(new Object[0]));
49-
Assert.assertEquals(util.choose(new Object[] {1}), 1);
47+
Assert.assertNull(RandomUtil.choose(random, new Object[0]));
48+
Assert.assertEquals(RandomUtil.choose(random, new Object[] {1}), 1);
5049

51-
final Integer chosenElement = util.choose(array);
50+
final Integer chosenElement = RandomUtil.choose(random, array);
5251
Assert.assertNotNull(chosenElement);
5352
Assert.assertTrue(0 <= chosenElement && chosenElement < HIGH);
5453
}
5554

5655
@Test
5756
public void testChooseList() {
58-
Assert.assertNull(util.choose(Collections.emptyList()));
59-
Assert.assertEquals(util.choose(Collections.singletonList(1)), (Integer) 1);
57+
Assert.assertNull(RandomUtil.choose(random, Collections.emptyList()));
58+
Assert.assertEquals(RandomUtil.choose(random, Collections.singletonList(1)), (Integer) 1);
6059

61-
final Integer chosenElement = util.choose(list);
60+
final Integer chosenElement = RandomUtil.choose(random, list);
6261
Assert.assertNotNull(chosenElement);
6362
Assert.assertTrue(0 <= chosenElement && chosenElement < HIGH);
6463
}
6564

6665
@Test
6766
public void testDistinctIntegers() {
68-
Assert.assertEquals(util.distinctIntegers(0, HIGH).length, 0);
67+
Assert.assertEquals(RandomUtil.distinctIntegers(random, 0, HIGH).length, 0);
6968

70-
int[] result = util.distinctIntegers(HIGH, HIGH);
71-
util.distinctIntegers(HIGH, HIGH);
69+
int[] result = RandomUtil.distinctIntegers(random, HIGH, HIGH);
7270
Assert.assertEquals(result.length, HIGH);
7371
Arrays.sort(result);
7472
Assert.assertEquals(box(result), array);
7573

7674
// Sample from lower half domain
77-
result = util.distinctIntegers(HIGH, HIGH / 2);
75+
result = RandomUtil.distinctIntegers(random, HIGH, HIGH / 2);
7876
Assert.assertEquals(result.length, HIGH / 2);
7977
Arrays.sort(result);
8078
Assert.assertEquals(box(result), Arrays.copyOfRange(array, 0, HIGH / 2));
8179

8280
// Sample from upper half domain
83-
result = util.distinctIntegers(HIGH, HIGH / 2, HIGH);
81+
result = RandomUtil.distinctIntegers(random, HIGH, HIGH / 2, HIGH);
8482
Assert.assertEquals(result.length, HIGH / 2);
8583
Arrays.sort(result);
8684
Assert.assertEquals(box(result), Arrays.copyOfRange(array, HIGH / 2, HIGH));
8785

8886
// Sample all but one from full domain
89-
result = util.distinctIntegers(HIGH - 1, HIGH);
87+
result = RandomUtil.distinctIntegers(random, HIGH - 1, HIGH);
9088
Assert.assertEquals(result.length, HIGH - 1);
9189
Assert.assertEquals(new HashSet<>(Arrays.asList(box(result))).size(), HIGH - 1);
9290
Assert.assertTrue(Arrays.asList(array).containsAll(Arrays.asList(box(result))));
9391
}
9492

9593
@Test
9694
public void testSampleUnique() {
97-
Assert.assertEquals(util.sampleUnique(Collections.emptyList(), HIGH), Collections.emptyList());
95+
Assert.assertEquals(RandomUtil.sampleUnique(random, Collections.emptyList(), HIGH), Collections.emptyList());
9896

99-
List<Integer> result = util.sampleUnique(list, HIGH);
97+
List<Integer> result = RandomUtil.sampleUnique(random, list, HIGH);
10098
Assert.assertEquals(result.size(), HIGH);
10199
result.sort(Integer::compareTo);
102100
Assert.assertEquals(result, list);
103101

104102
// Sample all but one from full domain
105-
result = util.sampleUnique(list, HIGH - 1);
103+
result = RandomUtil.sampleUnique(random, list, HIGH - 1);
106104
Assert.assertEquals(result.size(), HIGH - 1);
107105
Assert.assertEquals(new HashSet<>(result).size(), HIGH - 1);
108106
Assert.assertTrue(list.containsAll(result));
109107
}
110108

111109
@Test
112110
public void testSampleList() {
113-
Assert.assertEquals(util.sample(Collections.emptyList(), HIGH), Collections.emptyList());
111+
Assert.assertEquals(RandomUtil.sample(random, Collections.emptyList(), HIGH), Collections.emptyList());
114112

115-
List<Integer> result = util.sample(list, HIGH);
113+
List<Integer> result = RandomUtil.sample(random, list, HIGH);
116114
Assert.assertEquals(result.size(), HIGH);
117115
Assert.assertTrue(list.containsAll(result));
118116

119117
// Sample double the size from full domain
120-
result = util.sample(list, HIGH * 2);
118+
result = RandomUtil.sample(random, list, HIGH * 2);
121119
Assert.assertEquals(result.size(), HIGH * 2);
122120
Assert.assertTrue(list.containsAll(result));
123121

core/src/main/java/net/automatalib/graph/base/AbstractCompactGraph.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ public int getNodeId(Integer node) {
122122

123123
@Override
124124
public Integer getNode(int id) {
125+
if (id < 0 || id >= size) {
126+
throw new IllegalArgumentException();
127+
}
125128
return id;
126129
}
127130

0 commit comments

Comments
 (0)