Revision 4cab2fd1
Added by Andreas Müller almost 6 years ago
cdmlib-model/src/main/java/eu/etaxonomy/cdm/strategy/match/MatchMode.java | ||
---|---|---|
30 | 30 |
MATCH,//matches if parameter match (parameters must implement IMatchable) |
31 | 31 |
CACHE |
32 | 32 |
; |
33 |
//a possible further MatchMode could be NULL, saying that all instances of the given type should have a null |
|
34 |
//value, otherwise dirty data which should not be matched |
|
35 |
//e.g. NewParsedJournalInstance-authorship |
|
36 |
|
|
33 | 37 |
private static final Logger logger = Logger.getLogger(MatchMode.class); |
34 | 38 |
|
35 |
public boolean matches(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException{
|
|
39 |
public MatchResult matches(Object obj1, Object obj2, IMatchStrategy matchStrategy, String fieldName, boolean failAll) throws MatchException{
|
|
36 | 40 |
if (this == EQUAL_REQUIRED){ |
37 |
return matchesEqualRequired(obj1, obj2); |
|
41 |
return matchesEqualRequired(obj1, obj2, fieldName);
|
|
38 | 42 |
}else if(this == EQUAL){ |
39 |
return matchesEqual(obj1, obj2); |
|
43 |
return matchesEqual(obj1, obj2, fieldName);
|
|
40 | 44 |
}else if (this == EQUAL_OR_ONE_NULL){ |
41 |
return matchesEqualOrOneNull(obj1, obj2); |
|
45 |
return matchesEqualOrOneNull(obj1, obj2, fieldName);
|
|
42 | 46 |
}else if (this == EQUAL_OR_SECOND_NULL){ |
43 |
return matchesEqualOrSecondNull(obj1, obj2); |
|
47 |
return matchesEqualOrSecondNull(obj1, obj2, fieldName);
|
|
44 | 48 |
}else if(this == IGNORE){ |
45 | 49 |
return matchesIgnore(obj1, obj2); |
46 | 50 |
}else if(this == MATCH){ |
47 |
return matchesMatch(obj1, obj2, matchStrategy); |
|
51 |
return matchesMatch(obj1, obj2, matchStrategy, fieldName, failAll);
|
|
48 | 52 |
}else if(this == MATCH_REQUIRED){ |
49 |
return matchesMatchRequired(obj1, obj2, matchStrategy); |
|
53 |
return matchesMatchRequired(obj1, obj2, matchStrategy, fieldName, failAll);
|
|
50 | 54 |
}else if(this == MATCH_OR_ONE_NULL){ |
51 |
return matchesMatchOrOneNull(obj1, obj2, matchStrategy); |
|
55 |
return matchesMatchOrOneNull(obj1, obj2, matchStrategy, fieldName, failAll);
|
|
52 | 56 |
}else if(this == MATCH_OR_SECOND_NULL){ |
53 |
return matchesMatchOrSecondNull(obj1, obj2, matchStrategy); |
|
57 |
return matchesMatchOrSecondNull(obj1, obj2, matchStrategy, fieldName, failAll);
|
|
54 | 58 |
}else if(this == CACHE){ |
55 |
return matchesEqualRequired(obj1, obj2) && StringUtils.isNotBlank((String)obj1);
|
|
59 |
return matchCache(obj1, obj2, fieldName);
|
|
56 | 60 |
}else { |
57 | 61 |
throw new MatchException("Match mode not handled yet: " + this); |
58 | 62 |
} |
... | ... | |
61 | 65 |
|
62 | 66 |
|
63 | 67 |
/** |
68 |
* @param obj1 |
|
69 |
* @param obj2 |
|
70 |
* @return |
|
71 |
*/ |
|
72 |
private MatchResult matchCache(Object obj1, Object obj2, String fieldName) { |
|
73 |
if (StringUtils.isBlank((String)obj1)){ |
|
74 |
return MatchResult.NewInstance(fieldName, this,obj1, obj2); |
|
75 |
}else{ |
|
76 |
return matchesEqualRequired(obj1, obj2, fieldName); |
|
77 |
} |
|
78 |
} |
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
/** |
|
64 | 83 |
* @param obj1 |
65 | 84 |
* @param obj2 |
66 | 85 |
* @param matchStrategy |
67 | 86 |
* @return |
68 | 87 |
* @throws MatchException |
69 | 88 |
*/ |
70 |
private boolean matchesMatchRequired(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException {
|
|
89 |
private MatchResult matchesMatchRequired(Object obj1, Object obj2, IMatchStrategy matchStrategy, String fieldName, boolean failAll) throws MatchException {
|
|
71 | 90 |
if (obj1 == null || obj2 == null ){ |
72 |
return false;
|
|
91 |
return MatchResult.NewInstance(fieldName, this, obj1, obj2);
|
|
73 | 92 |
}else if (! (obj1 instanceof IMatchable && obj2 instanceof IMatchable) ){ |
74 | 93 |
logger.warn("Match objects are not of type IMatchable"); |
75 |
return matchesEqualRequired(obj1, obj2); |
|
94 |
return matchesEqualRequired(obj1, obj2, fieldName);
|
|
76 | 95 |
}else{ |
77 | 96 |
if (matchStrategy == null){ |
78 | 97 |
matchStrategy = DefaultMatchStrategy.NewInstance((Class<? extends IMatchable>) obj1.getClass()); |
79 | 98 |
} |
80 |
return matchStrategy.invoke((IMatchable)obj1, (IMatchable)obj2); |
|
99 |
return matchStrategy.invoke((IMatchable)obj1, (IMatchable)obj2, failAll);
|
|
81 | 100 |
} |
82 | 101 |
} |
83 | 102 |
|
... | ... | |
88 | 107 |
* @return |
89 | 108 |
* @throws MatchException |
90 | 109 |
*/ |
91 |
private boolean matchesMatchOrOneNull(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException { |
|
110 |
private MatchResult matchesMatchOrOneNull(Object obj1, Object obj2, |
|
111 |
IMatchStrategy matchStrategy, String fieldName, boolean failAll) throws MatchException { |
|
92 | 112 |
if (obj1 == null || obj2 == null ){ |
93 |
return true;
|
|
113 |
return MatchResult.SUCCESS();
|
|
94 | 114 |
}else { |
95 |
return matchesMatchRequired(obj1, obj2, matchStrategy); |
|
115 |
return matchesMatchRequired(obj1, obj2, matchStrategy, fieldName, failAll);
|
|
96 | 116 |
} |
97 | 117 |
} |
98 | 118 |
|
... | ... | |
102 | 122 |
* @return |
103 | 123 |
* @throws MatchException |
104 | 124 |
*/ |
105 |
private boolean matchesMatchOrSecondNull(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException { |
|
106 |
if (obj1 == null ){ |
|
107 |
return true; |
|
125 |
private MatchResult matchesMatchOrSecondNull(Object obj1, Object obj2, |
|
126 |
IMatchStrategy matchStrategy, String fieldName, boolean failAll) throws MatchException { |
|
127 |
if (obj2 == null ){ |
|
128 |
return MatchResult.SUCCESS(); |
|
108 | 129 |
}else { |
109 |
return matchesMatchRequired(obj1, obj2, matchStrategy); |
|
130 |
return matchesMatchRequired(obj1, obj2, matchStrategy, fieldName, failAll);
|
|
110 | 131 |
} |
111 | 132 |
} |
112 | 133 |
|
... | ... | |
118 | 139 |
* @return |
119 | 140 |
* @throws MatchException |
120 | 141 |
*/ |
121 |
private boolean matchesMatch(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException { |
|
142 |
private MatchResult matchesMatch(Object obj1, Object obj2, |
|
143 |
IMatchStrategy matchStrategy, String fieldName, boolean failAll) throws MatchException { |
|
122 | 144 |
if (obj1 == null && obj2 == null ){ |
123 |
return true;
|
|
145 |
return MatchResult.SUCCESS();
|
|
124 | 146 |
}else { |
125 |
return matchesMatchRequired(obj1, obj2, matchStrategy); |
|
147 |
return matchesMatchRequired(obj1, obj2, matchStrategy, fieldName, failAll);
|
|
126 | 148 |
} |
127 | 149 |
} |
128 | 150 |
|
... | ... | |
131 | 153 |
* @param obj2 |
132 | 154 |
* @return |
133 | 155 |
*/ |
134 |
private boolean matchesIgnore(Object obj1, Object obj2) {
|
|
135 |
return true;
|
|
156 |
private MatchResult matchesIgnore(Object obj1, Object obj2) {
|
|
157 |
return MatchResult.SUCCESS();
|
|
136 | 158 |
} |
137 | 159 |
|
138 | 160 |
|
... | ... | |
141 | 163 |
* @param obj2 |
142 | 164 |
* @return |
143 | 165 |
*/ |
144 |
private boolean matchesEqualRequired(Object obj1, Object obj2) { |
|
145 |
if (obj1 == null || obj2 == null || ! obj1.equals(obj2)){ |
|
146 |
return false; |
|
166 |
private MatchResult matchesEqualRequired(Object obj1, Object obj2, String fieldName) { |
|
167 |
if (obj1 == null || obj2 == null){ |
|
168 |
return MatchResult.NewInstance(fieldName, this, obj1, obj2); |
|
169 |
}else if (! obj1.equals(obj2)) { |
|
170 |
return MatchResult.NewInstance(fieldName, this, obj1, obj2); |
|
147 | 171 |
}else{ |
148 |
return true;
|
|
172 |
return MatchResult.SUCCESS();
|
|
149 | 173 |
} |
150 | 174 |
} |
151 | 175 |
|
... | ... | |
155 | 179 |
* @param obj2 |
156 | 180 |
* @return |
157 | 181 |
*/ |
158 |
private boolean matchesEqualOrOneNull(Object obj1, Object obj2) {
|
|
182 |
private MatchResult matchesEqualOrOneNull(Object obj1, Object obj2, String fieldName) {
|
|
159 | 183 |
if (obj1 == null || obj2 == null){ |
160 |
return true;
|
|
184 |
return MatchResult.SUCCESS();
|
|
161 | 185 |
}else{ |
162 |
return matchesEqualRequired(obj1, obj2); |
|
186 |
return matchesEqualRequired(obj1, obj2, fieldName);
|
|
163 | 187 |
} |
164 | 188 |
} |
165 | 189 |
|
... | ... | |
169 | 193 |
* @param obj2 |
170 | 194 |
* @return |
171 | 195 |
*/ |
172 |
private boolean matchesEqualOrSecondNull(Object obj1, Object obj2) {
|
|
196 |
private MatchResult matchesEqualOrSecondNull(Object obj1, Object obj2, String fieldName) {
|
|
173 | 197 |
if (obj2 == null){ |
174 |
return true;
|
|
198 |
return MatchResult.SUCCESS();
|
|
175 | 199 |
}else{ |
176 |
return matchesEqual(obj1, obj2); |
|
200 |
return matchesEqual(obj1, obj2, fieldName);
|
|
177 | 201 |
} |
178 | 202 |
} |
179 | 203 |
|
... | ... | |
183 | 207 |
* @param obj2 |
184 | 208 |
* @return |
185 | 209 |
*/ |
186 |
private boolean matchesEqual(Object obj1, Object obj2) {
|
|
210 |
private MatchResult matchesEqual(Object obj1, Object obj2, String fieldName) {
|
|
187 | 211 |
if (obj1 == null && obj2 == null){ |
188 |
return true;
|
|
212 |
return MatchResult.SUCCESS();
|
|
189 | 213 |
}else { |
190 |
return matchesEqualRequired(obj1, obj2); |
|
214 |
return matchesEqualRequired(obj1, obj2, fieldName);
|
|
191 | 215 |
} |
192 | 216 |
} |
193 | 217 |
|
Also available in: Unified diff
Add series parsing with letters (test)