Project

General

Profile

Download (7.35 KB) Statistics
| Branch: | Tag: | Revision:
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
 * @created 31.07.2009
21
 * @version 1.0
22
 */
23
public enum MatchMode {
24
	EQUAL_REQUIRED,  //parameters must be equal and not null
25
	EQUAL,			 //parameters must be equal or both null
26
	EQUAL_OR_ONE_NULL,   //parameters must be equal at least one parameter is null
27
	EQUAL_OR_SECOND_NULL,
28
	IGNORE,			//matches allways
29
	MATCH_REQUIRED,
30
	MATCH_OR_ONE_NULL,
31
	MATCH_OR_SECOND_NULL,
32
	MATCH,//matches if parameter mach (parameters must implement IMatchable)
33
	CACHE
34
	;
35
	private static final Logger logger = Logger.getLogger(MatchMode.class);
36

    
37
	public boolean matches(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException{
38
		if (this == EQUAL_REQUIRED){
39
			return matchesEqualRequired(obj1, obj2);
40
		}else if(this == EQUAL){
41
			return matchesEqual(obj1, obj2);
42
		}else if (this == EQUAL_OR_ONE_NULL){
43
			return matchesEqualOrOneNull(obj1, obj2);
44
		}else if (this == EQUAL_OR_SECOND_NULL){
45
			return matchesEqualOrSecondNull(obj1, obj2);
46
		}else if(this == IGNORE){
47
			return matchesIgnore(obj1, obj2);
48
		}else if(this == MATCH){
49
			return matchesMatch(obj1, obj2, matchStrategy);
50
		}else if(this == MATCH_REQUIRED){
51
			return matchesMatchRequired(obj1, obj2, matchStrategy);
52
		}else if(this == MATCH_OR_ONE_NULL){
53
			return matchesMatchOrOneNull(obj1, obj2, matchStrategy);
54
		}else if(this == MATCH_OR_SECOND_NULL){
55
			return matchesMatchOrSecondNull(obj1, obj2, matchStrategy);
56
		}else if(this == CACHE){
57
			return matchesEqualRequired(obj1, obj2) && CdmUtils.isNotEmpty((String)obj1);
58
		}else {
59
			throw new MatchException("Match mode not handled yet: " + this);
60
		}
61
	}
62

    
63
	
64
	
65
	/**
66
	 * @param obj1
67
	 * @param obj2
68
	 * @param matchStrategy 
69
	 * @return
70
	 * @throws MatchException 
71
	 */
72
	private boolean matchesMatchRequired(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException {
73
		if (obj1 == null || obj2 == null ){
74
			return false;
75
		}else if (! (obj1 instanceof IMatchable  && obj2 instanceof IMatchable) ){
76
			logger.warn("Match objects are not of type IMatchable");
77
			return matchesEqualRequired(obj1, obj2);
78
		}else{
79
			if (matchStrategy == null){
80
				matchStrategy = DefaultMatchStrategy.NewInstance((Class<? extends IMatchable>) obj1.getClass());
81
			}
82
			return matchStrategy.invoke((IMatchable)obj1, (IMatchable)obj2);
83
		}
84
	}
85

    
86

    
87
	/**
88
	 * @param obj1
89
	 * @param obj2
90
	 * @return
91
	 * @throws MatchException 
92
	 */
93
	private boolean matchesMatchOrOneNull(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException {
94
		if (obj1 == null || obj2 == null ){
95
			return true;
96
		}else {
97
			return matchesMatchRequired(obj1, obj2, matchStrategy);
98
		}
99
	}
100

    
101
	/**
102
	 * @param obj1
103
	 * @param obj2
104
	 * @return
105
	 * @throws MatchException 
106
	 */
107
	private boolean matchesMatchOrSecondNull(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException {
108
		if (obj1 == null ){
109
			return true;
110
		}else {
111
			return matchesMatchRequired(obj1, obj2, matchStrategy);
112
		}
113
	}
114

    
115
	
116
	/**
117
	 * @param obj1
118
	 * @param obj2
119
	 * @param matchStrategy 
120
	 * @return
121
	 * @throws MatchException 
122
	 */
123
	private boolean matchesMatch(Object obj1, Object obj2, IMatchStrategy matchStrategy) throws MatchException {
124
		if (obj1 == null && obj2 == null ){
125
			return true;
126
		}else {
127
			return matchesMatchRequired(obj1, obj2, matchStrategy);
128
		}
129
	}
130
	
131
	/**
132
	 * @param obj1
133
	 * @param obj2
134
	 * @return
135
	 */
136
	private boolean matchesIgnore(Object obj1, Object obj2) {
137
		return true;
138
	}
139

    
140

    
141
	/**
142
	 * @param obj1
143
	 * @param obj2
144
	 * @return
145
	 */
146
	private boolean matchesEqualRequired(Object obj1, Object obj2) {
147
		if (obj1 == null || obj2 == null || ! obj1.equals(obj2)){
148
			return false;
149
		}else{
150
			return true;
151
		}
152
	}
153

    
154
	
155
	/**
156
	 * @param obj1
157
	 * @param obj2
158
	 * @return
159
	 */
160
	private boolean matchesEqualOrOneNull(Object obj1, Object obj2) {
161
		if (obj1 == null || obj2 == null){
162
			return true;
163
		}else{
164
			return matchesEqualRequired(obj1, obj2);
165
		}
166
	}
167
	
168
	
169
	/**
170
	 * @param obj1
171
	 * @param obj2
172
	 * @return
173
	 */
174
	private boolean matchesEqualOrSecondNull(Object obj1, Object obj2) {
175
		if (obj2 == null){
176
			return true;
177
		}else{
178
			return matchesEqual(obj1, obj2);
179
		}
180
	}
181
	
182

    
183
	/**
184
	 * @param obj1
185
	 * @param obj2
186
	 * @return
187
	 */
188
	private boolean matchesEqual(Object obj1, Object obj2) {
189
		if (obj1 == null && obj2 == null){
190
			return true;
191
		}else {
192
			return matchesEqualRequired(obj1, obj2);
193
		}
194
	}
195
	
196
	//not needed?
197
	public boolean allowsSecondNull(Object first){
198
		if (isRequired()){
199
			return false;
200
		}else if(first == null){
201
			return true; 
202
		}else {
203
			return allowsExactlyOneNull(); 
204
		}
205
	}
206
	
207
	/**
208
	 * Returns true is this match mode can be ignored for retrieving
209
	 * matching objects
210
	 * @param first
211
	 * @return
212
	 */
213
	public boolean isIgnore(Object first){
214
		if (this == IGNORE){
215
			return true;
216
		}else if (first == null && (isXOrOneNull())){
217
				return true;
218
		}else{
219
			return false;
220
		}
221
	}
222
		
223
	/**
224
	 * Returns true if a non-null value is required for finding
225
	 * matching objects
226
	 * @param first
227
	 * @return
228
	 */
229
	public boolean requiresSecondValue(Object first){
230
		if (first == null){
231
			return false;
232
		}else{
233
			return ! allowsExactlyOneNull() ;
234
		}
235
		
236
	}
237
	
238
	/**
239
	 * Returns true if a null value is required for retrieveing
240
	 * matching objects
241
	 * @param first
242
	 * @return
243
	 * @throws MatchException if first is null and matching a non-null value is required
244
	 */
245
	public boolean requiresSecondNull(Object first) throws MatchException{
246
		if (first != null){
247
			return false;
248
		}else if (isRequired()){
249
			throw new MatchException("MatchMode " + this + " does not allow (null)");
250
		}else{ 
251
			return ! isXOrOneNull();
252
		}
253
	}
254
	
255
	/**
256
	 * Returns true if a non-null value is required, independent from the first value
257
	 * @return
258
	 */
259
	public boolean isRequired(){
260
		return ((this == EQUAL_REQUIRED) || (this == MATCH_REQUIRED));
261
	}
262
	
263
	/**
264
	 * Returns true, if this match mode allows that one value is null and the other is not null.
265
	 * @return
266
	 */
267
	private boolean allowsExactlyOneNull(){
268
		return (isXOrOneNull() ||
269
				(this == EQUAL_OR_SECOND_NULL)|| (this == MATCH_OR_SECOND_NULL) ||
270
				(this == IGNORE));
271
	}
272
	
273
	/**
274
	 * Returns true, if this match mode is of type MATCHXXX
275
	 * @return
276
	 */
277
	public boolean isMatch(){
278
		return ((this == MATCH_REQUIRED) || (this == MATCH_OR_ONE_NULL) ||
279
				(this == MATCH)|| (this == MATCH_OR_SECOND_NULL) ||
280
				(this == MATCH_OR_ONE_NULL));
281
	}
282
	
283
	/**
284
	 * Returns true, if this match mode is of type EQUALXXX
285
	 * @return
286
	 */
287
	public boolean isEqual(){
288
		return ((this == EQUAL_REQUIRED) || (this == EQUAL_OR_ONE_NULL) ||
289
				(this == EQUAL)|| (this == EQUAL_OR_SECOND_NULL) ||
290
				(this == EQUAL_OR_ONE_NULL));
291
	}
292
	
293
	/**
294
	 * Returns true, if this match mode is of type XXX_OR_ONE_NULL
295
	 * @return
296
	 */
297
	public boolean isXOrOneNull(){
298
		return (this == EQUAL_OR_ONE_NULL) || (this == MATCH_OR_ONE_NULL);	
299
		
300
	}	
301
}
(9-9/11)