ComparatorFn.java :  » Development » jga-Generic-Algorithms » net » sf » jga » fn » comparison » Java Open Source

Java Open Source » Development » jga Generic Algorithms 
jga Generic Algorithms » net » sf » jga » fn » comparison » ComparatorFn.java
// ============================================================================
// $Id: ComparatorFn.java,v 1.12 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 net.sf.jga.fn.BinaryFunctor;
import java.io.Serializable;
import java.util.Comparator;

/**
 * Functor wrapper around Comparator object.  Allows Comparators to be used
 * anywhere a Functor returning an Integer could have been used.  Also
 * implements Comparator as well, so an instance of this class could be used
 * anywhere that the constructor argument could be used.
 * <p>
 * To Serialize a ComparatorFn, the Comparator given at construction must be
 * Serializable.
 * <p>
 * Copyright &copy; 2002-2005  David A. Hall
 *
 * @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
 **/

public class ComparatorFn<T> extends BinaryFunctor<T,T,Integer> implements Comparator<T> {

    static final long serialVersionUID = -7851342943467256913L;

    private Comparator<T> _comp;

    /**
     * Builds the ComparatorFn wrapped around the given Comparator.
     *
     * @throws NullPointerException if no Comparator is passed.
     */
    public ComparatorFn(Comparator<T> comp) {
        if (comp == null) {
            throw new IllegalArgumentException("Comparator may not be null");
        }
        
        _comp = comp;
    }

    /**
     * Returns the comparator in use by this functor
     */
    public Comparator<T> getComparator() { return _comp; }

    // BinaryFunctor interface
    
    /**
     * Given arguments <b>x</b> and <b>y</b>, return the result of the
     * Comparator's <code>compare(x,y)</code> method, wrapped in an Integer.
     * Whether or not a NullPointerException is thrown if either x or y are
     * null is up to the Comparator
     * 
     * @return the result of the Comparator's <code>compare(x,y)</code> method
     */
    public Integer fn(T x, T y) {
        return new Integer(compare(x, y));
    }
    
    /**
     * Calls the Visitor's <code>visit(ComperatorFn)</code> method, if it
     * implements the nested Visitor interface.
     */
    public void accept(net.sf.jga.fn.Visitor v) {
        if (v instanceof ComparatorFn.Visitor)
            ((ComparatorFn.Visitor)v).visit(this); 
        else
            v.visit(this);
    }

    // Comparator interface

    public int compare(T x, T y) {
        return _comp.compare(x, y);
    }

    // Object overrides

    public String toString() {
        return "ComperatorFn";
    }
    
    // AcyclicVisitor
    
    /**
     * Interface for classes that may interpret a <b>ComparatorFn</b> functor.
     */
    public interface Visitor extends net.sf.jga.fn.Visitor {
        public void visit(ComparatorFn host);
    }
}


                                      
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.