KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > TransportedVectorTime


1 // $Id: TransportedVectorTime.java,v 1.3 2004/12/01 10:17:50 belaban Exp $
2

3 package org.jgroups.protocols;
4
5
6 import org.jgroups.Message;
7
8 import java.io.Serializable JavaDoc;
9
10
11
12 /**
13  * Lighweight representation of the VectorTime clock suitable for network transport
14  *
15  * @author Vladimir Blagojevic vladimir@cs.yorku.ca
16  * @version $Revision: 1.3 $
17  */

18 public class TransportedVectorTime implements Serializable JavaDoc
19 {
20    /**
21     * index of the sender
22     */

23    int senderPosition;
24
25    /**
26     * values represented as an array
27     */

28    int[] values;
29
30    /**
31     * Message associated with this vector clock
32     */

33    private transient Message m;
34
35    /**
36     *
37     */

38    public TransportedVectorTime()
39    {
40    }
41
42    /**
43     * Constructs TransportedVectorTime with sender index and vector values
44     *
45     * @param senderIndex index of the sender of the message
46     * @param values vector values
47     */

48    public TransportedVectorTime(int senderIndex, int[] values)
49    {
50       this.values = values;
51       this.senderPosition = senderIndex;
52    }
53
54    /**
55     * Returns sender index
56     * @return sender index position
57     */

58    public int getSenderIndex()
59    {
60       return senderPosition;
61    }
62
63    /**
64     * Returns vector values
65     * @return an array of vector values
66     */

67    public int[] getValues()
68    {
69       return values;
70    }
71
72    /**
73     * Returns size of this vector timestamp i.e number of process group members
74     * @return vector timestamp size
75     */

76    public int size()
77    {
78       return values.length;
79    }
80
81    /**
82     * Sets a message associated with this vector timestamp
83     * @param owner Message that is associated with this vector timestamp
84     */

85    public void setAssociatedMessage(Message owner)
86    {
87       m = owner;
88    }
89
90    /**
91     *Returns a message associated with this vector timestamp
92     * @returnMessage associated with this vector timestamp
93     */

94    public Message getAssociatedMessage()
95    {
96       return m;
97    }
98
99
100    /**
101     *<p>
102     *Checks if this TransportedVectorTime is less than equal from the given
103     *other TransportedVectorTimeas follows:
104     *</p>
105     * <p>
106     * VT1<=VT2 iff for every i:1..k VT1[i]<=VT2[i]
107     * </p>
108     * @param other TransportedVectorTimebeing compared with this.
109     * @return true if this TransportedVectorTimeis less than or equal from
110     * other, false othwerwise
111     */

112    public boolean lessThanOrEqual(TransportedVectorTime other)
113    {
114       int[] b = other.getValues();
115       int[] a = values;
116       for (int k = 0; k < a.length; k++)
117       {
118
119          if (a[k] <= b[k])
120             continue;
121          else
122             return false;
123       }
124       return true;
125    }
126
127    /**
128     * <p>
129     * Checks if this TransportedVectorTimeis equal to given TransportedVectorTime
130     * as follows:
131     * </p>
132     * <p>
133     * VT1==VT2 iff for every i:1..k VT1[i]==VT2[i]
134     * @param other TransportedVectorTimebeing compared with this.
135     * @return true if the above given equation is true, false otherwise
136     */

137    public boolean equals(TransportedVectorTime other)
138    {
139       int a [] = getValues();
140       int b [] = other.getValues();
141
142       for (int i = 0; i < a.length; i++)
143          if (a[i] != b[i]) return false;
144
145       return true;
146    }
147
148    /**
149     * Returns String representation of this vector timestamp
150     * @return String representing this vetor timestamp
151     */

152    public String JavaDoc toString()
153    {
154       String JavaDoc classType = "TransportedVectorTime[";
155       int bufferLength = classType.length() + values.length * 2 + 1;
156       StringBuffer JavaDoc buf = new StringBuffer JavaDoc(bufferLength);
157       buf.append(classType);
158       for (int i = 0; i < values.length - 1; i++)
159       {
160          buf.append(values[i]).append(',');
161       }
162       buf.append(values[values.length - 1]);
163       buf.append(']');
164       return buf.toString();
165    }
166 }
167
Popular Tags