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.string;
19  
20  import java.util.Arrays;
21  
22  import fulmine.event.AbstractEvent;
23  import fulmine.event.IEventSource;
24  import fulmine.event.listener.IEventListener;
25  import fulmine.model.container.IContainer;
26  
27  /**
28   * A receiving event encapsulates the <code>byte[]</code> that holds the state
29   * change in wire form of an {@link IContainer} received from a remote context.
30   * <p>
31   * This implementation of an {@link ISystemEvent} can be raised by a non-
32   * {@link ISystemEventSource}. This is so that an {@link IEventListener} for an
33   * {@link IContainer} will not receive these events.
34   * 
35   * @author Ramon Servadei
36   */
37  public final class RxEvent extends AbstractEvent implements ISystemEvent
38  {
39      /** The receive buffer */
40      private final byte[] buffer;
41  
42      /**
43       * Standard constructor
44       * 
45       * @param source
46       *            the container for this receiving event (the container is the
47       *            event source)
48       * @param buffer
49       *            the buffer encapsulating the change to apply to the container
50       */
51      public RxEvent(IEventSource source, byte[] buffer)
52      {
53          super();
54          setSource(source);
55          this.buffer = buffer;
56      }
57  
58      /**
59       * Get the byte[] holding the received state in wire form
60       * 
61       * @return a <code>byte[]</code>
62       */
63      public byte[] getBuffer()
64      {
65          return buffer;
66      }
67  
68      /**
69       * Provides a trace string the shows the byte level contents of the buffer
70       * data. Used for fine grain debugging.
71       * 
72       * @return a trace string
73       */
74      public String getTraceString()
75      {
76          return string(this, "buffer=" + Arrays.toString(getBuffer())
77              + ", source=" + getSource());
78      }
79  
80      protected String getAdditionalToString()
81      {
82          return "buffer=[" + getBuffer().length + " bytes]";
83      }
84  }