Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: edu/ucsb/ccs/jaqual/standard/InstanceOf.java


1   package edu.ucsb.ccs.jaqual.standard;
2   
3   import edu.ucsb.ccs.jaqual.Assertion;
4   
5   /**
6    * Assertion to check if an object is an instance of a certain class type.
7    *
8    * <p>Example use:
9    * <pre>
10   *   ForAll.in(elements).ensure(new InstanceOf(Integer.class));
11   * </pre>
12   *
13   * @author Parker Abercrombie
14   * @version $Id: InstanceOf.java,v 1.2 2002/07/11 19:58:33 parkera Exp $
15   */
16  
17  public class InstanceOf implements Assertion {
18  
19    /**
20     * The class to match instances of.
21     */
22    protected Class targetClass;
23  
24    /**
25     * Create a new assertion.
26     *
27     * @param clazz the cass to match instances of.
28     */ 
29    public InstanceOf (Class clazz) {
30      targetClass = clazz;
31    }
32  
33    /**
34     * Test an object to see if it is an instance of the target class.
35     *
36     * @param o the object to test.
37     *
38     * @return true if `o' is an instance of the target class.  False if
39     *         `o' is null, or not an instance.
40     */
41    public boolean eval (Object o) {
42      return (o != null)
43        && targetClass.isAssignableFrom(o.getClass());
44    }
45  }