View Javadoc

1   /*
2      Copyright 2007 Ramon Servadei
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package fulmine.event;
17  
18  import static fulmine.util.Utils.COMMA_SPACE;
19  import static fulmine.util.Utils.EMPTY_STRING;
20  import static fulmine.util.Utils.logException;
21  import static fulmine.util.Utils.nullCheck;
22  import static fulmine.util.Utils.safeToString;
23  import static fulmine.util.Utils.string;
24  import fulmine.IAddressable;
25  import fulmine.IDomain;
26  import fulmine.IType;
27  
28  /**
29   * Base-class for {@link IEvent} instances that are marker events. A marker
30   * event represents an arbitrary event and is not necessarily related to the
31   * state of its {@link IEventSource}. Therefore marker events cannot be
32   * coalesced and thus the {@link #equals(Object)} and {@link #hashCode()}
33   * methods are final and perform the standard operation exposed by the
34   * {@link Object} class.
35   * 
36   * @author Ramon Servadei
37   */
38  public abstract class AbstractEvent implements IEvent
39  {
40      /** The source for this */
41      private IEventSource source;
42  
43      /** The frame */
44      private IEventFrameExecution frame;
45  
46      /** The driving frame */
47      private IEventFrameExecution drivingFrame;
48  
49      private IAddressable address;
50  
51      /**
52       * The trigger event, this is queued via
53       * {@link IEventManager#queueEvent(IEvent)} <b>after</b> this event has been
54       * processed.
55       */
56      private IEvent triggerEvent;
57  
58      /**
59       * Public constructor
60       */
61      public AbstractEvent()
62      {
63          super();
64      }
65  
66      public final IEventFrameExecution getDrivingFrame()
67      {
68          return this.drivingFrame;
69      }
70  
71      public final IEventFrameExecution getFrame()
72      {
73          return this.frame;
74      }
75  
76      public final IEventSource getSource()
77      {
78          return this.source;
79      }
80  
81      public final void setSource(IEventSource source)
82      {
83          this.source = source;
84          setAddress(source);
85      }
86  
87      protected final void setAddress(IAddressable address)
88      {
89          nullCheck(address, "null address");
90          this.address = address;
91      }
92  
93      public void setFrame(IEventFrameExecution frame)
94      {
95          this.frame = frame;
96      }
97  
98      public void setDrivingFrame(IEventFrameExecution drivingFrame)
99      {
100         this.drivingFrame = drivingFrame;
101     }
102 
103     @Override
104     public Object clone() throws CloneNotSupportedException
105     {
106         return super.clone();
107     }
108 
109     public String toDetailedString()
110     {
111         return getString(true);
112     }
113 
114     public String toIdentityString()
115     {
116         return getClass().getSimpleName();
117     }
118 
119     public final IDomain getDomain()
120     {
121         return this.address.getDomain();
122     }
123 
124     public final String getIdentity()
125     {
126         return this.address.getIdentity();
127     }
128 
129     public final IType getType()
130     {
131         return this.address.getType();
132     }
133 
134     public final String getAddress()
135     {
136         return this.address.getAddress();
137     }
138 
139     public void setTriggerEvent(IEvent triggerEvent)
140     {
141         this.triggerEvent = triggerEvent;
142     }
143 
144     public IEvent getTriggerEvent()
145     {
146         return this.triggerEvent;
147     }
148 
149     /**
150      * Delegate method for subclasses to provide additional toString
151      * information. This should be a comma separated string, e.g.
152      * "one, two, three"
153      * 
154      * @return the additional toString information in comma separated format
155      */
156     protected String getAdditionalToString()
157     {
158         return EMPTY_STRING;
159     }
160 
161     private String getString(boolean detailed)
162     {
163         try
164         {
165             final String additionalToString = getAdditionalToString();
166             StringBuilder sb = new StringBuilder();
167             boolean addSeparator = false;
168             if (getSource() != null && !hideSourceFromToString())
169             {
170                 sb.append("source=").append(
171                     (detailed ? getSource().toDetailedString()
172                         : getSource().toIdentityString()));
173                 addSeparator = true;
174             }
175             if (getFrame() != null)
176             {
177                 if (addSeparator)
178                 {
179                     sb.append(COMMA_SPACE);
180                 }
181                 sb.append("frame=").append(getFrame());
182                 addSeparator = true;
183             }
184             if (getDrivingFrame() != null)
185             {
186                 if (addSeparator)
187                 {
188                     sb.append(COMMA_SPACE);
189                 }
190                 sb.append("drivingFrame=").append(getDrivingFrame());
191                 addSeparator = true;
192             }
193             if (additionalToString != EMPTY_STRING)
194             {
195                 if (addSeparator)
196                 {
197                     sb.append(COMMA_SPACE);
198                 }
199                 sb.append(additionalToString);
200             }
201             return string(this, sb.toString());
202         }
203         catch (Exception e)
204         {
205             String address = safeToString(this.address);
206             logException(null, address, e);
207             return string(this, address);
208         }
209 
210     }
211 
212     /**
213      * Allows sub-classes the ability to hide the source attribute from the
214      * {@link #toString()} calls.
215      * 
216      * @return <code>true</code> if the source attribute should not be shown in
217      *         string representations. Default is <code>false</code>.
218      */
219     protected boolean hideSourceFromToString()
220     {
221         return false;
222     }
223 
224     @Override
225     public final String toString()
226     {
227         return getString(false);
228     }
229 
230     @Override
231     public final int hashCode()
232     {
233         return super.hashCode();
234     }
235 
236     @Override
237     public final boolean equals(Object obj)
238     {
239         return super.equals(obj);
240     }
241 }