1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.control;
20
21 import java.util.Iterator;
22 import java.util.LinkedList;
23 import java.util.List;
24
25 import net.wotonomy.foundation.NSArray;
26 import net.wotonomy.foundation.NSDictionary;
27 import net.wotonomy.foundation.NSMutableArray;
28 import net.wotonomy.foundation.internal.WotonomyException;
29
30 /***
31 * EOAndQualifier contains other EOQualifiers,
32 * evaluating as true only if all of the contained
33 * qualifiers evaluate as true.
34 *
35 * @author michael@mpowers.net
36 * @author yjcheung@intersectsoft.com
37 * @author $Author: cgruber $
38 * @version $Revision: 894 $
39 */
40 public class EOAndQualifier extends EOQualifier
41 implements EOKeyValueArchiving, EOQualifierEvaluation
42 {
43 private List qualifiers;
44
45 public EOAndQualifier(
46 List aQualifierList )
47 {
48 qualifiers = new LinkedList( aQualifierList );
49 }
50
51 /***
52 * Returns a List of qualifiers contained by this qualifier.
53 */
54 public NSArray qualifiers()
55 {
56 return new NSArray( qualifiers );
57 }
58
59 /***
60 * Add a new qualifier to the list.
61 */
62 public void addQualifier(EOQualifier qualifier)
63 {
64 qualifiers.add(qualifier);
65 }
66
67 /***
68 * Evaluates this qualifier for the specified object,
69 * and returns whether the object is qualified.
70 * selector() is invoked on the value for key() on the
71 * specified object, with value() as the parameter.
72 *
73 * Note: this has the lazy "and" implementation. Ex.
74 * Qal1 and Qal2. If the Qal1 is evaluated to be false, then it returns
75 * false without evaluating Qa12.
76 */
77 public boolean evaluateWithObject( Object anObject )
78 {
79 boolean retVal = true;
80 Iterator it = qualifiers.iterator();
81 while (it.hasNext() && retVal)
82 {
83 retVal = ((EOQualifier) it.next()).evaluateWithObject(anObject);
84 }
85 return retVal;
86 }
87
88 /***
89 * Returns a string representation of this qualifier.
90 */
91 public String toString()
92 {
93 StringBuffer myBuf = new StringBuffer("(");
94 Iterator it = qualifiers.iterator();
95 while (it.hasNext())
96 {
97 myBuf = myBuf.append(((EOQualifier) it.next()).toString()).append(" and ");
98 }
99 String myStr = myBuf.toString();
100 myStr = myStr.substring(0, myStr.lastIndexOf(" and")).concat(")");
101 return myStr;
102 }
103
104 public static Object decodeWithKeyValueUnarchiver(EOKeyValueUnarchiver arch) {
105 NSArray a = (NSArray)arch.decodeObjectForKey("qualifiers");
106 if (a == null)
107 return null;
108 NSMutableArray l = new NSMutableArray();
109 for (int i = 0; i < a.count(); i++) {
110 NSDictionary d = (NSDictionary)a.objectAtIndex(i);
111 EOKeyValueUnarchiver ua = new EOKeyValueUnarchiver(d);
112 EOQualifier q = (EOQualifier)EOQualifier.decodeWithKeyValueUnarchiver(ua);
113 if (q != null)
114 l.addObject(q);
115 }
116 return new EOAndQualifier(l);
117 }
118
119 public void encodeWithKeyValueArchiver(EOKeyValueArchiver arch) {
120 arch.encodeObject("EOAndQualifier", "class");
121 NSMutableArray arr = new NSMutableArray(qualifiers.size());
122 for (int i = 0; i < qualifiers.size(); i++) {
123 EOQualifier q = (EOQualifier)qualifiers.get(i);
124 if (q instanceof EOKeyValueArchiving) {
125 EOKeyValueArchiver ar2 = new EOKeyValueArchiver();
126 ((EOKeyValueArchiving)q).encodeWithKeyValueArchiver(ar2);
127 arr.addObject(ar2.dictionary());
128 } else
129 throw new WotonomyException("Cannot archive instance of " + q.getClass().getName());
130 }
131 arch.encodeObject(arr, "qualifiers");
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