001: // $Id: TransportedVectorTime.java,v 1.5 2005/08/08 12:45:45 belaban Exp $
002:
003: package org.jgroups.protocols;
004:
005: import org.jgroups.Message;
006:
007: import java.io.Serializable;
008:
009: /**
010: * Lighweight representation of the VectorTime clock suitable for network transport
011: *
012: * @author Vladimir Blagojevic vladimir@cs.yorku.ca
013: * @version $Revision: 1.5 $
014: */
015: public class TransportedVectorTime implements Serializable {
016: /**
017: * index of the sender
018: */
019: int senderPosition;
020:
021: /**
022: * values represented as an array
023: */
024: int[] values;
025:
026: /**
027: * Message associated with this vector clock
028: */
029: private transient Message m;
030: private static final long serialVersionUID = 5857647322589533545L;
031:
032: /**
033: *
034: */
035: public TransportedVectorTime() {
036: }
037:
038: /**
039: * Constructs TransportedVectorTime with sender index and vector values
040: *
041: * @param senderIndex index of the sender of the message
042: * @param values vector values
043: */
044: public TransportedVectorTime(int senderIndex, int[] values) {
045: this .values = values;
046: this .senderPosition = senderIndex;
047: }
048:
049: /**
050: * Returns sender index
051: * @return sender index position
052: */
053: public int getSenderIndex() {
054: return senderPosition;
055: }
056:
057: /**
058: * Returns vector values
059: * @return an array of vector values
060: */
061: public int[] getValues() {
062: return values;
063: }
064:
065: /**
066: * Returns size of this vector timestamp i.e number of process group members
067: * @return vector timestamp size
068: */
069: public int size() {
070: return values.length;
071: }
072:
073: /**
074: * Sets a message associated with this vector timestamp
075: * @param owner Message that is associated with this vector timestamp
076: */
077: public void setAssociatedMessage(Message owner) {
078: m = owner;
079: }
080:
081: /**
082: * Returns a message associated with this vector timestamp.
083: * @return Message associated with this vector timestamp
084: */
085: public Message getAssociatedMessage() {
086: return m;
087: }
088:
089: /**
090: *<p>
091: *Checks if this TransportedVectorTime is less than or equal to the the specified TransportedVectorTime.
092: *The check is done as follows:
093: *</p>
094: * <p>
095: * VT1<=VT2 iff for every i:1..k VT1[i]<=VT2[i]
096: * </p>
097: * @param other TransportedVectorTimebeing compared with this.
098: * @return true if this TransportedVectorTimeis less than or equal from
099: * other, false othwerwise
100: */
101: public boolean lessThanOrEqual(TransportedVectorTime other) {
102: int[] b = other.getValues();
103: int[] a = values;
104: for (int k = 0; k < a.length; k++) {
105: if (a[k] <= b[k])
106: return true;
107: else
108: return false;
109: }
110: return true;
111: }
112:
113: /**
114: * <p>
115: * Checks if this TransportedVectorTimeis equal to the specified TransportedVectorTime.
116: * The check is done as follows:
117: * </p>
118: * <p>
119: * VT1==VT2 iff for every i:1..k VT1[i]==VT2[i]
120: * @param other TransportedVectorTimebeing compared with this.
121: * @return true if the equation given above is true, false otherwise
122: */
123: public boolean equals(Object other) {
124: int a[] = getValues();
125: int b[] = ((TransportedVectorTime) other).getValues();
126:
127: for (int i = 0; i < a.length; i++)
128: if (a[i] != b[i])
129: return false;
130:
131: return true;
132: }
133:
134: /**
135: * Returns String representation of this vector timestamp
136: * @return String representing this vetor timestamp
137: */
138: public String toString() {
139: String classType = "TransportedVectorTime[";
140: int bufferLength = classType.length() + values.length * 2 + 1;
141: StringBuffer buf = new StringBuffer(bufferLength);
142: buf.append(classType);
143: for (int i = 0; i < values.length - 1; i++) {
144: buf.append(values[i]).append(',');
145: }
146: buf.append(values[values.length - 1]);
147: buf.append(']');
148: return buf.toString();
149: }
150: }
|