001: // ============================================================================
002: // $Id: ComparatorFn.java,v 1.12 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 net.sf.jga.fn.BinaryFunctor;
036: import java.io.Serializable;
037: import java.util.Comparator;
038:
039: /**
040: * Functor wrapper around Comparator object. Allows Comparators to be used
041: * anywhere a Functor returning an Integer could have been used. Also
042: * implements Comparator as well, so an instance of this class could be used
043: * anywhere that the constructor argument could be used.
044: * <p>
045: * To Serialize a ComparatorFn, the Comparator given at construction must be
046: * Serializable.
047: * <p>
048: * Copyright © 2002-2005 David A. Hall
049: *
050: * @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
051: **/
052:
053: public class ComparatorFn<T> extends BinaryFunctor<T, T, Integer>
054: implements Comparator<T> {
055:
056: static final long serialVersionUID = -7851342943467256913L;
057:
058: private Comparator<T> _comp;
059:
060: /**
061: * Builds the ComparatorFn wrapped around the given Comparator.
062: *
063: * @throws NullPointerException if no Comparator is passed.
064: */
065: public ComparatorFn(Comparator<T> comp) {
066: if (comp == null) {
067: throw new IllegalArgumentException(
068: "Comparator may not be null");
069: }
070:
071: _comp = comp;
072: }
073:
074: /**
075: * Returns the comparator in use by this functor
076: */
077: public Comparator<T> getComparator() {
078: return _comp;
079: }
080:
081: // BinaryFunctor interface
082:
083: /**
084: * Given arguments <b>x</b> and <b>y</b>, return the result of the
085: * Comparator's <code>compare(x,y)</code> method, wrapped in an Integer.
086: * Whether or not a NullPointerException is thrown if either x or y are
087: * null is up to the Comparator
088: *
089: * @return the result of the Comparator's <code>compare(x,y)</code> method
090: */
091: public Integer fn(T x, T y) {
092: return new Integer(compare(x, y));
093: }
094:
095: /**
096: * Calls the Visitor's <code>visit(ComperatorFn)</code> method, if it
097: * implements the nested Visitor interface.
098: */
099: public void accept(net.sf.jga.fn.Visitor v) {
100: if (v instanceof ComparatorFn.Visitor)
101: ((ComparatorFn.Visitor) v).visit(this );
102: else
103: v.visit(this );
104: }
105:
106: // Comparator interface
107:
108: public int compare(T x, T y) {
109: return _comp.compare(x, y);
110: }
111:
112: // Object overrides
113:
114: public String toString() {
115: return "ComperatorFn";
116: }
117:
118: // AcyclicVisitor
119:
120: /**
121: * Interface for classes that may interpret a <b>ComparatorFn</b> functor.
122: */
123: public interface Visitor extends net.sf.jga.fn.Visitor {
124: public void visit(ComparatorFn host);
125: }
126: }
|