001: // ============================================================================
002: // $Id: MaxValue.java,v 1.8 2006/01/08 00:52:25 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: package net.sf.jga.fn.algorithm;
033:
034: import java.util.Collection;
035: import java.util.Collections;
036: import java.util.Comparator;
037: import net.sf.jga.fn.BinaryFunctor;
038: import net.sf.jga.fn.UnaryFunctor;
039:
040: /**
041: * Identifies the largest value in a collection.
042: * <p>
043: * To serialize a MaxValue, the comparator passed at construction must be
044: * Serializable.
045: * <p>
046: * Copyright © 2003-2005 David A. Hall
047: *
048: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
049: */
050:
051: public class MaxValue<T> extends
052: UnaryFunctor<Collection<? extends T>, T> {
053:
054: static final long serialVersionUID = -832437021573535619L;
055:
056: private Comparator<T> _comp;
057:
058: /**
059: * Builds a MaxValue functor that will use the given comparator to
060: * compare elements in the collection. Typically, the functor would compare
061: * its two arguments and return the greater value.
062: * @throws IllegalArgumentException if the Comparator is null
063: */
064: public MaxValue(Comparator<T> comp) {
065: if (comp == null)
066: throw new IllegalArgumentException();
067:
068: _comp = comp;
069: }
070:
071: /**
072: * Returns the comparator used to order values in the collection.
073: */
074: public Comparator<T> getComparator() {
075: return _comp;
076: }
077:
078: /**
079: * Returns the largest value in the collection
080: * @throws NoSuchElementException if the collection is empty
081: */
082: public T fn(Collection<? extends T> collection) {
083: return Collections.max(collection, _comp);
084: }
085:
086: /**
087: * Calls the Visitor's <code>visit(MaxValue)</code> method, if it
088: * implements the nested Visitor interface.
089: */
090: public void accept(net.sf.jga.fn.Visitor v) {
091: if (v instanceof MaxValue.Visitor)
092: ((MaxValue.Visitor) v).visit(this );
093: else
094: v.visit(this );
095: }
096:
097: // Object overrides
098:
099: public String toString() {
100: return "MaxValue";
101: }
102:
103: // AcyclicVisitor
104:
105: /**
106: * Interface for classes that may interpret an <b>MaxValue</b> functor.
107: */
108: public interface Visitor extends net.sf.jga.fn.Visitor {
109: public void visit(MaxValue host);
110: }
111: }
|