View Javadoc

1   /*
2    * Copyright (c) 1998-2002 The Jgroup Team.
3    *
4    * This program is free software; you can redistribute it and/or modify
5    * it under the terms of the GNU Lesser General Public License version 2 as
6    * published by the Free Software Foundation.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Lesser General Public License for more details.
12   *
13   * You should have received a copy of the GNU Lesser General Public License
14   * along with this program; if not, write to the Free Software
15   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16   *
17   */
18  
19  package jgroup.relacs.types;
20  
21  import java.io.Externalizable;
22  import java.io.IOException;
23  import java.io.ObjectInput;
24  import java.io.ObjectOutput;
25  
26  import jgroup.core.EndPoint;
27  
28  /**
29   *  A <code>LocalId</code> object is used to distinguish multiple local
30   *  members associated with a single Jgroup daemon.  It is comprised of
31   *  the member number and its <code>EndPoint</code>.
32   *
33   *  @author Hein Meling
34   *  @since Jgroup 1.2
35   */
36  public final class LocalId
37    implements Externalizable
38  {
39  
40    ////////////////////////////////////////////////////////////////////////////////////////////
41    // Constants
42    ////////////////////////////////////////////////////////////////////////////////////////////
43  
44    private static final long serialVersionUID = -340854940914476631L;
45  
46    /** Size of this object in bytes */
47    public static final int SIZE = 2 + EndPointImpl.SIZE;
48  
49  
50    ////////////////////////////////////////////////////////////////////////////////////////////
51    // Fields
52    ////////////////////////////////////////////////////////////////////////////////////////////
53    
54    /** Member number */
55    private int member;
56  
57    /** The local member endpoint address */
58    private EndPoint endpoint;
59  
60  
61    ////////////////////////////////////////////////////////////////////////////////////////////
62    // Constructor
63    ////////////////////////////////////////////////////////////////////////////////////////////
64  
65    /** Default constructor for externalization */
66    public LocalId() {}
67  
68  
69    /**
70     *  Creates a <CODE>LocalId</CODE> object containing the specified
71     *  member number, and member <code>EndPoint</code> address.
72     */
73    public LocalId(int member, EndPoint membEndPoint)
74    {
75      this.member = member;
76      this.endpoint = membEndPoint;
77    }
78  
79  
80    ////////////////////////////////////////////////////////////////////////////////////////////
81    // Access methods
82    ////////////////////////////////////////////////////////////////////////////////////////////
83  
84    public EndPoint getEndpoint()
85    {
86      return endpoint;
87    }
88  
89    public int getMemberNo()
90    {
91      return member;
92    }
93  
94    ////////////////////////////////////////////////////////////////////////////////////////////
95    // Marshaling / unmarshaling methods
96    ////////////////////////////////////////////////////////////////////////////////////////////
97  
98    /**
99     *  Restores the content of this object from the marshalled data contained
100    *  in the specified input stream.
101    * 
102    *  @param in the stream to be read
103    */
104   public void readExternal(ObjectInput in)
105     throws IOException, ClassNotFoundException
106   {
107     member = in.readUnsignedShort();
108     endpoint = new EndPointImpl();
109     endpoint.readExternal(in);
110   }
111 
112 
113   /**
114    *  Marshals the content of this object to the specified output stream.
115    * 
116    *  @param out the stream to be written
117    */
118   public void writeExternal(ObjectOutput out)
119     throws IOException
120   {
121     out.writeShort(member);
122     endpoint.writeExternal(out);
123   }
124 
125 
126   ////////////////////////////////////////////////////////////////////////////////////////////
127   // Object methods
128   ////////////////////////////////////////////////////////////////////////////////////////////
129 
130   /**
131    *  Returns <code>true</code> if and only if the given object is a non-null
132    *  <code>LocalId</code> object, whose member number and endpoint address
133    *  is equal to this object.  Otherwise, <code>false</code> is returned.
134    */
135   public boolean equals(Object obj) 
136   {
137     if (!(obj instanceof LocalId)) {
138       return false;
139     } else {
140       LocalId localId = (LocalId) obj;
141       return (member == localId.member && endpoint.equals(localId.endpoint));
142     }
143   }
144 
145 
146   /**
147    *  Returns a hashcode for this <code>LocalId</code> object.
148    */
149   public int hashCode() 
150   {
151     return member ^ endpoint.hashCode();
152   }
153 
154   /**
155    *  Returns a string representation of this LocalId.
156    */
157   public String toString()
158   {
159     StringBuilder buf = new StringBuilder();
160     buf.append(endpoint);
161     buf.append(".");
162     buf.append(member);
163     return buf.toString();
164   }
165   
166 } // END LocalId