KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > core > constraint > IsEventFrom


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package org.jmock.core.constraint;
4
5 import java.util.EventObject JavaDoc;
6 import org.jmock.core.Constraint;
7
8
9 /**
10  * Tests if the value is an event announced by a specific object.
11  */

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

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

30     public IsEventFrom( Class JavaDoc eventClass, Object JavaDoc source ) {
31         this.eventClass = eventClass;
32         this.source = source;
33     }
34
35     public boolean eval( Object JavaDoc o ) {
36         return (o instanceof EventObject JavaDoc)
37             && eventClass.isInstance(o)
38             && eventHasSameSource((EventObject JavaDoc)o);
39     }
40
41     private boolean eventHasSameSource(EventObject JavaDoc ev) {
42         return ev.getSource() == source;
43     }
44
45     public StringBuffer JavaDoc describeTo( StringBuffer JavaDoc buffer ) {
46         return buffer.append("an event of type ")
47                 .append(eventClass.getName())
48                 .append(" from ")
49                 .append(source);
50     }
51 }
52
Popular Tags