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 8 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.IOException;
22  import java.io.ObjectInput;
23  import java.io.ObjectOutput;
24  
25  import jgroup.core.EndPoint;
26  
27  /**
28   *  The <code>ViewId</code> class wraps a value of the relacs daemon
29   *  type "group id" in an object.  A group id uniquely identifies
30   *  groups.  An object of type <code>ViewId</code> contains a single
31   *  field whose type is long.
32   *
33   *  @author Alberto Montresor
34   *  @since  Jgroup 1.2
35   */
36  
37  public final class ViewId
38  {
39    
40    ////////////////////////////////////////////////////////////////////////////////////////////
41    // Constants
42    ////////////////////////////////////////////////////////////////////////////////////////////
43  
44    /** Size of this object in bytes (when marshalled) */
45    public static final int SIZE = 8;
46  
47  
48    ////////////////////////////////////////////////////////////////////////////////////////////
49    // Fields
50    ////////////////////////////////////////////////////////////////////////////////////////////
51    
52    /** Wrapped value */
53    private long value;
54  
55  
56    ////////////////////////////////////////////////////////////////////////////////////////////
57    // Constructor
58    ////////////////////////////////////////////////////////////////////////////////////////////
59    
60    /**
61     *  Creates a <CODE>ViewId</CODE> object containing the specified
62     *  value.
63     */
64    public ViewId(long value)
65    {
66      this.value = value;
67    }
68    
69    
70    ////////////////////////////////////////////////////////////////////////////////////////////
71    // Accessor method
72    ////////////////////////////////////////////////////////////////////////////////////////////
73    
74    /**
75     *  Returned the wrapped value.
76     */
77    public long getValue()
78    {
79      return value;
80    }
81    
82    ////////////////////////////////////////////////////////////////////////////////////////////
83    // Marshaling / unmarshaling methods
84    ////////////////////////////////////////////////////////////////////////////////////////////
85    
86    /**
87     *  Write the specified message id to an outgoing message.
88     */
89    public static void marshal(ObjectOutput msg, long value)
90      throws IOException
91    {
92      msg.writeLong(value);
93    }
94    
95    /**
96     *  Unmarshals a view id from an incoming message.
97     */
98    public static long unmarshal(ObjectInput msg)
99      throws IOException
100   {
101     return msg.readLong();
102   }
103 
104   /**
105    * Returns a new complete view identifier obtained from the host identifier and
106    * a sequence number.
107    *
108    * @param     host    the host idientifer.
109    * @param     counter sequence number.
110    */
111   public static long create(EndPoint host, int counter)
112   {
113     int hid = host.getIntAddress();
114     // FIXME Should we use also port to construct the view id?
115     return ((long)(hid) << 32) | (counter & 0xFFFFFFFFL);
116   }
117   
118   /**
119    * Returns a new partial view identifier obtained by a complete view identifier
120    * and an additional sequence number.
121    *
122    * @param     cvid    the complete view identifier.
123    * @param     counter the additional counter.
124    */
125   public static long createPartial(long cvid, int counter)
126   {
127     return (cvid + counter);
128   }
129 
130 } // END ViewId