KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > fn > property > InstanceOf


1 // ============================================================================
2
// $Id: InstanceOf.java,v 1.8 2006/04/26 03:35:01 davidahall Exp $
3
// Copyright (c) 2004-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.fn.property;
34
35 import net.sf.jga.fn.UnaryPredicate;
36
37 /**
38  * Functor that returns true if the argument is an instance of given type.
39  * <p>
40  * NOTE: the generic parm T is not related to the class parameter. If there was
41  * such a relationship (ie, if the class paramater given to the constructor was
42  * declared-- if it were declared Class&lt;T&gt; rather than Class&lt;?&gt;,
43  * then the compiler would require that the arguemnt be of the given type to
44  * compile code that uses this functor, eliminating the need for the functor
45  * in the first plase. Instead, the generic parm is the class of objects that
46  * will be tested.
47  *
48  * <p>
49  * Copyright &copy; 2004-2005 David A. Hall
50  *
51  * @author <a HREF="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
52  **/

53
54 public class InstanceOf<T> extends UnaryPredicate<T> {
55
56     static final long serialVersionUID = -1792964506358538850L;
57
58     private Class JavaDoc<?> _class;
59
60     /**
61      * Builds a InstanceOf predicate that tests against the given class.
62      * @throws IllegalArgumentException if the class is null.
63      */

64     public InstanceOf(Class JavaDoc<?> cl) {
65         if (cl == null)
66             throw new IllegalArgumentException JavaDoc("A class must be given");
67         
68         _class = cl;
69     }
70
71
72     public Class JavaDoc<?> getTestClass() { return _class; }
73     
74     // UnaryPredicate interface
75

76     public Boolean JavaDoc fn(T arg) {
77         return _class.isInstance(arg);
78     }
79
80     /**
81      * Calls the Visitor's <code>visit(InstanceOf)</code> method, if it
82      * implements the nested Visitor interface.
83      */

84     public void accept(net.sf.jga.fn.Visitor v) {
85         if (v instanceof InstanceOf.Visitor)
86             ((InstanceOf.Visitor)v).visit(this);
87         else
88             v.visit(this);
89     }
90     
91     // Object overrides
92

93     public String JavaDoc toString() {
94         return "InstanceOf["+_class.getName()+"]";
95     }
96     
97    // AcyclicVisitor
98

99     /**
100      * Interface for classes that may interpret a <b>InstanceOf</b>
101      * function.
102      */

103     public interface Visitor extends net.sf.jga.fn.Visitor {
104         public void visit(InstanceOf host);
105     }
106 }
107
Popular Tags