KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > ViewId


1 // $Id: ViewId.java,v 1.8 2004/10/07 15:46:29 belaban Exp $
2

3 package org.jgroups;
4
5 import org.jgroups.util.Streamable;
6 import org.jgroups.util.Util;
7
8 import java.io.*;
9
10
11 /**
12  * ViewIds are used for ordering views (each view has a ViewId and a list of members).
13  * Ordering between views is important for example in a virtual synchrony protocol where
14  * all views seen by a member have to be ordered.
15  */

16 public class ViewId implements Externalizable, Comparable JavaDoc, Cloneable JavaDoc, Streamable {
17     Address coord_addr=null; // Address of the issuer of this view
18
long id=0; // Lamport time of the view
19

20
21     public ViewId() { // used for externalization
22
}
23
24
25     /**
26      * Creates a ViewID with the coordinator address and a Lamport timestamp of 0.
27      *
28      * @param coord_addr the address of the member that issued this view
29      */

30     public ViewId(Address coord_addr) {
31         this.coord_addr=coord_addr;
32     }
33
34     /**
35      * Creates a ViewID with the coordinator address and the given Lamport timestamp.
36      *
37      * @param coord_addr - the address of the member that issued this view
38      * @param id - the Lamport timestamp of the view
39      */

40     public ViewId(Address coord_addr, long id) {
41         this.coord_addr=coord_addr;
42         this.id=id;
43     }
44
45     /**
46      * returns the lamport time of the view
47      *
48      * @return the lamport time timestamp
49      */

50     public long getId() {
51         return id;
52     }
53
54
55     /**
56      * returns the address of the member that issued this view
57      *
58      * @return the Address of the the issuer
59      */

60     public Address getCoordAddress() {
61         return coord_addr;
62     }
63
64
65     public String JavaDoc toString() {
66         return "[" + coord_addr + '|' + id + ']';
67     }
68
69     /**
70      * Cloneable interface
71      * Returns a new ViewID object containing the same address and lamport timestamp as this view
72      */

73     public Object JavaDoc clone() {
74         return new ViewId(coord_addr, id);
75     }
76
77     /**
78      * Old Copy method, deprecated because it is substituted by clone()
79      */

80     public ViewId copy() {
81         return (ViewId)clone();
82     }
83
84     /**
85      * Establishes an order between 2 ViewIds. First compare on id. <em>Compare on coord_addr
86      * only if necessary</em> (i.e. ids are equal) !
87      *
88      * @return 0 for equality, value less than 0 if smaller, greater than 0 if greater.
89      */

90     public int compareTo(Object JavaDoc other) {
91         if(other == null) return 1; //+++ Maybe necessary to throw an exception
92

93         if(!(other instanceof ViewId)) {
94             throw new ClassCastException JavaDoc("ViewId.compareTo(): view id is not comparable with different Objects");
95         }
96         try {
97             return id > ((ViewId)other).id ? 1 : id < ((ViewId)other).id ? -1 : 0;
98         }
99         catch(NullPointerException JavaDoc e) {
100             System.err.println("ViewId.compareTo(): " + e);
101             throw e;
102         }
103     }
104
105     /**
106      * Old Compare
107      */

108     public int compare(Object JavaDoc o) {
109         return compareTo(o);
110     }
111
112
113     public boolean equals(Object JavaDoc other_view) {
114         return compareTo(other_view) == 0 ? true : false;
115     }
116
117
118     public int hashCode() {
119         return (int)id;
120     }
121
122
123     public void writeExternal(ObjectOutput out) throws IOException {
124         out.writeObject(coord_addr);
125         out.writeLong(id);
126     }
127
128
129     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
130         coord_addr=(Address)in.readObject();
131         id=in.readLong();
132     }
133
134     public void writeTo(DataOutputStream out) throws IOException {
135         Util.writeAddress(coord_addr, out);
136         out.writeLong(id);
137     }
138
139     public void readFrom(DataInputStream in) throws IOException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
140         coord_addr=Util.readAddress(in);
141         id=in.readLong();
142     }
143
144 }
145
Popular Tags