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  * 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  * $Log$
138  * Revision 1.2  2006/02/16 16:47:14  cgruber
139  * Move some classes in to "internal" packages and re-work imports, etc.
140  *
141  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
142  *
143  * Revision 1.1  2006/02/16 13:19:57  cgruber
144  * Check in all sources in eclipse-friendly maven-enabled packages.
145  *
146  * Revision 1.6  2003/08/12 01:43:04  chochos
147  * formally implement EOQualifierEvaluation
148  *
149  * Revision 1.5  2003/08/09 01:22:51  chochos
150  * qualifiers implement EOKeyValueArchiving
151  *
152  * Revision 1.4  2003/08/06 23:07:52  chochos
153  * general code cleanup (mostly, removing unused imports)
154  *
155  * Revision 1.3  2001/10/31 15:25:14  mpowers
156  * Cleanup of qualifiers.
157  *
158  * Revision 1.2  2001/10/30 22:57:28  mpowers
159  * EOQualifier framework is now working.
160  *
161  * Revision 1.1  2001/09/13 15:25:56  mpowers
162  * Started implementation of the EOQualifier framework.
163  *
164  *
165  */
166 
167