KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > dream > protocol > causality > Update


1 /**
2  * Dream
3  * Copyright (C) 2003-2004 INRIA Rhone-Alpes
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact : dream@objectweb.org
20  *
21  * Initial developer(s): Matthieu Leclercq
22  * Contributor(s):
23  */

24
25 package org.objectweb.dream.protocol.causality;
26
27 import java.io.Externalizable JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectInput JavaDoc;
30 import java.io.ObjectOutput JavaDoc;
31
32 /**
33  * Matrix update elements
34  */

35 public class Update implements Externalizable JavaDoc
36 {
37
38   /** Matrix clock line element. */
39   private transient short l;
40
41   /** Matrix clock column element. */
42   private transient short c;
43
44   /** Matrix clock value. */
45   private transient int stamp;
46
47   /** pointer on next element of update list. */
48   private transient Update next;
49
50   /**
51    * Default contructor. Defines for serialization.
52    */

53   public Update()
54   {
55   }
56
57   /**
58    * Creates an element in a new empty list.
59    *
60    * @param l Matrix line.
61    * @param c Matrix column.
62    * @param stamp Element stamp.
63    */

64   public Update(short l, short c, int stamp)
65   {
66     super();
67     this.l = l;
68     this.c = c;
69     this.stamp = stamp;
70   }
71
72   /**
73    * Creates an element and links it after the head.
74    *
75    * @param l Matrix line.
76    * @param c Matrix column.
77    * @param s Element stamp.
78    * @param list The head list element.
79    */

80   public Update(short l, short c, int s, Update list)
81   {
82     this.l = l;
83     this.c = c;
84     this.stamp = s;
85     if (list != null)
86     {
87       this.next = list.next;
88       list.next = this;
89     }
90     else
91     {
92       this.next = null;
93     }
94   }
95
96   /**
97    * @return Matrix clock line element.
98    */

99   public short getLine()
100   {
101     return l;
102   }
103
104   /**
105    * @return Matrix clock column element.
106    */

107   public short getColumn()
108   {
109     return c;
110   }
111
112   /**
113    * @return Matrix clock value.
114    */

115   public int getStamp()
116   {
117     return stamp;
118   }
119
120   /**
121    * @return pointer on next element of update list.
122    */

123   public Update getNext()
124   {
125     return next;
126   }
127
128   /**
129    * @see java.lang.Object#toString()
130    */

131   public String JavaDoc toString()
132   {
133     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
134     sb.append('(');
135     Update u = this;
136     while (u != null)
137     {
138       u.toStringBuffer(sb);
139       u = u.next;
140     }
141     sb.append(')');
142     return sb.toString();
143   }
144
145   private StringBuffer JavaDoc toStringBuffer(StringBuffer JavaDoc sb)
146   {
147     sb.append('(');
148     sb.append(l).append(", ");
149     sb.append(c).append(", ");
150     sb.append(stamp).append(')');
151     return sb;
152   }
153
154   /**
155    * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
156    */

157   public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc,
158       ClassNotFoundException JavaDoc
159   {
160     this.l = in.readShort();
161     this.c = in.readShort();
162     this.stamp = in.readInt();
163
164     short l;
165     while ((l = in.readShort()) != -1)
166     {
167       alloc(l, in.readShort(), in.readInt(), this);
168     }
169   }
170
171   /**
172    * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
173    */

174   public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
175   {
176     Update update = this;
177     while (update != null)
178     {
179       out.writeShort(update.l);
180       out.writeShort(update.c);
181       out.writeInt(update.stamp);
182       Update nextUpdate = update.next;
183       unalloc(update);
184       update = nextUpdate;
185     }
186     out.writeShort(-1);
187   }
188
189   /**
190    * @see java.io.Externalizable#readExternal(ObjectInput)
191    */

192   public static Update readUpdate(ObjectInput JavaDoc in) throws IOException JavaDoc,
193       ClassNotFoundException JavaDoc
194   {
195     Update u = alloc((short) 0, (short) 0, 0, null);
196     u.readExternal(in);
197     return u;
198   }
199
200   private static Update[] pool = new Update[30];
201
202   private static int elementCount;
203
204   static synchronized Update alloc(short l, short c, int s, Update list)
205   {
206     if (elementCount == 0)
207     {
208       return new Update(l, c, s, list);
209     }
210     elementCount -= 1;
211     Update update = pool[elementCount];
212
213     update.l = l;
214     update.c = c;
215     update.stamp = s;
216     if (list != null)
217     {
218       update.next = list.next;
219       list.next = update;
220     }
221     else
222     {
223       update.next = null;
224     }
225
226     pool[elementCount] = null;
227     return update;
228
229   }
230
231   static synchronized void unalloc(Update update)
232   {
233     if (elementCount == pool.length)
234     {
235       return;
236     }
237     pool[elementCount] = update;
238     update.next = null;
239     elementCount += 1;
240   }
241 }
Popular Tags