Project

General

Profile

Download (7.36 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.cdm.strategy.match;
12

    
13
import org.apache.log4j.Logger;
14

    
15
import eu.etaxonomy.cdm.common.CdmUtils;
16

    
17

    
18
/**
19
 * Enumeration for matching modes.
20
 * @author a.mueller
21
 * @created 31.07.2009
22
 * @version 1.0
23
 */
24
public enum MatchMode {
25
	EQUAL_REQUIRED,  //parameters must be equal and not null
26
	EQUAL,			 //parameters must be equal or both null
27
	EQUAL_OR_ONE_NULL,   //parameters must be equal at least one parameter is null
28
	EQUAL_OR_SECOND_NULL,
29
	IGNORE,			//matches allways
30
	MATCH_REQUIRED,
31
	MATCH_OR_ONE_NULL,
32
	MATCH_OR_SECOND_NULL,
33
	MATCH,//matches if parameter mach (parameters must implement IMatchable)
34
	CACHE
35
	;
36
	private static final Logger logger = Logger.getLogger(MatchMode.class);
37

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

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

    
87

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

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

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

    
141

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

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

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