001: // $Id: ViewId.java,v 1.11 2006/08/14 16:05:09 belaban Exp $
002:
003: package org.jgroups;
004:
005: import org.jgroups.util.Streamable;
006: import org.jgroups.util.Util;
007:
008: import java.io.*;
009:
010: /**
011: * ViewIds are used for ordering views (each view has a ViewId and a list of members).
012: * Ordering between views is important for example in a virtual synchrony protocol where
013: * all views seen by a member have to be ordered.
014: */
015: public class ViewId implements Externalizable, Comparable, Cloneable,
016: Streamable {
017: Address coord_addr = null; // Address of the issuer of this view
018: long id = 0; // Lamport time of the view
019:
020: public ViewId() { // used for externalization
021: }
022:
023: /**
024: * Creates a ViewID with the coordinator address and a Lamport timestamp of 0.
025: *
026: * @param coord_addr the address of the member that issued this view
027: */
028: public ViewId(Address coord_addr) {
029: this .coord_addr = coord_addr;
030: }
031:
032: /**
033: * Creates a ViewID with the coordinator address and the given Lamport timestamp.
034: *
035: * @param coord_addr - the address of the member that issued this view
036: * @param id - the Lamport timestamp of the view
037: */
038: public ViewId(Address coord_addr, long id) {
039: this .coord_addr = coord_addr;
040: this .id = id;
041: }
042:
043: /**
044: * returns the lamport time of the view
045: *
046: * @return the lamport time timestamp
047: */
048: public long getId() {
049: return id;
050: }
051:
052: /**
053: * returns the address of the member that issued this view
054: *
055: * @return the Address of the the issuer
056: */
057: public Address getCoordAddress() {
058: return coord_addr;
059: }
060:
061: public String toString() {
062: return "[" + coord_addr + '|' + id + ']';
063: }
064:
065: /**
066: * Cloneable interface
067: * Returns a new ViewID object containing the same address and lamport timestamp as this view
068: */
069: public Object clone() {
070: return new ViewId(coord_addr, id);
071: }
072:
073: /**
074: * Old Copy method, deprecated because it is substituted by clone()
075: */
076: public ViewId copy() {
077: return (ViewId) clone();
078: }
079:
080: /**
081: * Establishes an order between 2 ViewIds. First compare on id. <em>Compare on coord_addr
082: * only if necessary</em> (i.e. ids are equal) !
083: *
084: * @return 0 for equality, value less than 0 if smaller, greater than 0 if greater.
085: */
086: public int compareTo(Object other) {
087: if (other == null)
088: return 1; //+++ Maybe necessary to throw an exception
089:
090: if (!(other instanceof ViewId)) {
091: throw new ClassCastException(
092: "ViewId.compareTo(): view id is not comparable with different Objects");
093: }
094: return id > ((ViewId) other).id ? 1
095: : id < ((ViewId) other).id ? -1 : 0;
096: }
097:
098: /**
099: * Old Compare
100: */
101: public int compare(Object o) {
102: return compareTo(o);
103: }
104:
105: public boolean equals(Object other_view) {
106: return compareTo(other_view) == 0;
107: }
108:
109: public int hashCode() {
110: return (int) id;
111: }
112:
113: public void writeExternal(ObjectOutput out) throws IOException {
114: out.writeObject(coord_addr);
115: out.writeLong(id);
116: }
117:
118: public void readExternal(ObjectInput in) throws IOException,
119: ClassNotFoundException {
120: coord_addr = (Address) in.readObject();
121: id = in.readLong();
122: }
123:
124: public void writeTo(DataOutputStream out) throws IOException {
125: Util.writeAddress(coord_addr, out);
126: out.writeLong(id);
127: }
128:
129: public void readFrom(DataInputStream in) throws IOException,
130: IllegalAccessException, InstantiationException {
131: coord_addr = Util.readAddress(in);
132: id = in.readLong();
133: }
134:
135: public int serializedSize() {
136: int retval = Global.LONG_SIZE; // for the id
137: retval += Util.size(coord_addr);
138: return retval;
139: }
140:
141: }
|