-
Notifications
You must be signed in to change notification settings - Fork 188
/
MatchingRuleRegistryImpl.java
72 lines (62 loc) · 2.47 KB
/
MatchingRuleRegistryImpl.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
70
71
72
/*
* Copyright (c) 2010-2018 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.prism.impl.match;
import com.evolveum.midpoint.prism.match.MatchingRule;
import com.evolveum.midpoint.prism.match.MatchingRuleRegistry;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
import org.jetbrains.annotations.NotNull;
import javax.xml.namespace.QName;
import java.util.HashMap;
import java.util.Map;
/**
* @author semancik
*
*/
public class MatchingRuleRegistryImpl implements MatchingRuleRegistry {
@NotNull private final MatchingRule<?> defaultMatchingRule;
@NotNull private final Map<QName, MatchingRule<?>> matchingRules = new HashMap<>();
MatchingRuleRegistryImpl() {
this.defaultMatchingRule = new DefaultMatchingRule<>();
}
// if typeQName is null, we skip the rule-type correspondence test
@Override
@NotNull
public <T> MatchingRule<T> getMatchingRule(QName ruleName, QName typeQName) throws SchemaException {
if (ruleName == null) {
//noinspection unchecked
return (MatchingRule<T>) defaultMatchingRule;
}
MatchingRule<T> matchingRule = findMatchingRule(ruleName);
if (typeQName == null || matchingRule.supports(typeQName)) {
return matchingRule;
} else {
throw new SchemaException("Matching rule " + ruleName + " does not support type " + typeQName);
}
}
@NotNull
private <T> MatchingRule<T> findMatchingRule(QName ruleName) throws SchemaException {
//noinspection unchecked
MatchingRule<T> rule = (MatchingRule<T>) matchingRules.get(ruleName);
if (rule != null) {
return rule;
}
// try match according to the localPart
QName qualifiedRuleName = QNameUtil.resolveNs(ruleName, matchingRules.keySet());
if (qualifiedRuleName != null) {
//noinspection unchecked
MatchingRule<T> ruleAfterQualification = (MatchingRule<T>) matchingRules.get(qualifiedRuleName);
if (ruleAfterQualification != null) {
return ruleAfterQualification;
}
}
throw new SchemaException("Couldn't find matching rule named '" + ruleName + "'");
}
void registerMatchingRule(MatchingRule<?> rule) {
matchingRules.put(rule.getName(), rule);
}
}