Coverage Report - net.wotonomy.control.EOKeyValueQualifier
 
Classes in this File Line Coverage Branch Coverage Complexity
EOKeyValueQualifier
0% 
0% 
4.375
 
 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  0
     public EOKeyValueQualifier(
 54  
         String aKey,
 55  
         NSSelector aSelector,
 56  
         Object aValue )
 57  0
     {
 58  0
         key = aKey;
 59  0
         selector = aSelector;
 60  0
         value = aValue;
 61  0
     }
 62  
 
 63  
     /**
 64  
     * Returns the key for this qualifier.
 65  
     */
 66  
     public String key()
 67  
     {
 68  0
         return key;
 69  
     }
 70  
 
 71  
     /**
 72  
     * Returns the selector for this qualifier.
 73  
     */
 74  
     public NSSelector selector()
 75  
     {
 76  0
         return selector;
 77  
     }
 78  
 
 79  
     /**
 80  
     * Returns the value for this qualifier.
 81  
     */
 82  
     public Object value()
 83  
     {
 84  0
         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  0
             if ( anObject instanceof EOKeyValueCoding )
 99  
             {
 100  0
                 value = ((EOKeyValueCoding)anObject).valueForKey( key() );
 101  0
             }
 102  
             else
 103  
             {
 104  0
                 value = EOKeyValueCodingSupport.valueForKey( anObject, key() );
 105  
             }
 106  0
             return ((Boolean)selector.invoke( value(), value)).booleanValue();
 107  
         }
 108  0
         catch ( Exception exc )
 109  
         {
 110  0
             throw new WotonomyException( exc );
 111  
         }
 112  
     }
 113  
 
 114  
     /**
 115  
     * Returns a string representation of this qualifier.
 116  
     */
 117  
     public String toString()
 118  
     {
 119  0
         return "( " + key + " " + selector + " " + value + " )";
 120  
     }
 121  
 
 122  
         public void encodeWithKeyValueArchiver(EOKeyValueArchiver arch) {
 123  0
                 arch.encodeObject("EOKeyValueQualifier", "class");
 124  0
                 arch.encodeObject(key, "key");
 125  0
                 NSMutableDictionary d = new NSMutableDictionary(2);
 126  0
                 if (value instanceof String)
 127  0
                         d.setObjectForKey("NSString", "class");
 128  0
                 else if (value instanceof java.math.BigDecimal)
 129  0
                         d.setObjectForKey("NSDecimalNumber", "class");
 130  0
                 else if (value instanceof Number)
 131  0
                         d.setObjectForKey("NSNumber", "class");
 132  0
                 else if (value instanceof NSKeyValueCoding.Null)
 133  0
                         d.setObjectForKey("EONull", "class");
 134  0
                 if (value != null && !(value instanceof NSKeyValueCoding.Null))
 135  0
                         d.setObjectForKey(value.toString(), "value");
 136  0
                 arch.encodeObject(d, "value");
 137  0
                 String selname = null;
 138  0
                 if (selector.equals(EOQualifier.QualifierOperatorCaseInsensitiveLike))
 139  0
                         selname = "caseInsensitiveLike:";
 140  0
                 else if (selector.equals(EOQualifier.QualifierOperatorContains))
 141  0
                         selname = "contains:";
 142  0
                 else if (selector.equals(EOQualifier.QualifierOperatorEqual))
 143  0
                         selname = "isEqualTo:";
 144  0
                 else if (selector.equals(EOQualifier.QualifierOperatorGreaterThan))
 145  0
                         selname = "greaterThan:";
 146  0
                 else if (selector.equals(EOQualifier.QualifierOperatorGreaterThanOrEqualTo))
 147  0
                         selname = "greaterThanOrEqualTo:";
 148  0
                 else if (selector.equals(EOQualifier.QualifierOperatorLessThan))
 149  0
                         selname = "lessThan:";
 150  0
                 else if (selector.equals(EOQualifier.QualifierOperatorLessThanOrEqualTo))
 151  0
                         selname = "lessThanOrEqualTo:";
 152  0
                 else if (selector.equals(EOQualifier.QualifierOperatorLike))
 153  0
                         selname = "like:";
 154  0
                 else if (selector.equals(EOQualifier.QualifierOperatorNotEqual))
 155  0
                         selname = "isNotEqualTo:";
 156  
                 else
 157  0
                         selname = selector.name() + ":";
 158  0
                 arch.encodeObject(selname, "selectorName");
 159  0
         }
 160  
 
 161  
         public static Object decodeWithKeyValueUnarchiver(EOKeyValueUnarchiver arch) {
 162  0
                 String k = (String)arch.decodeObjectForKey("key");
 163  0
                 Object v = arch.decodeObjectForKey("value");
 164  0
                 NSSelector sel = null;
 165  0
                 String sname = (String)arch.decodeObjectForKey("selectorName");
 166  0
                 if (sname.equals("isEqualTo:"))
 167  0
                         sel = EOQualifier.QualifierOperatorEqual;
 168  0
                 else if (sname.equals("isNotEqualTo:"))
 169  0
                         sel = EOQualifier.QualifierOperatorNotEqual;
 170  0
                 else if (sname.equals("caseInsensitiveLike:"))
 171  0
                         sel = EOQualifier.QualifierOperatorCaseInsensitiveLike;
 172  0
                 else if (sname.equals("contains:"))
 173  0
                         sel = EOQualifier.QualifierOperatorContains;
 174  0
                 else if (sname.equals("greaterThan:"))
 175  0
                         sel = EOQualifier.QualifierOperatorGreaterThan;
 176  0
                 else if (sname.equals("greaterThanOrEqualTo:"))
 177  0
                         sel = EOQualifier.QualifierOperatorGreaterThanOrEqualTo;
 178  0
                 else if (sname.equals("lessThan:"))
 179  0
                         sel = EOQualifier.QualifierOperatorLessThan;
 180  0
                 else if (sname.equals("lessThanOrEqualTo:"))
 181  0
                         sel = EOQualifier.QualifierOperatorLessThanOrEqualTo;
 182  0
                 else if (sname.equals("like:"))
 183  0
                         sel = EOQualifier.QualifierOperatorLike;
 184  0
                 EOKeyValueQualifier q = new EOKeyValueQualifier(k, sel, v);
 185  0
                 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