Skip to content

Commit 5ac6ba1

Browse files
committed
Find restriction with closest relation in restriction lookup to support subclassing properly
1 parent 324ff73 commit 5ac6ba1

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

config/pmd/pmd.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@
184184
\QFound 'DU'-anomaly for variable 'outer' (lines '\E.* |
185185
\QFound 'DU'-anomaly for variable 'restrictionPolicy' (lines '\E.* |
186186
\QFound 'DU'-anomaly for variable 'result' (lines '\E.* |
187-
\QFound 'DU'-anomaly for variable 'usageTree' (lines '\E.*
187+
\QFound 'DU'-anomaly for variable 'usageTree' (lines '\E.* |
188+
\QFound 'DD'-anomaly for variable 'distance' (lines '\E.*
188189
</value>
189190
</property>
190191
</properties>

src/main/java/net/kautler/command/restriction/RestrictionLookup.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020

2121
import java.util.Collection;
2222
import java.util.Map;
23+
import java.util.Set;
2324
import java.util.StringJoiner;
2425
import java.util.concurrent.ConcurrentHashMap;
25-
import java.util.concurrent.CopyOnWriteArrayList;
26+
import java.util.concurrent.CopyOnWriteArraySet;
27+
28+
import static java.util.Comparator.comparingInt;
2629

2730
/**
2831
* A directory of restrictions that can be looked up by their type.
@@ -33,7 +36,7 @@ public class RestrictionLookup<M> {
3336
/**
3437
* The restrictions.
3538
*/
36-
private final Collection<Restriction<? super M>> restrictions = new CopyOnWriteArrayList<>();
39+
private final Set<Restriction<? super M>> restrictions = new CopyOnWriteArraySet<>();
3740

3841
/**
3942
* The restrictions by class. As the actual restriction instances are proxied by CDI, this map cannot be
@@ -48,6 +51,7 @@ public class RestrictionLookup<M> {
4851
*/
4952
public void addAllRestrictions(Collection<Restriction<? super M>> restrictions) {
5053
this.restrictions.addAll(restrictions);
54+
restrictionByClass.clear();
5155
}
5256

5357
/**
@@ -62,10 +66,20 @@ public Restriction<? super M> getRestriction(Class<?> restrictionClass) {
6266
key -> restrictions.stream()
6367
// we cannot use a map as the classes are proxied by CDI
6468
.filter(key::isInstance)
65-
.findAny()
69+
.min(comparingInt(restriction -> getInheritanceDistance(restriction, key)))
6670
.orElse(null));
6771
}
6872

73+
private static int getInheritanceDistance(Restriction<?> restriction, Class<?> restrictionClass) {
74+
int distance = 0;
75+
for (Class<?> clazz = restriction.getClass();
76+
!clazz.equals(restrictionClass) && restrictionClass.isAssignableFrom(clazz);
77+
clazz = clazz.getSuperclass()) {
78+
distance++;
79+
}
80+
return distance;
81+
}
82+
6983
@Override
7084
public String toString() {
7185
return new StringJoiner(", ", RestrictionLookup.class.getSimpleName() + "[", "]")

0 commit comments

Comments
 (0)