// ============================================================================
// $Id: GreaterEqual.java,v 1.16 2006/01/08 00:52:25 davidahall Exp $
// Copyright (c) 2002-2005 David A. Hall
// ============================================================================
// The contents of this file are subject to the Common Development and
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
// file except in compliance with the License. You should have received a copy
// of the the License along with this file: if not, a copy of the License is
// available from Sun Microsystems, Inc.
//
// http://www.sun.com/cddl/cddl.html
//
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
// publish revised and/or new versions of the License. You may not use,
// distribute, or otherwise make this file available under subsequent versions
// of the License.
//
// Alternatively, the contents of this file may be used under the terms of the
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
// case the provisions of the LGPL are applicable instead of those above. If you
// wish to allow use of your version of this file only under the terms of the
// LGPL, and not to allow others to use your version of this file under the
// terms of the CDDL, indicate your decision by deleting the provisions above
// and replace them with the notice and other provisions required by the LGPL.
// If you do not delete the provisions above, a recipient may use your version
// of this file under the terms of either the CDDL or the LGPL.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ============================================================================
package net.sf.jga.fn.comparison;
import java.util.Comparator;
import net.sf.jga.fn.BinaryPredicate;
import net.sf.jga.util.ComparableComparator;
/**
* Binary Predicate that returns TRUE for arguments <b>x</b> and <b>y</b> when
* x >= y. The comparison is performed using a comparator supplied at
* construction time, although a default comparator will be used if the nested
* Comparable class' default constructor is used.
* The behaviour of this class in the presence of null arguments is left to the
* implementation of the specific Comparator, however it is generally safe to
* assume that using null arguments will cause a NullPointerException to be
* thrown.
* <p>
* To serialize a GreaterEqual, the comparator passed at construction must be
* Serializable.
* <p>
* Copyright © 2002-2005 David A. Hall
*
* @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
**/
public class GreaterEqual<T> extends BinaryPredicate<T,T>{
static final long serialVersionUID = 8834315867722931262L;
// The Comparator used to compare elements.
private Comparator/*@*/<? super T>/*@*/ _comp;
/**
* Builds a GreaterEqual predicate using the given Comparator
* @throws IllegalArgumentException if the argument is null
*/
public GreaterEqual(Comparator/*@*/<? super T>/*@*/ comp) {
if (comp == null) {
throw new IllegalArgumentException("Comparator may not be null");
}
_comp = comp;
}
/**
* Returns the comparator in use by this functor
* @return the comparator in use by this functor
*/
public Comparator<? super T> getComparator() { return _comp; }
// BinaryPredicate interface
/**
* Given Comparable arguments <b>x</b> and <b>y</b>, returns x >= y.
*
* @throws NullPointerException if either argument is null
* @return x >= y
*/
public Boolean fn(T x, T y) {
return Boolean.valueOf(_comp.compare(x, y) >= 0);
}
/**
* Calls the Visitor's <code>visit(GreaterEqual)</code> method, if it
* implements the nested Visitor interface.
*/
public void accept(net.sf.jga.fn.Visitor v) {
if (v instanceof GreaterEqual.Visitor)
((GreaterEqual.Visitor)v).visit(this);
else
v.visit(this);
}
// Object overrides
public String toString() {
return "GreaterEqual";
}
// Acyclic Visitor
/**
* Interface for classes that may interpret a <b>GreaterEqual</b> predicate.
*/
public interface Visitor extends net.sf.jga.fn.Visitor {
public void visit(GreaterEqual host);
}
/**
* GreaterEqual predicate for use with Comparable arguments. This class exists
* as an implementation detail that works around a limit in the javac
* inferencer -- in all substantive ways, this is simply a GreaterEqual functor.
*/
static public class Comparable<T extends java.lang.Comparable/*@*/<? super T>/*@*/>
extends GreaterEqual<T>
{
static final long serialVersionUID = 62633572749973541L;
public Comparable() { super(new ComparableComparator<T>()); }
}
}
|