KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > io > serializer > DSOSerializerPolicy


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

5 package com.tc.io.serializer;
6
7 import com.tc.io.serializer.api.Serializer;
8 import com.tc.io.serializer.api.SerializerPolicy;
9 import com.tc.io.serializer.api.StringIndex;
10 import com.tc.io.serializer.impl.BooleanSerializer;
11 import com.tc.io.serializer.impl.ByteSerializer;
12 import com.tc.io.serializer.impl.CharacterSerializer;
13 import com.tc.io.serializer.impl.DoubleSerializer;
14 import com.tc.io.serializer.impl.FloatSerializer;
15 import com.tc.io.serializer.impl.IntegerSerializer;
16 import com.tc.io.serializer.impl.LongSerializer;
17 import com.tc.io.serializer.impl.ObjectIDSerializer;
18 import com.tc.io.serializer.impl.ObjectSerializer;
19 import com.tc.io.serializer.impl.ShortSerializer;
20 import com.tc.io.serializer.impl.StringSerializer;
21 import com.tc.io.serializer.impl.StringUTFSerializer;
22 import com.tc.object.ObjectID;
23
24 import gnu.trove.TIntObjectHashMap;
25
26 import java.io.DataOutput JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.ObjectInput JavaDoc;
29 import java.io.ObjectOutput JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Map JavaDoc;
32
33 public final class DSOSerializerPolicy implements SerializerPolicy {
34
35   private final Map class2SerializerDescriptor;
36   private final TIntObjectHashMap id2Serializer;
37   private final DSOSerializerPolicy.SDesc objectSerializer;
38
39   public DSOSerializerPolicy(StringIndex stringIndex) {
40     this(stringIndex, false);
41   }
42
43   public DSOSerializerPolicy() {
44     this(null, true);
45   }
46
47   private DSOSerializerPolicy(StringIndex stringIndex, boolean useUTFStrings) {
48     class2SerializerDescriptor = new HashMap();
49     id2Serializer = new TIntObjectHashMap();
50
51     addSerializerMapping(Object JavaDoc.class, new ObjectSerializer());
52     addSerializerMapping(ObjectID.class, new ObjectIDSerializer());
53     addSerializerMapping(Boolean JavaDoc.class, new BooleanSerializer());
54     addSerializerMapping(Byte JavaDoc.class, new ByteSerializer());
55     addSerializerMapping(Character JavaDoc.class, new CharacterSerializer());
56     if (useUTFStrings) {
57       addSerializerMapping(String JavaDoc.class, new StringUTFSerializer());
58     } else {
59       addSerializerMapping(String JavaDoc.class, new StringSerializer(stringIndex));
60     }
61     addSerializerMapping(Double JavaDoc.class, new DoubleSerializer());
62     addSerializerMapping(Float JavaDoc.class, new FloatSerializer());
63     addSerializerMapping(Integer JavaDoc.class, new IntegerSerializer());
64     addSerializerMapping(Long JavaDoc.class, new LongSerializer());
65     addSerializerMapping(Short JavaDoc.class, new ShortSerializer());
66
67     objectSerializer = (SDesc) class2SerializerDescriptor.get(Object JavaDoc.class);
68   }
69
70   private void addSerializerMapping(Class JavaDoc clazz, Serializer serializer) {
71     SDesc desc = new SDesc(clazz, serializer);
72     if (id2Serializer.containsKey(desc.getID())) throw new AssertionError JavaDoc("Duplicate desc ids: " + desc.getID());
73     id2Serializer.put(desc.getID(), desc.serializer);
74     class2SerializerDescriptor.put(desc.clazz, desc);
75   }
76
77   public Serializer getSerializerFor(Object JavaDoc o, ObjectOutput JavaDoc out) throws IOException JavaDoc {
78     DSOSerializerPolicy.SDesc desc = null;
79     if (o != null) {
80       desc = (DSOSerializerPolicy.SDesc) class2SerializerDescriptor.get(o.getClass());
81     }
82     desc = (desc == null) ? objectSerializer : desc;
83     return extractSerializer(desc, out);
84   }
85
86   private Serializer extractSerializer(DSOSerializerPolicy.SDesc desc, DataOutput JavaDoc out) throws IOException JavaDoc {
87     out.writeByte(desc.getID());
88     return desc.serializer;
89   }
90
91   public Serializer getSerializerFor(ObjectInput JavaDoc in) throws IOException JavaDoc {
92     Serializer rv = (Serializer) id2Serializer.get(in.readByte());
93     return rv == null ? objectSerializer.serializer : rv;
94   }
95
96   static final class SDesc {
97     Class JavaDoc clazz;
98     Serializer serializer;
99
100     SDesc(Class JavaDoc clazz, Serializer serializer) {
101       this.clazz = clazz;
102       this.serializer = serializer;
103     }
104
105     int getID() {
106       return serializer.getSerializerID();
107     }
108   }
109 }
Popular Tags