KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > managedobject > MapManagedObjectState


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.objectserver.managedobject;
5
6 import com.tc.object.ObjectID;
7 import com.tc.object.SerializationUtil;
8 import com.tc.object.dna.api.DNACursor;
9 import com.tc.object.dna.api.DNAWriter;
10 import com.tc.object.dna.api.LogicalAction;
11 import com.tc.objectserver.mgmt.FacadeUtil;
12 import com.tc.objectserver.mgmt.LogicalManagedObjectFacade;
13 import com.tc.objectserver.mgmt.ManagedObjectFacade;
14 import com.tc.objectserver.mgmt.MapEntryFacade;
15 import com.tc.objectserver.mgmt.MapEntryFacadeImpl;
16 import com.tc.text.PrettyPrintable;
17 import com.tc.text.PrettyPrinter;
18
19 import java.io.IOException JavaDoc;
20 import java.io.ObjectInput JavaDoc;
21 import java.io.ObjectOutput JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.Map.Entry;
26
27 /**
28  * state for maps
29  */

30 public class MapManagedObjectState extends LogicalManagedObjectState implements PrettyPrintable {
31   protected Map JavaDoc references;
32
33   protected MapManagedObjectState(long classID, Map JavaDoc map) {
34     super(classID);
35     references = map;
36   }
37
38   protected MapManagedObjectState(ObjectInput JavaDoc in) throws IOException JavaDoc {
39     super(in);
40   }
41
42   public void apply(ObjectID objectID, DNACursor cursor, BackReferences includeIDs) throws IOException JavaDoc {
43     while (cursor.next()) {
44       LogicalAction action = cursor.getLogicalAction();
45       int method = action.getMethod();
46       Object JavaDoc[] params = action.getParameters();
47       applyMethod(objectID, includeIDs, method, params);
48     }
49   }
50
51   protected void applyMethod(ObjectID objectID, BackReferences includeIDs, int method, Object JavaDoc[] params) {
52     switch (method) {
53       case SerializationUtil.PUT:
54
55         mapPreProcess(params);
56         Object JavaDoc key = getKey(params);
57         Object JavaDoc value = getValue(params);
58         references.put(key, value);
59         if (key instanceof ObjectID) {
60           ObjectID v = (ObjectID) key;
61           getListener().changed(objectID, null, v);
62           addBackReferenceForKey(includeIDs, v, objectID);
63         }
64         if (value instanceof ObjectID) {
65           ObjectID v = (ObjectID) value;
66           getListener().changed(objectID, null, v);
67           addBackReferenceForValue(includeIDs, v, objectID);
68         }
69         break;
70       case SerializationUtil.REMOVE:
71         references.remove(params[0]);
72         break;
73       case SerializationUtil.CLEAR:
74         references.clear();
75         break;
76       default:
77         throw new AssertionError JavaDoc("Invalid action:" + method);
78     }
79
80   }
81
82   protected void addBackReferenceForKey(BackReferences includeIDs, ObjectID key, ObjectID map) {
83     includeIDs.addBackReference(key, map);
84   }
85   
86   protected void addBackReferenceForValue(BackReferences includeIDs, ObjectID value, ObjectID map) {
87     includeIDs.addBackReference(value, map);
88   }
89
90
91   private Object JavaDoc getKey(Object JavaDoc[] params) {
92     // Hack hack big hack for trove maps which replace the key on set as opposed to HashMaps which do not.
93
return params.length == 3 ? params[1] : params[0];
94   }
95
96   private Object JavaDoc getValue(Object JavaDoc[] params) {
97     // Hack hack big hack for trove maps which replace the key on set as opposed to HashMaps which do not.
98
return params.length == 3 ? params[2] : params[1];
99   }
100
101   private void mapPreProcess(Object JavaDoc[] params) {
102     // Hack hack big hack for trove maps which replace the key on set as opposed to HashMaps which do not.
103
if (params.length == 3) {
104       references.remove(params[0]);
105     }
106   }
107
108   public void dehydrate(ObjectID objectID, DNAWriter writer) {
109     for (Iterator JavaDoc i = references.entrySet().iterator(); i.hasNext();) {
110       Entry entry = (Entry) i.next();
111       Object JavaDoc key = entry.getKey();
112       Object JavaDoc value = entry.getValue();
113       writer.addLogicalAction(SerializationUtil.PUT, new Object JavaDoc[] { key, value });
114     }
115   }
116
117   protected void addAllObjectReferencesTo(Set JavaDoc refs) {
118     addAllObjectReferencesFromIteratorTo(this.references.keySet().iterator(), refs);
119     addAllObjectReferencesFromIteratorTo(this.references.values().iterator(), refs);
120   }
121   
122   public PrettyPrinter prettyPrint(PrettyPrinter out) {
123     PrettyPrinter rv = out;
124     out = out.println("MapManagedObjectState").duplicateAndIndent();
125     out.indent().println("references: " + references);
126     return rv;
127   }
128
129   public ManagedObjectFacade createFacade(ObjectID objectID, String JavaDoc className, int limit) {
130     final int size = references.size();
131
132     if (limit < 0) {
133       limit = size;
134     } else {
135       limit = Math.min(limit, size);
136     }
137
138     MapEntryFacade[] data = new MapEntryFacade[limit];
139
140     int index = 0;
141
142     for (Iterator JavaDoc i = references.entrySet().iterator(); i.hasNext() && index < limit; index++) {
143       Entry entry = (Entry) i.next();
144       Object JavaDoc key = FacadeUtil.processValue(entry.getKey());
145       Object JavaDoc value = FacadeUtil.processValue(entry.getValue());
146       data[index] = new MapEntryFacadeImpl(key, value);
147     }
148
149     return LogicalManagedObjectFacade.createMapInstance(objectID, className, data, size);
150   }
151
152   protected void basicWriteTo(ObjectOutput JavaDoc out) throws IOException JavaDoc {
153     // CollectionsPersistor will save retrieve data in references map.
154
if (false) throw new IOException JavaDoc();
155   }
156
157   public void setMap(Map JavaDoc map) {
158     if (this.references != null) { throw new AssertionError JavaDoc("The references map is already set ! " + references); }
159     this.references = map;
160   }
161
162   public Map JavaDoc getMap() {
163     return references;
164   }
165
166   // CollectionsPersistor will save retrieve data in references map.
167
static MapManagedObjectState readFrom(ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
168     if (false) {
169       // This is added to make the compiler happy. For some reason if I have readFrom() method throw
170
// ClassNotFoundException in LinkedHashMapManagedObjectState, it shows as an error !!
171
throw new ClassNotFoundException JavaDoc();
172     }
173     return new MapManagedObjectState(in);
174   }
175
176   public byte getType() {
177     return MAP_TYPE;
178   }
179
180   protected boolean basicEquals(LogicalManagedObjectState o) {
181     MapManagedObjectState mmo = (MapManagedObjectState) o;
182     return references.equals(mmo.references);
183   }
184 }
Popular Tags