View Javadoc

1   /*
2    *  jDTAUS Core API
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.monitor;
22  
23  import java.util.EventObject;
24  
25  /**
26   * Event produced by a {@code Task}.
27   *
28   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
29   * @version $JDTAUS: TaskEvent.java 8641 2012-09-27 06:45:17Z schulte $
30   *
31   * @see TaskEventSource
32   */
33  public class TaskEvent extends EventObject
34  {
35      //--Constants---------------------------------------------------------------
36  
37      /** Event constant indicating the start of a {@code Task}. */
38      public static final int STARTED = 1001;
39  
40      /** Event constant indicating that state of a {@code Task} changed. */
41      public static final int CHANGED_STATE = 1002;
42  
43      /** Event constant indicating the end of a {@code Task}. */
44      public static final int ENDED = 1003;
45  
46      /** Serial version UID for backwards compatibility with 1.0.x classes. */
47      private static final long serialVersionUID = 4764885368541939098L;
48  
49      //---------------------------------------------------------------Constants--
50      //--Constructors------------------------------------------------------------
51  
52      /**
53       * Creates a new instance of {@code TaskEvent}.
54       *
55       * @param source the task producing the new event.
56       * @param type constant indicating the type of the event.
57       *
58       * @throws IllegalArgumentException if {@code type} is not equal to one of
59       * the constants {@code STARTED}, {@code CHANGED_STATE} or {@code ENDED}.
60       */
61      public TaskEvent( final Task source, final int type )
62      {
63          super( source );
64  
65          if ( type != STARTED && type != CHANGED_STATE && type != ENDED )
66          {
67              throw new IllegalArgumentException( Integer.toString( type ) );
68          }
69  
70          this.type = type;
71      }
72  
73      //------------------------------------------------------------Constructors--
74      //--TaskEvent---------------------------------------------------------------
75  
76      /**
77       * Event type.
78       * @serial
79       */
80      private final int type;
81  
82      /**
83       * Getter for property {@code type}.
84       *
85       * @return the type of the event.
86       */
87      public final int getType()
88      {
89          return this.type;
90      }
91  
92      /**
93       * Gets the {@code Task} producing the event.
94       *
95       * @return the source of the event.
96       */
97      public final Task getTask()
98      {
99          return (Task) this.getSource();
100     }
101 
102     /**
103      * Creates a string representing the properties of the instance.
104      *
105      * @return a string representing the properties of the instance.
106      */
107     private String internalString()
108     {
109         return new StringBuffer( 500 ).append( '{' ).
110             append( "source=" ).append( this.source ).
111             append( ", type=" ).append( this.type ).
112             append( '}' ).toString();
113 
114     }
115 
116     //---------------------------------------------------------------TaskEvent--
117     //--Object------------------------------------------------------------------
118 
119     /**
120      * Returns a string representation of the object.
121      *
122      * @return a string representation of the object.
123      */
124     public String toString()
125     {
126         return super.toString() + this.internalString();
127     }
128 
129     //------------------------------------------------------------------Object--
130 }