KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > Modification


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache;
8
9
10 import java.io.Externalizable JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.ObjectInput JavaDoc;
13 import java.io.ObjectOutput JavaDoc;
14 import java.util.Map JavaDoc;
15
16
17 /**
18  * Represents a modification in the cache. Contains the nature of the modification
19  * (e.g. PUT, REMOVE), the fqn of the node, the new value and the previous value.
20  * A list of modifications will be sent to all nodes in a cluster when a transaction
21  * has been committed (PREPARE phase). A Modification is also used to roll back changes,
22  * e.g. since we know the previous value, we can reconstruct the previous state by
23  * applying the changes in a modification listin reverse order.
24  *
25  * @author <a HREF="mailto:bela@jboss.org">Bela Ban</a> Apr 12, 2003
26  * @version $Revision: 1.7 $
27  */

28 public class Modification implements Externalizable JavaDoc
29 {
30
31    private static final long serialVersionUID = 7463314130283897197L;
32
33    public enum ModificationType
34    {
35       PUT_KEY_VALUE,
36       PUT_DATA,
37       PUT_DATA_ERASE,
38       REMOVE_NODE,
39       REMOVE_KEY_VALUE,
40       REMOVE_DATA,
41       MOVE,
42       UNKNOWN
43    }
44
45    private ModificationType type = ModificationType.UNKNOWN;
46    private Fqn fqn = null;
47    private Fqn fqn2 = null;
48    private Object JavaDoc key = null;
49    private Object JavaDoc value = null;
50    private Object JavaDoc old_value = null;
51    private Map JavaDoc data = null;
52
53    /**
54     * Constructs a new modification.
55     */

56    public Modification()
57    {
58    }
59
60    /**
61     * Constructs a new modification with details.
62     */

63    public Modification(ModificationType type, Fqn fqn, Object JavaDoc key, Object JavaDoc value)
64    {
65       this.type = type;
66       this.fqn = fqn;
67       this.key = key;
68       this.value = value;
69    }
70
71    /**
72     * Constructs a new modification with key.
73     */

74    public Modification(ModificationType type, Fqn fqn, Object JavaDoc key)
75    {
76       this.type = type;
77       this.fqn = fqn;
78       this.key = key;
79    }
80
81    /**
82     * Constructs a new modification with data map.
83     */

84    public Modification(ModificationType type, Fqn fqn, Map JavaDoc data)
85    {
86       this.type = type;
87       this.fqn = fqn;
88       this.data = data;
89    }
90
91    /**
92     * Constructs a new modification with fqn only.
93     */

94    public Modification(ModificationType type, Fqn fqn)
95    {
96       this.type = type;
97       this.fqn = fqn;
98    }
99
100    /**
101     * Constructs a new modification with fqn only.
102     */

103    public Modification(ModificationType type, Fqn fqn1, Fqn fqn2)
104    {
105       this.type = type;
106       this.fqn = fqn1;
107       this.fqn2 = fqn2;
108    }
109
110
111    /**
112     * Returns the type of modification.
113     */

114    public ModificationType getType()
115    {
116       return type;
117    }
118
119    /**
120     * Sets the type of modification.
121     */

122    public void setType(ModificationType type)
123    {
124       this.type = type;
125    }
126
127    /**
128     * Returns the modification fqn.
129     */

130    public Fqn getFqn()
131    {
132       return fqn;
133    }
134
135    /**
136     * Sets the modification fqn.
137     */

138    public void setFqn(Fqn fqn)
139    {
140       this.fqn = fqn;
141    }
142
143    public void setFqn2(Fqn fqn2)
144    {
145       this.fqn2 = fqn2;
146    }
147
148    public Fqn getFqn2()
149    {
150       return fqn2;
151    }
152
153    /**
154     * Returns the modification key.
155     */

156    public Object JavaDoc getKey()
157    {
158       return key;
159    }
160
161    /**
162     * Sets the modification key.
163     */

164    public void setKey(Object JavaDoc key)
165    {
166       this.key = key;
167    }
168
169    /**
170     * Returns the modification value.
171     */

172    public Object JavaDoc getValue()
173    {
174       return value;
175    }
176
177    /**
178     * Sets the modification value.
179     */

180    public void setValue(Object JavaDoc value)
181    {
182       this.value = value;
183    }
184
185    /**
186     * Returns the <i>post</i> modification old value.
187     */

188    public Object JavaDoc getOldValue()
189    {
190       return old_value;
191    }
192
193    /**
194     * Sets the <i>post</i> modification old value.
195     */

196    public void setOldValue(Object JavaDoc old_value)
197    {
198       this.old_value = old_value;
199    }
200
201    /**
202     * Returns the modification Map set.
203     */

204    public Map JavaDoc getData()
205    {
206       return data;
207    }
208
209    /**
210     * Sets the modification Map set.
211     */

212    public void setData(Map JavaDoc data)
213    {
214       this.data = data;
215    }
216
217    /**
218     * Writes data to an external stream.
219     */

220    public void writeExternal(ObjectOutput JavaDoc out) throws IOException JavaDoc
221    {
222       out.writeObject(type);
223
224       out.writeBoolean(fqn != null);
225       if (fqn != null)
226       {
227          fqn.writeExternal(out);
228       }
229
230       out.writeObject(key);
231       out.writeObject(value);
232       out.writeObject(old_value);
233
234       out.writeBoolean(data != null);
235       if (data != null)
236       {
237          out.writeObject(data);
238       }
239    }
240
241    /**
242     * Reads data from an external stream.
243     */

244    public void readExternal(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
245    {
246       type = (ModificationType) in.readObject();
247
248       if (in.readBoolean())
249       {
250          fqn = new Fqn();
251          fqn.readExternal(in);
252       }
253
254       key = in.readObject();
255       value = in.readObject();
256       old_value = in.readObject();
257
258       if (in.readBoolean())
259       {
260          data = (Map JavaDoc) in.readObject();
261       }
262    }
263
264    /**
265     * Returns debug information about this modification.
266     */

267    public String JavaDoc toString()
268    {
269       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
270       sb.append(type.toString()).append(": ").append(fqn);
271       if (key != null)
272       {
273          sb.append("\nkey=").append(key);
274       }
275       if (value != null)
276       {
277          sb.append("\nvalue=").append(value);
278       }
279       if (old_value != null)
280       {
281          sb.append("\nold_value=").append(old_value);
282       }
283       if (data != null)
284       {
285          sb.append("\ndata=").append(data);
286       }
287       return sb.toString();
288    }
289
290 }
291
Popular Tags