View Javadoc

1   // Copyright (C) 2003, 2004, 2005 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.communication;
23  
24  import java.util.Random;
25  
26  
27  /**
28   *  Simple Message implementation.
29   *
30   * @author Philip Aston
31   */
32  public final class SimpleMessage implements Message {
33  
34    private static Random s_random = new Random();
35  
36    private final String m_text = "Some message";
37    private final int m_random = s_random.nextInt();
38    private final int[] m_padding;
39    private Object m_payload;
40  
41    public SimpleMessage() {
42      this(30);
43    }
44  
45    public SimpleMessage(int paddingSize) {
46  
47      m_padding = new int[paddingSize];
48  
49      for (int i=0; i<paddingSize; i++) {
50        m_padding[i] = i;
51      }
52    }
53  
54    public void setPayload(Object payload) {
55      m_payload = payload;
56    }
57  
58    public Object getPayload() {
59      return m_payload;
60    }
61  
62    public String toString() {
63      return "(" + m_text + ", " + m_random + ")";
64    }
65  
66    public boolean equals(Object o) {
67      if (o == this) {
68        return true;
69      }
70  
71      if (!(o instanceof SimpleMessage)) {
72        return false;
73      }
74  
75      final SimpleMessage other = (SimpleMessage)o;
76  
77      return m_text.equals(other.m_text) && m_random == other.m_random;
78    }
79  
80    public int hashCode() {
81      return m_text.hashCode() ^ m_random;
82    }
83  }
84