// ============================================================================
// $Id: InstanceOf.java,v 1.8 2006/04/26 03:35:01 davidahall Exp $
// Copyright (c) 2004-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.property;
import net.sf.jga.fn.UnaryPredicate;
/**
* Functor that returns true if the argument is an instance of given type.
* <p>
* NOTE: the generic parm T is not related to the class parameter. If there was
* such a relationship (ie, if the class paramater given to the constructor was
* declared-- if it were declared Class<T> rather than Class<?>,
* then the compiler would require that the arguemnt be of the given type to
* compile code that uses this functor, eliminating the need for the functor
* in the first plase. Instead, the generic parm is the class of objects that
* will be tested.
*
* <p>
* Copyright © 2004-2005 David A. Hall
*
* @author <a href="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
**/
public class InstanceOf<T> extends UnaryPredicate<T> {
static final long serialVersionUID = -1792964506358538850L;
private Class<?> _class;
/**
* Builds a InstanceOf predicate that tests against the given class.
* @throws IllegalArgumentException if the class is null.
*/
public InstanceOf(Class<?> cl) {
if (cl == null)
throw new IllegalArgumentException("A class must be given");
_class = cl;
}
public Class<?> getTestClass() { return _class; }
// UnaryPredicate interface
public Boolean fn(T arg) {
return _class.isInstance(arg);
}
/**
* Calls the Visitor's <code>visit(InstanceOf)</code> method, if it
* implements the nested Visitor interface.
*/
public void accept(net.sf.jga.fn.Visitor v) {
if (v instanceof InstanceOf.Visitor)
((InstanceOf.Visitor)v).visit(this);
else
v.visit(this);
}
// Object overrides
public String toString() {
return "InstanceOf["+_class.getName()+"]";
}
// AcyclicVisitor
/**
* Interface for classes that may interpret a <b>InstanceOf</b>
* function.
*/
public interface Visitor extends net.sf.jga.fn.Visitor {
public void visit(InstanceOf host);
}
}
|