Home | History | Annotate | Download | only in core
      1 /*  Copyright (c) 2000-2006 hamcrest.org
      2  */
      3 package org.hamcrest.core;
      4 
      5 import org.hamcrest.Description;
      6 import org.hamcrest.Matcher;
      7 import org.hamcrest.Factory;
      8 import org.hamcrest.BaseMatcher;
      9 
     10 
     11 /**
     12  * Tests whether the value is an instance of a class.
     13  */
     14 public class IsInstanceOf extends BaseMatcher<Object> {
     15     private final Class<?> theClass;
     16 
     17     /**
     18      * Creates a new instance of IsInstanceOf
     19      *
     20      * @param theClass The predicate evaluates to true for instances of this class
     21      *                 or one of its subclasses.
     22      */
     23     public IsInstanceOf(Class<?> theClass) {
     24         this.theClass = theClass;
     25     }
     26 
     27     public boolean matches(Object item) {
     28         return theClass.isInstance(item);
     29     }
     30 
     31     public void describeTo(Description description) {
     32         description.appendText("an instance of ")
     33                 .appendText(theClass.getName());
     34     }
     35 
     36     /**
     37      * Is the value an instance of a particular type?
     38      */
     39     @Factory
     40     public static Matcher<Object> instanceOf(Class<?> type) {
     41         return new IsInstanceOf(type);
     42     }
     43 
     44 }
     45