View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2001 Intersect Software Corporation
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
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  * $Log$
140  * Revision 1.2  2006/02/16 16:47:14  cgruber
141  * Move some classes in to "internal" packages and re-work imports, etc.
142  *
143  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
144  *
145  * Revision 1.1  2006/02/16 13:19:57  cgruber
146  * Check in all sources in eclipse-friendly maven-enabled packages.
147  *
148  * Revision 1.6  2003/08/12 01:43:04  chochos
149  * formally implement EOQualifierEvaluation
150  *
151  * Revision 1.5  2003/08/09 01:22:51  chochos
152  * qualifiers implement EOKeyValueArchiving
153  *
154  * Revision 1.4  2003/08/06 23:07:52  chochos
155  * general code cleanup (mostly, removing unused imports)
156  *
157  * Revision 1.3  2001/10/31 15:25:14  mpowers
158  * Cleanup of qualifiers.
159  *
160  * Revision 1.2  2001/10/30 22:57:28  mpowers
161  * EOQualifier framework is now working.
162  *
163  * Revision 1.1  2001/09/13 15:25:56  mpowers
164  * Started implementation of the EOQualifier framework.
165  *
166  *
167  */
168 
169