00001 00005 package edu.mit.csail.sdg.squander.serializer.special; 00006 00007 import java.util.HashMap; 00008 import java.util.Map; 00009 import java.util.Stack; 00010 00016 public class ObjSerFactory { 00017 00018 public static final ObjSerFactory factory = new ObjSerFactory(); 00019 00020 private Stack<IObjSer> serializers = new Stack<IObjSer>(); 00021 private Map<Class<?>, IObjSer> cache = new HashMap<Class<?>, IObjSer>(); 00022 00026 public IObjSer getSerForObj(Object obj) { 00027 return getSerForCls(obj.getClass()); 00028 } 00029 00033 public IObjSer getSerForCls(Class<?> clz) { 00034 IObjSer result = cache.get(clz); 00035 if (result == null) { 00036 for (IObjSer ser : serializers) { 00037 if (ser.accepts(clz)) { 00038 result = ser; 00039 break; 00040 } 00041 } 00042 cache.put(clz, result); 00043 } 00044 return result; 00045 } 00046 00047 private ObjSerFactory() { 00048 serializers.push(new ArraySer()); 00049 serializers.push(new ListSer()); 00050 serializers.push(new SetSer()); 00051 serializers.push(new MapSer()); 00052 serializers.push(new StringSer()); 00053 serializers.push(new DefaultObjSer()); 00054 } 00055 00056 }