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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Michael Powers
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.control;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import net.wotonomy.foundation.NSArray;
import net.wotonomy.foundation.NSMutableArray;
import net.wotonomy.foundation.NSMutableDictionary;
import net.wotonomy.foundation.NSSelector;
import net.wotonomy.foundation.NSSet;
import net.wotonomy.foundation.internal.ValueConverter;
import net.wotonomy.foundation.internal.WotonomyException;
/**
* EOQualifiers are used to perform property-based qualifications on objects:
* for a set of criteria, a qualifier either qualifies or disqualifies an given
* object. EOKeyValueQualifiers can be joined by EOAndQualifier and
* EOOrQualifier, and so can form a tree of qualifications. <br>
* <br>
*
* Certain qualifiers can accept a variable in place of a value; variable names
* are marked by a "$", as in "$name". Variables are resolved with the
* qualifierWithBindings() method.
*
* @author michael@mpowers.net
* @author yjcheung@intersectsoft.com
* @author $Author: cgruber $
* @version $Revision: 894 $
*/
public abstract class EOQualifier {
public static final NSSelector QualifierOperatorCaseInsensitiveLike = new OperatorCaseInsensitiveLike();
public static final NSSelector QualifierOperatorContains = new OperatorContains();
public static final NSSelector QualifierOperatorEqual = new OperatorEqual();
public static final NSSelector QualifierOperatorGreaterThan = new OperatorGreaterThan();
public static final NSSelector QualifierOperatorGreaterThanOrEqualTo = new OperatorGreaterThanOrEqualTo();
public static final NSSelector QualifierOperatorLessThan = new OperatorLessThan();
public static final NSSelector QualifierOperatorLessThanOrEqualTo = new OperatorLessThanOrEqualTo();
public static final NSSelector QualifierOperatorLike = new OperatorLike();
public static final NSSelector QualifierOperatorNotEqual = new OperatorNotEqual();
/**
* Default constructor.
*/
public EOQualifier() {
}
/**
* Adds all qualifier keys in this qualifier to the specified Set, which is
* expected to be mutable. The tree of qualifiers is traversed and the
* left-hand-side of each expression is added to the set.
*/
public void addQualifierKeysToSet(Set aSet) {
throw new RuntimeException("Not implemented yet.");
}
/**
* Returns a Set of all property names used for comparisons by this qualifier.
* The tree of qualifiers is traversed and the left-hand-side of each expression
* is added to the set.
*/
public NSSet allQualifierKeys() {
throw new RuntimeException("Not implemented yet.");
}
/**
* Returns a List containing the variables used at compare-time by this
* qualifier. Each variable will appear only once in the list.
*/
public NSArray bindingKeys() {
throw new RuntimeException("Not implemented yet.");
}
/**
* Returns whether the specified object meets the criteria defined by this
* qualifier.
*/
public boolean evaluateWithObject(Object anObject) {
return true;
}
/**
* Returns the key (which can be a key path) that is tested against the
* specified binding variable. The tree is traversed looking for the first
* instance of the specified variable, and the corresponding left-hand-side of
* the expression is returned.
*/
public String keyPathForBindingKey(String aVariable) {
throw new RuntimeException("Not implemented yet.");
}
/**
* Returns a qualifier that is like this qualifier, except all variables will be
* replaced with values from the specified Map whose keys match the variable
* names. If requireAll is true, an exception will be thrown if there is no key
* that matches on of the variables in the tree; otherwise, the qualifier
* containing the unmatched variable is removed.
*/
public EOQualifier qualifierWithBindings(Map aMap, boolean requireAll) {
throw new WotonomyException("Not implemented yet.");
}
/**
* Tests whether all the keys in this qualifier can be applied to an object of
* the specified class.
*
* @return A Throwable if the validation fails, otherwise returns null if the
* class can be used with this qualifier.
*/
public Throwable validateKeysWithRootClassDescription(Class aClass) {
throw new WotonomyException("Not implemented yet.");
}
// statics
/**
* Convenience to retain only those objects from the specified List that meet
* the specified qualifier's requirements.
*/
public static void filterArrayWithQualifier(List anObjectList, EOQualifier aQualifier) {
ListIterator iterator = anObjectList.listIterator();
while (iterator.hasNext()) {
if (!aQualifier.evaluateWithObject(iterator.next())) {
iterator.remove();
}
}
}
/**
* Convenience to return a List consisting only of those objects in the
* specified List that meet the specified qualifier's requirements.
*/
public static NSArray filteredArrayWithQualifier(List anObjectList, EOQualifier aQualifier) {
Object o;
List result = new LinkedList();
Iterator iterator = anObjectList.iterator();
while (iterator.hasNext()) {
o = iterator.next();
if (aQualifier.evaluateWithObject(o)) {
result.add(o);
}
}
return new NSArray((Collection) result);
}
/**
* Convenience to create a set of EOKeyValueQualifiers joined by an
* EOAndQualifier. Each pair of keys and values are used to create
* EOKeyValueQualifiers.
*/
public static EOQualifier qualifierToMatchAllValues(Map aMap) {
Object key, value;
List qualifierList = new LinkedList();
Iterator iterator = aMap.keySet().iterator();
while (iterator.hasNext()) {
key = iterator.next();
value = aMap.get(key);
qualifierList.add(new EOKeyValueQualifier(key.toString(), QualifierOperatorEqual, value));
}
return new EOAndQualifier(qualifierList);
}
/**
* Convenience to create a set of EOKeyValueQualifiers joined by an
* EOOrQualifier. Each pair of keys and values are used to create
* EOKeyValueQualifiers.
*/
public static EOQualifier qualifierToMatchAnyValue(Map aMap) {
Object key, value;
List qualifierList = new LinkedList();
Iterator iterator = aMap.keySet().iterator();
while (iterator.hasNext()) {
key = iterator.next();
value = aMap.get(key);
qualifierList.add(new EOKeyValueQualifier(key.toString(), QualifierOperatorEqual, value));
}
return new EOOrQualifier(qualifierList);
}
/**
* Returns an EOQualifier that meets the criteria represented by the specified
* string and variable length argument list. This method parses the string and
* returns a tree of qualifiers. Each token beginning with "%" is replaced with
* the corresponding object from the argument list. The parser recognizes the
* operation tokens returned from the allQualifierOperators() method.
*/
public static EOQualifier qualifierWithQualifierFormat(String aString, List anArgumentList) {
throw new RuntimeException("Not implemented yet.");
}
/**
* Returns a List of operators that are supported for relational operations.
* This excludes string comparison operators.
*/
public static NSArray relationalQualifierOperators() {
NSMutableArray result = new NSMutableArray();
BaseSelector selector;
Iterator iterator = allQualifierOperators().iterator();
while (iterator.hasNext()) {
selector = (BaseSelector) iterator.next();
if (selector.isRelationalOperator()) {
result.addObject(selector);
}
}
return result;
}
/**
* Returns a List of valid operators.
*/
public static NSArray allQualifierOperators() {
return operators.allKeys();
}
/**
* Returns a selector the corresponds to the operation represented by the
* specified string. For example, ">" represents QualifierOperatorGreaterThan.
*/
public static NSSelector operatorSelectorForString(String anOperatorString) {
return (NSSelector) operators.objectForKey(anOperatorString);
}
/**
* Returns a string the corresponds to the operation represented by the
* specified selector. For example, QualifierOperatorGreaterThan is represented
* with ">".
*/
public static String stringForOperatorSelector(NSSelector aSelector) {
return (String) operators.allKeysForObject(aSelector).lastObject();
}
/**
* Returns a string representation of this qualifier.
*/
public String toString() {
// TODO: implement this
return super.toString();
}
// built-in qualifiers
private static NSMutableDictionary operators;
static {
operators = new NSMutableDictionary();
operators.setObjectForKey(QualifierOperatorCaseInsensitiveLike,
QualifierOperatorCaseInsensitiveLike.toString());
operators.setObjectForKey(QualifierOperatorContains, QualifierOperatorContains.toString());
operators.setObjectForKey(QualifierOperatorEqual, QualifierOperatorEqual.toString());
operators.setObjectForKey(QualifierOperatorGreaterThan, QualifierOperatorGreaterThan.toString());
operators.setObjectForKey(QualifierOperatorGreaterThanOrEqualTo,
QualifierOperatorGreaterThanOrEqualTo.toString());
operators.setObjectForKey(QualifierOperatorLessThan, QualifierOperatorLessThan.toString());
operators.setObjectForKey(QualifierOperatorLessThanOrEqualTo, QualifierOperatorLessThanOrEqualTo.toString());
operators.setObjectForKey(QualifierOperatorLike, QualifierOperatorLike.toString());
operators.setObjectForKey(QualifierOperatorNotEqual, QualifierOperatorNotEqual.toString());
}
static private abstract class BaseSelector extends NSSelector {
public String name() {
return "BaseSelector";
}
public Class[] parameterTypes() {
return new Class[] { Object.class, Object.class };
}
public Method methodOnClass(Class aClass) throws NoSuchMethodException {
throw new NoSuchMethodException();
}
public Method methodOnObject(Object anObject) throws NoSuchMethodException {
throw new NoSuchMethodException();
}
public boolean implementedByClass(Class aClass) {
return true;
}
public boolean implementedByObject(Object anObject) {
return true;
}
public boolean isRelationalOperator() {
return true;
}
public Object invoke(Object anObject, Object[] parameters) throws IllegalAccessException,
IllegalArgumentException, InvocationTargetException, NoSuchMethodException {
return qualify(anObject, parameters[0]);
}
abstract protected Boolean qualify(Object target, Object parameter);
protected int doCompare(Object o1, Object o2) {
Class firstClass = o1.getClass();
Class secondClass = o2.getClass();
if (!(secondClass.equals(firstClass))) {
Object converted = null;
if (o2 instanceof Comparable) {
converted = ValueConverter.convertObjectToClass(o1, secondClass);
if (converted != null) {
o1 = converted;
}
}
if (converted == null && (o1 instanceof Comparable)) {
converted = ValueConverter.convertObjectToClass(o2, firstClass);
if (converted != null) {
o2 = converted;
}
}
if (converted == null) {
throw new WotonomyException("Qualifier: Not Comparable Objects");
// no way to compare
}
}
return ((Comparable) o2).compareTo(o1);
}
}
static class OperatorCaseInsensitiveLike extends BaseSelector {
public boolean isRelationalOperator() {
return false;
}
protected Boolean qualify(Object o1, Object o2) {
String myString1 = o1.toString();
String myString2 = o2.toString();
myString1 = myString1.toLowerCase();
myString2 = myString2.toLowerCase();
StringTokenizer st = new StringTokenizer(myString1, "%");
while (st.hasMoreTokens()) {
String part = st.nextToken();
int index = myString2.indexOf(part);
if (index > -1) {
myString2 = myString2.substring(index + part.length());
} else {
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
public String toString() {
return "caseInsensitiveLike";
}
}
static class OperatorContains extends BaseSelector {
public boolean isRelationalOperator() {
return false;
}
protected Boolean qualify(Object o1, Object o2) {
String myString1 = o1.toString();
String myString2 = o2.toString();
return new Boolean(myString2.indexOf(myString1) > -1);
}
public String toString() {
return "contains";
}
}
static class OperatorEqual extends BaseSelector {
protected Boolean qualify(Object o1, Object o2) {
return new Boolean(doCompare(o1, o2) == 0);
}
public String toString() {
return "=";
}
}
static class OperatorGreaterThan extends BaseSelector {
protected Boolean qualify(Object o1, Object o2) {
return new Boolean(doCompare(o1, o2) > 0);
}
public String toString() {
return ">";
}
}
static class OperatorGreaterThanOrEqualTo extends BaseSelector {
protected Boolean qualify(Object o1, Object o2) {
return new Boolean(doCompare(o1, o2) >= 0);
}
public String toString() {
return new String(" >= ");
}
}
static class OperatorLessThan extends BaseSelector {
protected Boolean qualify(Object o1, Object o2) {
return new Boolean(doCompare(o1, o2) < 0);
}
public String toString() {
return ">";
}
}
static class OperatorLessThanOrEqualTo extends BaseSelector {
protected Boolean qualify(Object o1, Object o2) {
return new Boolean(doCompare(o1, o2) <= 0);
}
public String toString() {
return "<=";
}
}
static class OperatorLike extends BaseSelector {
public boolean isRelationalOperator() {
return false;
}
protected Boolean qualify(Object o1, Object o2) {
String myString1 = o1.toString();
String myString2 = o2.toString();
StringTokenizer st = new StringTokenizer(myString1, "%");
while (st.hasMoreTokens()) {
String part = st.nextToken();
int index = myString2.indexOf(part);
if (index > -1) {
myString2 = myString2.substring(index + part.length());
} else {
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
public String toString() {
return "like";
}
}
static class OperatorNotEqual extends BaseSelector {
protected Boolean qualify(Object o1, Object o2) {
return new Boolean(!(o1.equals(o2)));
}
public String toString() {
return "!=";
}
}
public static Object decodeWithKeyValueUnarchiver(EOKeyValueUnarchiver ua) {
String cname = (String) ua.decodeObjectForKey("class");
if (cname.equals("EOKeyValueQualifier"))
return (EOQualifier) EOKeyValueQualifier.decodeWithKeyValueUnarchiver(ua);
if (cname.equals("EOAndQualifier"))
return (EOQualifier) EOAndQualifier.decodeWithKeyValueUnarchiver(ua);
if (cname.equals("EOOrQualifier"))
return (EOQualifier) EOOrQualifier.decodeWithKeyValueUnarchiver(ua);
if (cname.equals("EONotQualifier"))
return (EOQualifier) EONotQualifier.decodeWithKeyValueUnarchiver(ua);
return null;
}
}
/*
* $Log$ Revision 1.2 2006/02/16 16:47:14 cgruber Move some classes in to
* "internal" packages and re-work imports, etc.
*
* Also use UnsupportedOperationExceptions where appropriate, instead of
* WotonomyExceptions.
*
* Revision 1.1 2006/02/16 13:19:57 cgruber Check in all sources in
* eclipse-friendly maven-enabled packages.
*
* Revision 1.10 2003/08/09 01:22:51 chochos qualifiers implement
* EOKeyValueArchiving
*
* Revision 1.9 2001/11/04 18:29:11 mpowers Better handling for non-string types
* used with non-relational operators.
*
* Revision 1.8 2001/10/31 15:25:14 mpowers Cleanup of qualifiers.
*
* Revision 1.7 2001/10/30 22:57:28 mpowers EOQualifier framework is now
* working.
*
* Revision 1.6 2001/10/30 22:16:37 mpowers Implemented operators as selectors.
*
* Revision 1.5 2001/09/14 14:21:28 mpowers Updated javadoc.
*
* Revision 1.3 2001/09/13 15:25:56 mpowers Started implementation of the
* EOQualifier framework.
*
* Revision 1.2 2001/02/27 03:33:04 mpowers Initial draft of the key-value
* qualifier.
*
* Revision 1.1.1.1 2000/12/21 15:46:47 mpowers Contributing wotonomy.
*
* Revision 1.2 2000/12/20 16:25:35 michael Added log to all files.
*
*
*/
|