001: // ============================================================================
002: // $Id: Between.java,v 1.13 2006/01/08 00:52:25 davidahall Exp $
003: // Copyright (c) 2002-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.comparison;
034:
035: import java.util.Comparator;
036: import net.sf.jga.fn.UnaryFunctor;
037: import net.sf.jga.fn.UnaryPredicate;
038: import net.sf.jga.util.ComparableComparator;
039:
040: /**
041: * Unary Predicate that returns TRUE when its argument is between two given
042: * values. By default, the range is inclusive: a constructor is provided that
043: * allows client code to supply two predicates that can create exclusive ranges.
044: * The comparison is performed using a comparator or a pair of functors supplied
045: * at construction time: a default comparator will be used if the nested
046: * Comparable class' default constructor is used.
047: * The behaviour of this class in the presence of null arguments is left to the
048: * implementation of the specific Comparator, however it is generally safe to
049: * assume that using null arguments will cause a NullPointerException to be
050: * thrown.
051: * <p>
052: * Copyright © 2003-2005 David A. Hall
053: *
054: * @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
055: **/
056:
057: public class Between<T> extends UnaryPredicate<T> {
058:
059: static final long serialVersionUID = 7520704443234013748L;
060:
061: private UnaryFunctor<T, Boolean> _ge;
062: private UnaryFunctor<T, Boolean> _le;
063:
064: /**
065: * Builds a Between predicate that returns TRUE when its argument is between
066: * its two arguments (inclusive). The given comparator will be used to
067: * compare values.
068: * @throws IllegalArgumentException when either argument is null or when
069: * lo > hi
070: */
071: public Between(T lo, T hi, Comparator<? super T> comp) {
072: if (lo == null || hi == null) {
073: String msg = "a pair of values is required";
074: throw new IllegalArgumentException(msg);
075: }
076:
077: if (comp.compare(lo, hi) > 0) {
078: String msg = "lo value must be less than hi value";
079: throw new IllegalArgumentException(msg);
080: }
081:
082: _ge = new GreaterEqual<T>(comp).bind2nd(lo);
083: _le = new LessEqual<T>(comp).bind2nd(hi);
084: }
085:
086: /**
087: * Builds a Between predicate that returns TRUE when both of the given
088: * predicates return TRUE for the same argument. This version of the
089: * constructor is provided to allow finer control over the comparisons
090: * performed.
091: */
092: public Between(UnaryFunctor<T, Boolean> lo,
093: UnaryFunctor<T, Boolean> hi) {
094: if (lo == null || hi == null) {
095: String msg = "a pair of predicates is required";
096: throw new IllegalArgumentException(msg);
097: }
098:
099: _ge = lo;
100: _le = hi;
101: }
102:
103: // UnaryPredicate interface
104:
105: /**
106: * Given argument <b>x</b>, returns TRUE if x is between lo and hi.
107: *
108: * @return lo < x < hi
109: */
110: public Boolean fn(T x) {
111: return Boolean.valueOf(_ge.fn(x).booleanValue()
112: && _le.fn(x).booleanValue());
113: }
114:
115: /**
116: * Calls the Visitor's <code>visit(Between)</code> method, if it
117: * implements the nested Visitor interface.
118: */
119: public void accept(net.sf.jga.fn.Visitor v) {
120: if (v instanceof Between.Visitor)
121: ((Between.Visitor) v).visit(this );
122: else
123: v.visit(this );
124: }
125:
126: // Object overrides
127:
128: public String toString() {
129: return "Between";
130: }
131:
132: // Acyclic Visitor
133:
134: /**
135: * Interface for classes that may interpret an <b>Between</b> predicate.
136: */
137: public interface Visitor extends net.sf.jga.fn.Visitor {
138: public void visit(Between host);
139: }
140:
141: /**
142: * Between functor for use with Comparable arguments. This class exists
143: * as an implementation detail that works around a limit in the javac
144: * inferencer -- in all substantive ways, this is simply a Between functor.
145: */
146:
147: static public class Comparable<T extends java.lang.Comparable/*@*/<? super T>/*@*/>
148: extends Between<T> {
149: static final long serialVersionUID = 4385771596777515479L;
150:
151: public Comparable(T lo, T hi) {
152: super (lo, hi, new ComparableComparator<T>());
153: }
154: }
155: }
|