View Javadoc

1   /*
2    Copyright 2008 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.system;
17  
18  import static fulmine.util.Utils.nullCheck;
19  import fulmine.IAddressable;
20  import fulmine.event.AbstractEvent;
21  import fulmine.event.IEventManager;
22  
23  /**
24   * Base class for {@link ISystemEvent} instances.
25   * <p>
26   * Note that it is not a good idea to have multiple levels of inheritance for
27   * system events. All <i>true</i> system events should only extend this and
28   * should not be further extended. This is to prevent logic bugs with any code
29   * that does <code>instanceof</code> checks on an event type. Events should not
30   * extend each other unless there is a genuine semantic meaning for the
31   * inheritance. The drawback is that events with similar internal constructs
32   * have to be re-written each time.
33   * 
34   * @author Ramon Servadei
35   */
36  public class AbstractSystemEvent extends AbstractEvent implements ISystemEvent
37  {
38      /**
39       * Create a system event
40       * 
41       * @param context
42       *            the context for the {@link ISystemEventSource} that generates
43       *            this event
44       */
45      public AbstractSystemEvent(IEventManager context)
46      {
47          super();
48          nullCheck(context, "Null context");
49          setSource(context.getSystemEventSource(getClass()));
50      }
51  
52      /**
53       * Create a system event with a specific address
54       * 
55       * @param context
56       *            the context for the {@link ISystemEventSource} that generates
57       *            this event
58       * @param address
59       *            the {@link IAddressable} for this event
60       */
61      protected AbstractSystemEvent(IEventManager context, IAddressable address)
62      {
63          this(context);
64          setAddress(address);
65      }
66  
67      @Override
68      protected String getAdditionalToString()
69      {
70          return getAddress();
71      }
72  
73      @Override
74      protected boolean hideSourceFromToString()
75      {
76          return true;
77      }
78  }