KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > dna > impl > ObjectStringSerializer


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.object.dna.impl;
5
6 import com.tc.io.TCByteBufferInputStream;
7 import com.tc.io.TCByteBufferOutput;
8 import com.tc.io.TCByteBufferOutputStream;
9 import com.tc.io.TCDataOutput;
10 import com.tc.io.TCSerializable;
11 import com.tc.util.Assert;
12
13 import gnu.trove.TIntObjectHashMap;
14 import gnu.trove.TLongObjectHashMap;
15 import gnu.trove.TObjectIntHashMap;
16 import gnu.trove.TObjectIntProcedure;
17
18 import java.io.IOException JavaDoc;
19
20 public class ObjectStringSerializer implements TCSerializable {
21
22   private final TObjectIntHashMap stringToID = new TObjectIntHashMap();
23   private final TIntObjectHashMap idToString = new TIntObjectHashMap();
24   private final TLongObjectHashMap fields = new TLongObjectHashMap();
25
26   private static class SerializeProcedure implements TObjectIntProcedure {
27     private final TCDataOutput out;
28
29     public SerializeProcedure(TCDataOutput out) {
30       this.out = out;
31     }
32
33     public boolean execute(Object JavaDoc key, int value) {
34       out.writeString((String JavaDoc) key);
35       out.writeInt(value);
36       return true;
37     }
38   }
39
40   public synchronized void serializeTo(TCByteBufferOutput serialOutput) {
41     serialOutput.writeInt(stringToID.size());
42     stringToID.forEachEntry(new SerializeProcedure(serialOutput));
43   }
44
45   public synchronized Object JavaDoc deserializeFrom(TCByteBufferInputStream serialInput) throws IOException JavaDoc {
46     int size = serialInput.readInt();
47     for (int i = 0; i < size; i++) {
48       addStringAndID(serialInput.readString(), serialInput.readInt());
49     }
50     return this;
51   }
52
53   public synchronized void writeString(TCByteBufferOutputStream out, String JavaDoc string) {
54     int sid = -1;
55     if (stringToID.containsKey(string)) {
56       sid = idForString(string);
57     } else {
58       sid = createID(string);
59     }
60     out.writeInt(sid);
61   }
62
63   public synchronized void writeFieldName(TCByteBufferOutputStream out, String JavaDoc fieldName) {
64     int lastDot = fieldName.lastIndexOf('.');
65     String JavaDoc cn = fieldName.substring(0, lastDot);
66     String JavaDoc fn = fieldName.substring(lastDot + 1, fieldName.length());
67
68     writeString(out, cn);
69     writeString(out, fn);
70   }
71
72   public synchronized String JavaDoc readString(TCByteBufferInputStream in) throws IOException JavaDoc {
73     int id = in.readInt();
74
75     String JavaDoc string = stringForID(id);
76     if (string == null) { throw new AssertionError JavaDoc("cid:" + id + " map:" + stringToID); }
77
78     return string;
79   }
80
81   public synchronized String JavaDoc readFieldName(TCByteBufferInputStream in) throws IOException JavaDoc {
82     final int classId;
83     classId = in.readInt();
84
85     final int fieldId;
86     fieldId = in.readInt();
87
88     long key = ((long) classId << 32) + fieldId;
89     String JavaDoc rv = (String JavaDoc) fields.get(key);
90     if (rv == null) {
91       String JavaDoc cn = stringForID(classId);
92       Assert.eval(cn != null);
93       String JavaDoc fn = stringForID(fieldId);
94       Assert.eval(fn != null);
95       StringBuffer JavaDoc buf = new StringBuffer JavaDoc(cn.length() + fn.length() + 1); // +1 for '.'
96
buf.append(cn).append('.').append(fn);
97       rv = buf.toString().intern();
98       fields.put(key, rv);
99     }
100
101     return rv;
102   }
103
104   private void addStringAndID(String JavaDoc name, int id) {
105     name = name.intern();
106     stringToID.put(name, id);
107     idToString.put(id, name);
108   }
109
110   private String JavaDoc stringForID(int id) {
111     return (String JavaDoc) idToString.get(id);
112   }
113
114   private int idForString(String JavaDoc string) {
115     return stringToID.get(string);
116   }
117
118   private int createID(String JavaDoc string) {
119     Assert.assertNotNull(string);
120     int newID = stringToID.size() + 1;
121     stringToID.put(string, newID);
122     idToString.put(newID, string);
123     return newID;
124   }
125 }
126
Popular Tags