001: // ============================================================================
002: // $Id: InstanceOf.java,v 1.8 2006/04/26 03:35:01 davidahall Exp $
003: // Copyright (c) 2004-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.property;
034:
035: import net.sf.jga.fn.UnaryPredicate;
036:
037: /**
038: * Functor that returns true if the argument is an instance of given type.
039: * <p>
040: * NOTE: the generic parm T is not related to the class parameter. If there was
041: * such a relationship (ie, if the class paramater given to the constructor was
042: * declared-- if it were declared Class<T> rather than Class<?>,
043: * then the compiler would require that the arguemnt be of the given type to
044: * compile code that uses this functor, eliminating the need for the functor
045: * in the first plase. Instead, the generic parm is the class of objects that
046: * will be tested.
047: *
048: * <p>
049: * Copyright © 2004-2005 David A. Hall
050: *
051: * @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
052: **/
053:
054: public class InstanceOf<T> extends UnaryPredicate<T> {
055:
056: static final long serialVersionUID = -1792964506358538850L;
057:
058: private Class<?> _class;
059:
060: /**
061: * Builds a InstanceOf predicate that tests against the given class.
062: * @throws IllegalArgumentException if the class is null.
063: */
064: public InstanceOf(Class<?> cl) {
065: if (cl == null)
066: throw new IllegalArgumentException("A class must be given");
067:
068: _class = cl;
069: }
070:
071: public Class<?> getTestClass() {
072: return _class;
073: }
074:
075: // UnaryPredicate interface
076:
077: public Boolean fn(T arg) {
078: return _class.isInstance(arg);
079: }
080:
081: /**
082: * Calls the Visitor's <code>visit(InstanceOf)</code> method, if it
083: * implements the nested Visitor interface.
084: */
085: public void accept(net.sf.jga.fn.Visitor v) {
086: if (v instanceof InstanceOf.Visitor)
087: ((InstanceOf.Visitor) v).visit(this );
088: else
089: v.visit(this );
090: }
091:
092: // Object overrides
093:
094: public String toString() {
095: return "InstanceOf[" + _class.getName() + "]";
096: }
097:
098: // AcyclicVisitor
099:
100: /**
101: * Interface for classes that may interpret a <b>InstanceOf</b>
102: * function.
103: */
104: public interface Visitor extends net.sf.jga.fn.Visitor {
105: public void visit(InstanceOf host);
106: }
107: }
|