KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > fn > property > CompareProperty


1 // ============================================================================
2
// $Id: CompareProperty.java,v 1.13 2006/08/05 21:31:37 davidahall Exp $
3
// Copyright (c) 2003-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.fn.property;
34
35 import net.sf.jga.fn.BinaryFunctor;
36 import net.sf.jga.fn.UnaryPredicate;
37 import net.sf.jga.fn.comparison.EqualTo;
38
39 /**
40  * Unary Predicate that compares the value of the named property to the
41  * given value. The type of comparison is a binary predicate: the two
42  * arguments passed to the predicate are the value of the argument's property
43  * in the first position and the constant value passed at construction in the
44  * second position.
45  * <p>
46  * The test returns
47  * <code>bp(getProperty(name).fn(arg), value)</code>. This is also
48  * equivalent to the following:<br>
49  * <pre>
50  * UnaryPredicate CompareProperty =
51  * new UnaryCompose(new Binder2nd(bp, value),
52  * new GetProperty(propName))
53  * </pre>
54  * with one less call to <code>fn</code> at evaluation and somewhat clearer
55  * construction syntax.
56  * <p>
57  * To Serialize a CompareProperty, the generic parameter V must be serializable.
58  * <p>
59  * Copyright &copy; 2003-2005 David A. Hall
60  *
61  * @author <a HREF="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
62  **/

63
64 public class CompareProperty<T,V> extends UnaryPredicate<T> {
65     
66     static final long serialVersionUID = 8734296101969960336L;
67
68     private BinaryFunctor<V,V,Boolean JavaDoc> _comp;
69     private GetProperty<T,V> _gpf;
70     private String JavaDoc _prop;
71     private V _value;
72
73     /**
74      * Builds the CompareProperty predicate that will compare the named
75      * property of an instance of type argType to the given value using
76      * an EqualTo predicate.
77      */

78     public CompareProperty(Class JavaDoc<T> argType, String JavaDoc propName, V val) {
79         this(argType, propName, new EqualTo<V>(), val);
80     }
81     
82     /**
83      * Builds the CompareProperty predicate that will compare the named
84      * property of an instance of type argType to the given value. The
85      * comparison can be any type of BinaryFunctor where both arguments
86      * are of the same type.
87      */

88     public CompareProperty(Class JavaDoc<T> argType,String JavaDoc propName,BinaryFunctor<V,V,Boolean JavaDoc> pred,V val){
89         _prop = propName;
90         _comp = pred;
91         _value = val;
92         _gpf = new GetProperty<T, V>(argType, propName);
93     }
94
95     // TODO:
96
// public CompareProperty(Class<T> argType, String propName, UnaryFunctor<V,Boolean> pred)
97

98     /**
99      * Returns the name of the property to be compared
100      * @return the name of the property to be compared
101      */

102     public String JavaDoc getPropName() { return _prop; }
103     
104     /**
105      * Returns the constant value to which properties are compared
106      * @return the constant value to which properties are compared
107      */

108     public V getValue() { return _value; }
109     
110     /**
111      * Returns the predicate used to compare property values
112      * @return the predicate used to compare property values.
113      */

114     public BinaryFunctor<V,V,Boolean JavaDoc> getPredicate() {
115         return _comp;
116     }
117     
118     // UnaryPredicate interface
119

120     /**
121      * Tests the designated property of the argument against the value given at
122      * construction.
123      * <p>
124      * @return the boolean value of the comparison
125      */

126     public Boolean JavaDoc fn(T arg) {
127         return _comp.fn(_gpf.fn(arg), _value);
128     }
129
130     /**
131      * Calls the Visitor's <code>visit(CompareProperty)</code> method, if it
132      * implements the nested Visitor interface.
133      */

134     public void accept(net.sf.jga.fn.Visitor v) {
135         if (v instanceof CompareProperty.Visitor)
136             ((CompareProperty.Visitor)v).visit(this);
137         else
138             v.visit(this);
139     }
140
141     // Object overrides
142

143     public String JavaDoc toString() {
144         return "CompareProperty["+_prop+" "+_comp+" "+_value+"]";
145     }
146     
147     // Acyclic Visitor
148

149     /**
150      * Interface for classes that may interpret a <b>CompareProperty</b>
151      * predicate.
152      */

153     public interface Visitor extends net.sf.jga.fn.Visitor {
154         public void visit(CompareProperty host);
155     }
156 }
157     
158
159
160
Popular Tags