View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2001 Michael Powers
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 net.wotonomy.foundation.NSKeyValueCoding;
22  import net.wotonomy.foundation.NSMutableDictionary;
23  import net.wotonomy.foundation.NSSelector;
24  import net.wotonomy.foundation.internal.WotonomyException;
25  
26  /***
27  * EOKeyValueQualifier performs a property-based
28  * comparison against a specified value.  The comparison 
29  * is specified in the form of a NSSelector.  The
30  * selector is expected to take two arguments, the
31  * property value on an object and the comparison value, 
32  * and return a Boolean indicating whether the object
33  * is qualified.  EOQualifier defines selectors that 
34  * may be used in creating EOKeyValueQualifiers.
35  *
36  * @author michael@mpowers.net
37  * @author yjcheung@intersectsoft.com
38  * @author $Author: cgruber $
39  * @version $Revision: 894 $
40  */
41  public class EOKeyValueQualifier extends EOQualifier
42  	implements EOKeyValueArchiving, EOQualifierEvaluation
43  {
44      private String key;
45      private NSSelector selector;
46      private Object value;
47  
48      /***
49      * Constructor specifying a property key, a selector,
50      * and a value for comparison.  The selector may be
51      * one of the constant selectors defined on EOQualifier.
52      */
53      public EOKeyValueQualifier(
54          String aKey,
55          NSSelector aSelector,
56          Object aValue )
57      {
58          key = aKey;
59          selector = aSelector;
60          value = aValue;
61      }
62  
63      /***
64      * Returns the key for this qualifier.
65      */
66      public String key()
67      {
68          return key;
69      }
70  
71      /***
72      * Returns the selector for this qualifier.
73      */
74      public NSSelector selector()
75      {
76          return selector;
77      }
78  
79      /***
80      * Returns the value for this qualifier.
81      */
82      public Object value()
83      {
84          return value;
85      }
86  
87      /***
88      * Evaluates this qualifier for the specified object,
89      * and returns whether the object is qualified.
90      * selector() is invoked on the value for key() on the
91      * specified object, with value() as the parameter.
92      */
93      public boolean evaluateWithObject( Object anObject )
94      {
95          try
96          {
97              Object value;
98              if ( anObject instanceof EOKeyValueCoding )
99              {
100                 value = ((EOKeyValueCoding)anObject).valueForKey( key() );
101             }
102             else
103             {
104                 value = EOKeyValueCodingSupport.valueForKey( anObject, key() );
105             }
106             return ((Boolean)selector.invoke( value(), value)).booleanValue();
107         }
108         catch ( Exception exc )
109         {
110             throw new WotonomyException( exc );
111         }
112     }
113 
114     /***
115     * Returns a string representation of this qualifier.
116     */
117     public String toString()
118     {
119         return "( " + key + " " + selector + " " + value + " )";
120     }
121 
122 	public void encodeWithKeyValueArchiver(EOKeyValueArchiver arch) {
123 		arch.encodeObject("EOKeyValueQualifier", "class");
124 		arch.encodeObject(key, "key");
125 		NSMutableDictionary d = new NSMutableDictionary(2);
126 		if (value instanceof String)
127 			d.setObjectForKey("NSString", "class");
128 		else if (value instanceof java.math.BigDecimal)
129 			d.setObjectForKey("NSDecimalNumber", "class");
130 		else if (value instanceof Number)
131 			d.setObjectForKey("NSNumber", "class");
132 		else if (value instanceof NSKeyValueCoding.Null)
133 			d.setObjectForKey("EONull", "class");
134 		if (value != null && !(value instanceof NSKeyValueCoding.Null))
135 			d.setObjectForKey(value.toString(), "value");
136 		arch.encodeObject(d, "value");
137 		String selname = null;
138 		if (selector.equals(EOQualifier.QualifierOperatorCaseInsensitiveLike))
139 			selname = "caseInsensitiveLike:";
140 		else if (selector.equals(EOQualifier.QualifierOperatorContains))
141 			selname = "contains:";
142 		else if (selector.equals(EOQualifier.QualifierOperatorEqual))
143 			selname = "isEqualTo:";
144 		else if (selector.equals(EOQualifier.QualifierOperatorGreaterThan))
145 			selname = "greaterThan:";
146 		else if (selector.equals(EOQualifier.QualifierOperatorGreaterThanOrEqualTo))
147 			selname = "greaterThanOrEqualTo:";
148 		else if (selector.equals(EOQualifier.QualifierOperatorLessThan))
149 			selname = "lessThan:";
150 		else if (selector.equals(EOQualifier.QualifierOperatorLessThanOrEqualTo))
151 			selname = "lessThanOrEqualTo:";
152 		else if (selector.equals(EOQualifier.QualifierOperatorLike))
153 			selname = "like:";
154 		else if (selector.equals(EOQualifier.QualifierOperatorNotEqual))
155 			selname = "isNotEqualTo:";
156 		else
157 			selname = selector.name() + ":";
158 		arch.encodeObject(selname, "selectorName");
159 	}
160 
161 	public static Object decodeWithKeyValueUnarchiver(EOKeyValueUnarchiver arch) {
162 		String k = (String)arch.decodeObjectForKey("key");
163 		Object v = arch.decodeObjectForKey("value");
164 		NSSelector sel = null;
165 		String sname = (String)arch.decodeObjectForKey("selectorName");
166 		if (sname.equals("isEqualTo:"))
167 			sel = EOQualifier.QualifierOperatorEqual;
168 		else if (sname.equals("isNotEqualTo:"))
169 			sel = EOQualifier.QualifierOperatorNotEqual;
170 		else if (sname.equals("caseInsensitiveLike:"))
171 			sel = EOQualifier.QualifierOperatorCaseInsensitiveLike;
172 		else if (sname.equals("contains:"))
173 			sel = EOQualifier.QualifierOperatorContains;
174 		else if (sname.equals("greaterThan:"))
175 			sel = EOQualifier.QualifierOperatorGreaterThan;
176 		else if (sname.equals("greaterThanOrEqualTo:"))
177 			sel = EOQualifier.QualifierOperatorGreaterThanOrEqualTo;
178 		else if (sname.equals("lessThan:"))
179 			sel = EOQualifier.QualifierOperatorLessThan;
180 		else if (sname.equals("lessThanOrEqualTo:"))
181 			sel = EOQualifier.QualifierOperatorLessThanOrEqualTo;
182 		else if (sname.equals("like:"))
183 			sel = EOQualifier.QualifierOperatorLike;
184 		EOKeyValueQualifier q = new EOKeyValueQualifier(k, sel, v);
185 		return q;
186 	}
187 
188 }
189 
190 /*
191  * $Log$
192  * Revision 1.2  2006/02/16 16:47:14  cgruber
193  * Move some classes in to "internal" packages and re-work imports, etc.
194  *
195  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
196  *
197  * Revision 1.1  2006/02/16 13:19:57  cgruber
198  * Check in all sources in eclipse-friendly maven-enabled packages.
199  *
200  * Revision 1.10  2003/08/12 01:43:04  chochos
201  * formally implement EOQualifierEvaluation
202  *
203  * Revision 1.9  2003/08/11 19:39:30  chochos
204  * special conditions for NSKeyValueCoding.NullValue -> EONull
205  *
206  * Revision 1.8  2003/08/09 01:22:51  chochos
207  * qualifiers implement EOKeyValueArchiving
208  *
209  * Revision 1.7  2003/08/06 23:07:52  chochos
210  * general code cleanup (mostly, removing unused imports)
211  *
212  * Revision 1.6  2001/10/31 15:26:06  mpowers
213  * Fixed typo.
214  *
215  * Revision 1.5  2001/10/31 15:25:14  mpowers
216  * Cleanup of qualifiers.
217  *
218  * Revision 1.4  2001/10/30 22:57:28  mpowers
219  * EOQualifier framework is now working.
220  *
221  * Revision 1.3  2001/09/13 15:25:56  mpowers
222  * Started implementation of the EOQualifier framework.
223  *
224  * Revision 1.2  2001/03/29 03:29:49  mpowers
225  * Now using KeyValueCoding and Support instead of Introspector.
226  *
227  * Revision 1.1  2001/02/27 03:33:04  mpowers
228  * Initial draft of the key-value qualifier.
229  *
230  *
231  */
232 
233