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  * EOOrQualifier contains other EOQualifiers,
32  * evaluating as true if any 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 EOOrQualifier extends EOQualifier
41  	implements EOKeyValueArchiving, EOQualifierEvaluation
42  {
43      private List qualifiers;
44  
45      public EOOrQualifier(
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 a lazy "or" implementation.  Ex. Qal1 or Qal2.
74      * If Qal1 is evaluated to be true, then it returns true without
75      * further evaluate Qal2.
76      */
77      public boolean evaluateWithObject( Object anObject )
78      {
79          Iterator it = qualifiers.iterator();
80          while( it.hasNext() )
81          {
82              if ( ((EOQualifier) it.next()).evaluateWithObject(anObject) )
83              {
84                  return true;
85              }
86          }
87          return false;
88      }
89  
90      /***
91      * Returns a string representation of this qualifier.
92      */
93      public String toString()
94      {
95           StringBuffer myBuf = new StringBuffer("(");
96           Iterator it = qualifiers.iterator();
97           while (it.hasNext())
98           {
99              myBuf = myBuf.append(((EOQualifier) it.next()).toString()).append(" or ");
100          }
101          String myStr = myBuf.toString();
102          myStr = myStr.substring(0, myStr.lastIndexOf(" or ")).concat(")");
103          return myStr;
104     }
105 
106 	public static Object decodeWithKeyValueUnarchiver(EOKeyValueUnarchiver arch) {
107 		NSArray a = (NSArray)arch.decodeObjectForKey("qualifiers");
108 		if (a == null)
109 			return null;
110 		NSMutableArray l = new NSMutableArray();
111 		for (int i = 0; i < a.count(); i++) {
112 			NSDictionary d = (NSDictionary)a.objectAtIndex(i);
113 			EOKeyValueUnarchiver ua = new EOKeyValueUnarchiver(d);
114 			EOQualifier q = (EOQualifier)EOQualifier.decodeWithKeyValueUnarchiver(ua);
115 			if (q != null)
116 				l.addObject(q);
117 		}
118 		return new EOAndQualifier(l);
119 	}
120 
121 	public void encodeWithKeyValueArchiver(EOKeyValueArchiver arch) {
122 		arch.encodeObject("EOOrQualifier", "class");
123 		NSMutableArray arr = new NSMutableArray(qualifiers.size());
124 		for  (int i = 0; i < qualifiers.size(); i++) {
125 			EOQualifier q = (EOQualifier)qualifiers.get(i);
126 			if (q instanceof EOKeyValueArchiving) {
127 				EOKeyValueArchiver ar2 = new EOKeyValueArchiver();
128 				((EOKeyValueArchiving)q).encodeWithKeyValueArchiver(ar2);
129 				arr.addObject(ar2.dictionary());
130 			} else
131 				throw new WotonomyException("Cannot archive instance of " + q.getClass().getName());
132 		}
133 		arch.encodeObject(arr, "qualifiers");
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