-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBiFunctionClassMap.java
More file actions
108 lines (90 loc) · 3.33 KB
/
BiFunctionClassMap.java
File metadata and controls
108 lines (90 loc) · 3.33 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* Copyright 2015 SPeCS Research Group.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package pt.up.fe.specs.util.classmap;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;
import pt.up.fe.specs.util.exceptions.NotImplementedException;
import pt.up.fe.specs.util.utilities.ClassMapper;
/**
* Maps a class to a BiFunction that receives an instance of that class being
* used as key and other object.
*
* @author JoaoBispo
*
* @param <T>
* @param <U>
*/
public class BiFunctionClassMap<T, U, R> {
private final Map<Class<? extends T>, BiFunction<? extends T, U, R>> map;
private final ClassMapper classMapper;
public BiFunctionClassMap() {
this.map = new HashMap<>();
// this.supportInterfaces = true;
this.classMapper = new ClassMapper();
}
public <ER extends R> BiFunctionClassMap(BiFunctionClassMap<T, U, ER> other) {
this.map = new HashMap<>();
for (var keyPair : other.map.entrySet()) {
this.map.put(keyPair.getKey(), (BiFunction<T, U, R>) keyPair.getValue());
}
this.classMapper = new ClassMapper(other.classMapper);
}
/**
* Associates the specified value with the specified key.
*
* <p>
* The key is always a class of a type that is a subtype of the type in the
* value.
* <p>
* Example: <br>
* - put(Subclass.class, usesSuperClass), ok<br>
* - put(Subclass.class, usesSubClass), ok<br>
* - put(Superclass.class, usesSubClass), error<br>
*
*/
public <VS extends T, KS extends VS> void put(Class<KS> aClass,
BiFunction<VS, U, R> value) {
this.map.put(aClass, value);
classMapper.add(aClass);
}
@SuppressWarnings("unchecked")
private <TK extends T> BiFunction<T, U, R> get(Class<TK> key) {
// Map given class to a class supported by this instance
var mappedClass = classMapper.map(key);
if (mappedClass.isEmpty()) {
return null;
}
var function = this.map.get(mappedClass.get());
Objects.requireNonNull(function, () -> "There should be a mapping for " + mappedClass.get() + ", verify");
return (BiFunction<T, U, R>) function;
}
@SuppressWarnings("unchecked")
private <TK extends T> BiFunction<T, U, R> get(TK key) {
return get((Class<TK>) key.getClass());
}
/**
* Calls the BiFunction.accept associated with class of the value t, or throws
* an Exception if no BiFunction could be found in the map.
*
*/
public R apply(T t, U u) {
BiFunction<T, U, R> result = get(t);
if (result == null) {
throw new NotImplementedException("BiConsumer not defined for class '"
+ t.getClass() + "'");
}
return result.apply(t, u);
}
}