1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.strategy.match;
|
11
|
|
12
|
import org.apache.log4j.Logger;
|
13
|
|
14
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
15
|
|
16
|
|
17
|
/**
|
18
|
* Enumeration for matching modes.
|
19
|
* @author a.mueller
|
20
|
* @since 31.07.2009
|
21
|
*/
|
22
|
public enum MatchMode {
|
23
|
EQUAL_REQUIRED, //parameters must be equal and not null
|
24
|
EQUAL, //parameters must be equal or both null
|
25
|
EQUAL_OR_ONE_NULL, //parameters must be equal or at least one parameter is null
|
26
|
EQUAL_OR_SECOND_NULL,
|
27
|
IGNORE, //matches always
|
28
|
MATCH_REQUIRED,
|
29
|
MATCH_OR_ONE_NULL,
|
30
|
MATCH_OR_SECOND_NULL,
|
31
|
MATCH,//matches if parameter match (parameters must implement IMatchable)
|
32
|
CACHE
|
33
|
;
|
34
|
private static final Logger logger = Logger.getLogger(MatchMode.class);
|
35
|
|
36
|
public boolean matches(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException{
|
37
|
if (this == EQUAL_REQUIRED){
|
38
|
return matchesEqualRequired(obj1, obj2);
|
39
|
}else if(this == EQUAL){
|
40
|
return matchesEqual(obj1, obj2);
|
41
|
}else if (this == EQUAL_OR_ONE_NULL){
|
42
|
return matchesEqualOrOneNull(obj1, obj2);
|
43
|
}else if (this == EQUAL_OR_SECOND_NULL){
|
44
|
return matchesEqualOrSecondNull(obj1, obj2);
|
45
|
}else if(this == IGNORE){
|
46
|
return matchesIgnore(obj1, obj2);
|
47
|
}else if(this == MATCH){
|
48
|
return matchesMatch(obj1, obj2, matchStrategy);
|
49
|
}else if(this == MATCH_REQUIRED){
|
50
|
return matchesMatchRequired(obj1, obj2, matchStrategy);
|
51
|
}else if(this == MATCH_OR_ONE_NULL){
|
52
|
return matchesMatchOrOneNull(obj1, obj2, matchStrategy);
|
53
|
}else if(this == MATCH_OR_SECOND_NULL){
|
54
|
return matchesMatchOrSecondNull(obj1, obj2, matchStrategy);
|
55
|
}else if(this == CACHE){
|
56
|
return matchesEqualRequired(obj1, obj2) && CdmUtils.isNotEmpty((String)obj1);
|
57
|
}else {
|
58
|
throw new MatchException("Match mode not handled yet: " + this);
|
59
|
}
|
60
|
}
|
61
|
|
62
|
|
63
|
|
64
|
/**
|
65
|
* @param obj1
|
66
|
* @param obj2
|
67
|
* @param matchStrategy
|
68
|
* @return
|
69
|
* @throws MatchException
|
70
|
*/
|
71
|
private boolean matchesMatchRequired(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException {
|
72
|
if (obj1 == null || obj2 == null ){
|
73
|
return false;
|
74
|
}else if (! (obj1 instanceof IMatchable && obj2 instanceof IMatchable) ){
|
75
|
logger.warn("Match objects are not of type IMatchable");
|
76
|
return matchesEqualRequired(obj1, obj2);
|
77
|
}else{
|
78
|
if (matchStrategy == null){
|
79
|
matchStrategy = DefaultMatchStrategy.NewInstance((Class<? extends IMatchable>) obj1.getClass());
|
80
|
}
|
81
|
return matchStrategy.invoke((IMatchable)obj1, (IMatchable)obj2);
|
82
|
}
|
83
|
}
|
84
|
|
85
|
|
86
|
/**
|
87
|
* @param obj1
|
88
|
* @param obj2
|
89
|
* @return
|
90
|
* @throws MatchException
|
91
|
*/
|
92
|
private boolean matchesMatchOrOneNull(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException {
|
93
|
if (obj1 == null || obj2 == null ){
|
94
|
return true;
|
95
|
}else {
|
96
|
return matchesMatchRequired(obj1, obj2, matchStrategy);
|
97
|
}
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* @param obj1
|
102
|
* @param obj2
|
103
|
* @return
|
104
|
* @throws MatchException
|
105
|
*/
|
106
|
private boolean matchesMatchOrSecondNull(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException {
|
107
|
if (obj1 == null ){
|
108
|
return true;
|
109
|
}else {
|
110
|
return matchesMatchRequired(obj1, obj2, matchStrategy);
|
111
|
}
|
112
|
}
|
113
|
|
114
|
|
115
|
/**
|
116
|
* @param obj1
|
117
|
* @param obj2
|
118
|
* @param matchStrategy
|
119
|
* @return
|
120
|
* @throws MatchException
|
121
|
*/
|
122
|
private boolean matchesMatch(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException {
|
123
|
if (obj1 == null && obj2 == null ){
|
124
|
return true;
|
125
|
}else {
|
126
|
return matchesMatchRequired(obj1, obj2, matchStrategy);
|
127
|
}
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* @param obj1
|
132
|
* @param obj2
|
133
|
* @return
|
134
|
*/
|
135
|
private boolean matchesIgnore(Object obj1, Object obj2) {
|
136
|
return true;
|
137
|
}
|
138
|
|
139
|
|
140
|
/**
|
141
|
* @param obj1
|
142
|
* @param obj2
|
143
|
* @return
|
144
|
*/
|
145
|
private boolean matchesEqualRequired(Object obj1, Object obj2) {
|
146
|
if (obj1 == null || obj2 == null || ! obj1.equals(obj2)){
|
147
|
return false;
|
148
|
}else{
|
149
|
return true;
|
150
|
}
|
151
|
}
|
152
|
|
153
|
|
154
|
/**
|
155
|
* @param obj1
|
156
|
* @param obj2
|
157
|
* @return
|
158
|
*/
|
159
|
private boolean matchesEqualOrOneNull(Object obj1, Object obj2) {
|
160
|
if (obj1 == null || obj2 == null){
|
161
|
return true;
|
162
|
}else{
|
163
|
return matchesEqualRequired(obj1, obj2);
|
164
|
}
|
165
|
}
|
166
|
|
167
|
|
168
|
/**
|
169
|
* @param obj1
|
170
|
* @param obj2
|
171
|
* @return
|
172
|
*/
|
173
|
private boolean matchesEqualOrSecondNull(Object obj1, Object obj2) {
|
174
|
if (obj2 == null){
|
175
|
return true;
|
176
|
}else{
|
177
|
return matchesEqual(obj1, obj2);
|
178
|
}
|
179
|
}
|
180
|
|
181
|
|
182
|
/**
|
183
|
* @param obj1
|
184
|
* @param obj2
|
185
|
* @return
|
186
|
*/
|
187
|
private boolean matchesEqual(Object obj1, Object obj2) {
|
188
|
if (obj1 == null && obj2 == null){
|
189
|
return true;
|
190
|
}else {
|
191
|
return matchesEqualRequired(obj1, obj2);
|
192
|
}
|
193
|
}
|
194
|
|
195
|
//not needed?
|
196
|
public boolean allowsSecondNull(Object first){
|
197
|
if (isRequired()){
|
198
|
return false;
|
199
|
}else if(first == null){
|
200
|
return true;
|
201
|
}else {
|
202
|
return allowsExactlyOneNull();
|
203
|
}
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* Returns true is this match mode can be ignored for retrieving
|
208
|
* matching objects
|
209
|
* @param first
|
210
|
* @return
|
211
|
*/
|
212
|
public boolean isIgnore(Object first){
|
213
|
if (this == IGNORE){
|
214
|
return true;
|
215
|
}else if (first == null && (isXOrOneNull())){
|
216
|
return true;
|
217
|
}else{
|
218
|
return false;
|
219
|
}
|
220
|
}
|
221
|
|
222
|
/**
|
223
|
* Returns true if a non-null value is required for finding
|
224
|
* matching objects
|
225
|
* @param first
|
226
|
* @return
|
227
|
*/
|
228
|
public boolean requiresSecondValue(Object first){
|
229
|
if (first == null){
|
230
|
return false;
|
231
|
}else{
|
232
|
return ! allowsExactlyOneNull() ;
|
233
|
}
|
234
|
|
235
|
}
|
236
|
|
237
|
/**
|
238
|
* Returns true if a null value is required for retrieveing
|
239
|
* matching objects
|
240
|
* @param first
|
241
|
* @return
|
242
|
* @throws MatchException if first is null and matching a non-null value is required
|
243
|
*/
|
244
|
public boolean requiresSecondNull(Object first) throws MatchException{
|
245
|
if (first != null){
|
246
|
return false;
|
247
|
}else if (isRequired()){
|
248
|
throw new MatchException("MatchMode " + this + " does not allow (null)");
|
249
|
}else{
|
250
|
return ! isXOrOneNull();
|
251
|
}
|
252
|
}
|
253
|
|
254
|
/**
|
255
|
* Returns true if a non-null value is required, independent from the first value
|
256
|
* @return
|
257
|
*/
|
258
|
public boolean isRequired(){
|
259
|
return ((this == EQUAL_REQUIRED) || (this == MATCH_REQUIRED));
|
260
|
}
|
261
|
|
262
|
/**
|
263
|
* Returns true, if this match mode allows that one value is null and the other is not null.
|
264
|
* @return
|
265
|
*/
|
266
|
private boolean allowsExactlyOneNull(){
|
267
|
return (isXOrOneNull() ||
|
268
|
(this == EQUAL_OR_SECOND_NULL)|| (this == MATCH_OR_SECOND_NULL) ||
|
269
|
(this == IGNORE));
|
270
|
}
|
271
|
|
272
|
/**
|
273
|
* Returns true, if this match mode is of type MATCHXXX
|
274
|
* @return
|
275
|
*/
|
276
|
public boolean isMatch(){
|
277
|
return ((this == MATCH_REQUIRED) || (this == MATCH_OR_ONE_NULL) ||
|
278
|
(this == MATCH)|| (this == MATCH_OR_SECOND_NULL) ||
|
279
|
(this == MATCH_OR_ONE_NULL));
|
280
|
}
|
281
|
|
282
|
/**
|
283
|
* Returns true, if this match mode is of type EQUALXXX
|
284
|
* @return
|
285
|
*/
|
286
|
public boolean isEqual(){
|
287
|
return ((this == EQUAL_REQUIRED) || (this == EQUAL_OR_ONE_NULL) ||
|
288
|
(this == EQUAL)|| (this == EQUAL_OR_SECOND_NULL) ||
|
289
|
(this == EQUAL_OR_ONE_NULL));
|
290
|
}
|
291
|
|
292
|
/**
|
293
|
* Returns true, if this match mode is of type XXX_OR_ONE_NULL
|
294
|
* @return
|
295
|
*/
|
296
|
public boolean isXOrOneNull(){
|
297
|
return (this == EQUAL_OR_ONE_NULL) || (this == MATCH_OR_ONE_NULL);
|
298
|
|
299
|
}
|
300
|
}
|