KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > constraint > IsEventFrom


1 /** Created on Jun 28, 2002 by npryce
2  * Copyright (c) B13media Ltd.
3  */

4 package com.mockobjects.constraint;
5
6 import java.util.EventObject JavaDoc;
7
8
9 /** Tests if the value is an event announced by a specific object.
10  */

11 public class IsEventFrom
12     implements Constraint
13 {
14     private Class JavaDoc _event_class;
15     private Object JavaDoc _source;
16     
17     /** Constructs an IsEventFrom predicate that returns true for any object
18      * derived from {@link java.util.EventObject} announced by
19      * <var>source</var>.
20      */

21     public IsEventFrom( Object JavaDoc source ) {
22         this( EventObject JavaDoc.class, source );
23     }
24     
25     /** Constructs an IsEventFrom predicate that returns true for any object
26      * derived from <var>event_class</var> announced by
27      * <var>source</var>.
28      */

29     public IsEventFrom( Class JavaDoc event_class, Object JavaDoc source ) {
30         _event_class = event_class;
31         _source = source;
32     }
33     
34     public boolean eval( Object JavaDoc o ) {
35         if( o instanceof EventObject JavaDoc ) {
36             EventObject JavaDoc ev = (EventObject JavaDoc)o;
37             return _event_class.isInstance(o) && ev.getSource() == _source;
38             
39         } else {
40             return false;
41         }
42     }
43     
44     public String JavaDoc toString() {
45         return "an event of type " + _event_class.getName() +
46                " from " + _source;
47     }
48 }
49
Popular Tags