001: // ============================================================================
002: // $Id: CompareProperty.java,v 1.13 2006/08/05 21:31:37 davidahall Exp $
003: // Copyright (c) 2003-2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032:
033: package net.sf.jga.fn.property;
034:
035: import net.sf.jga.fn.BinaryFunctor;
036: import net.sf.jga.fn.UnaryPredicate;
037: import net.sf.jga.fn.comparison.EqualTo;
038:
039: /**
040: * Unary Predicate that compares the value of the named property to the
041: * given value. The type of comparison is a binary predicate: the two
042: * arguments passed to the predicate are the value of the argument's property
043: * in the first position and the constant value passed at construction in the
044: * second position.
045: * <p>
046: * The test returns
047: * <code>bp(getProperty(name).fn(arg), value)</code>. This is also
048: * equivalent to the following:<br>
049: * <pre>
050: * UnaryPredicate CompareProperty =
051: * new UnaryCompose(new Binder2nd(bp, value),
052: * new GetProperty(propName))
053: * </pre>
054: * with one less call to <code>fn</code> at evaluation and somewhat clearer
055: * construction syntax.
056: * <p>
057: * To Serialize a CompareProperty, the generic parameter V must be serializable.
058: * <p>
059: * Copyright © 2003-2005 David A. Hall
060: *
061: * @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
062: **/
063:
064: public class CompareProperty<T, V> extends UnaryPredicate<T> {
065:
066: static final long serialVersionUID = 8734296101969960336L;
067:
068: private BinaryFunctor<V, V, Boolean> _comp;
069: private GetProperty<T, V> _gpf;
070: private String _prop;
071: private V _value;
072:
073: /**
074: * Builds the CompareProperty predicate that will compare the named
075: * property of an instance of type argType to the given value using
076: * an EqualTo predicate.
077: */
078: public CompareProperty(Class<T> argType, String propName, V val) {
079: this (argType, propName, new EqualTo<V>(), val);
080: }
081:
082: /**
083: * Builds the CompareProperty predicate that will compare the named
084: * property of an instance of type argType to the given value. The
085: * comparison can be any type of BinaryFunctor where both arguments
086: * are of the same type.
087: */
088: public CompareProperty(Class<T> argType, String propName,
089: BinaryFunctor<V, V, Boolean> pred, V val) {
090: _prop = propName;
091: _comp = pred;
092: _value = val;
093: _gpf = new GetProperty<T, V>(argType, propName);
094: }
095:
096: // TODO:
097: // public CompareProperty(Class<T> argType, String propName, UnaryFunctor<V,Boolean> pred)
098:
099: /**
100: * Returns the name of the property to be compared
101: * @return the name of the property to be compared
102: */
103: public String getPropName() {
104: return _prop;
105: }
106:
107: /**
108: * Returns the constant value to which properties are compared
109: * @return the constant value to which properties are compared
110: */
111: public V getValue() {
112: return _value;
113: }
114:
115: /**
116: * Returns the predicate used to compare property values
117: * @return the predicate used to compare property values.
118: */
119: public BinaryFunctor<V, V, Boolean> getPredicate() {
120: return _comp;
121: }
122:
123: // UnaryPredicate interface
124:
125: /**
126: * Tests the designated property of the argument against the value given at
127: * construction.
128: * <p>
129: * @return the boolean value of the comparison
130: */
131: public Boolean fn(T arg) {
132: return _comp.fn(_gpf.fn(arg), _value);
133: }
134:
135: /**
136: * Calls the Visitor's <code>visit(CompareProperty)</code> method, if it
137: * implements the nested Visitor interface.
138: */
139: public void accept(net.sf.jga.fn.Visitor v) {
140: if (v instanceof CompareProperty.Visitor)
141: ((CompareProperty.Visitor) v).visit(this );
142: else
143: v.visit(this );
144: }
145:
146: // Object overrides
147:
148: public String toString() {
149: return "CompareProperty[" + _prop + " " + _comp + " " + _value
150: + "]";
151: }
152:
153: // Acyclic Visitor
154:
155: /**
156: * Interface for classes that may interpret a <b>CompareProperty</b>
157: * predicate.
158: */
159: public interface Visitor extends net.sf.jga.fn.Visitor {
160: public void visit(CompareProperty host);
161: }
162: }
|