-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSequence.java
69 lines (62 loc) · 2.45 KB
/
BSequence.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package de.hhu.stups.prob.translator.interpretations;
import de.hhu.stups.prob.translator.BNumber;
import de.hhu.stups.prob.translator.BTuple;
import de.hhu.stups.prob.translator.BValue;
import de.hhu.stups.prob.translator.exceptions.DuplicateKeyException;
import de.hhu.stups.prob.translator.exceptions.InterpretationException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public class BSequence<V extends BValue> extends BFunction<BNumber, V> {
@SuppressFBWarnings("CT_CONSTRUCTOR_THROW")
@SuppressWarnings("unchecked")
public BSequence(final Set<? extends BValue> bValues) {
super(bValues);
final boolean isValid = bValues.stream().allMatch(tuple ->
tuple.getClass().equals(BTuple.class)
&& ((BTuple<BValue, ?>) tuple).getFirst()
.getClass().equals(BNumber.class)
);
if (!isValid) {
throw new InterpretationException(
"Incompatible set for conversion to sequence");
}
}
/**
* Converts a sequence-type set (pairs of index, value) to a list of values
* ordered by their index.
*
* @return list of values
*/
@SuppressWarnings("unchecked")
public List<V> toList() {
return this.toList(Function.identity());
}
@SuppressWarnings("PMD.DataflowAnomalyAnalysis")
public <K> List<K> toList(final Function<V, K> mapper) {
final Set<BNumber> seen = new HashSet<>();
for (final BTuple<BNumber, V> tuple : this.getValues()) {
if (seen.add(tuple.getFirst())) {
continue;
}
throw
new DuplicateKeyException(String.format(
"Repeated Key in Sequence: key=%s",
tuple.getFirst()));
}
return this.getValues().stream()
.sorted(Comparator.comparingInt(
value -> value.getFirst().intValue()))
.map(BTuple::getSecond)
.map(mapper)
.collect(
Collectors.collectingAndThen(
Collectors.toList(),
Collections::unmodifiableList));
}
}