-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFunction.java
38 lines (31 loc) · 1.27 KB
/
BFunction.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
package de.hhu.stups.prob.translator.interpretations;
import de.hhu.stups.prob.translator.BValue;
import de.hhu.stups.prob.translator.exceptions.DuplicateKeyException;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
@SuppressWarnings("WeakerAccess")
public class BFunction<K extends BValue, V extends BValue>
extends BRelation<K, V> {
public BFunction(final Set<? extends BValue> bValues) {
super(bValues);
}
public <M, N> Map<M, N> toMap(
final Function<? super K, ? extends M> keyMapper,
final Function<? super V, ? extends N> valueMapper) {
try {
return this.getValues().stream().collect(
Collectors.collectingAndThen(Collectors.toMap(
tuple -> keyMapper.apply(tuple.getFirst()),
tuple -> valueMapper.apply(tuple.getSecond())
), Collections::unmodifiableMap));
} catch (final IllegalStateException exception) {
throw new DuplicateKeyException(exception.getMessage(), exception);
}
}
public Map<K, V> toMap() {
return this.toMap(Function.identity(), Function.identity());
}
}