1 4 package com.tc.object.applicator; 5 6 import com.tc.object.ClientObjectManager; 7 import com.tc.object.ObjectID; 8 import com.tc.object.TCObject; 9 import com.tc.object.TraversedReferences; 10 import com.tc.object.dna.api.DNA; 11 import com.tc.object.dna.api.DNACursor; 12 import com.tc.object.dna.api.DNAWriter; 13 import com.tc.object.dna.api.LogicalAction; 14 import com.tc.object.dna.api.PhysicalAction; 15 import com.tc.object.dna.impl.DNAEncoding; 16 import com.tc.util.Assert; 17 import com.tc.util.FieldUtils; 18 19 import java.io.IOException ; 20 import java.lang.reflect.Field ; 21 import java.util.Collection ; 22 import java.util.Comparator ; 23 import java.util.Iterator ; 24 import java.util.TreeMap ; 25 import java.util.TreeSet ; 26 27 30 public class TreeSetApplicator extends HashSetApplicator { 31 private static final String COMPARATOR_FIELDNAME = TreeMapApplicator.COMPARATOR_FIELDNAME; 32 private static final Field COMPARATOR_FIELD; 33 private static final Field TREE_MAP_FIELD; 34 35 static { 36 try { 37 COMPARATOR_FIELD = TreeMap .class.getDeclaredField("comparator"); 38 COMPARATOR_FIELD.setAccessible(true); 39 40 TREE_MAP_FIELD = TreeSet .class.getDeclaredField("m"); 41 TREE_MAP_FIELD.setAccessible(true); 42 } catch (Exception e) { 43 throw new RuntimeException (e); 44 } 45 } 46 47 public TreeSetApplicator(DNAEncoding encoding) { 48 super(encoding); 49 } 50 51 public void hydrate(ClientObjectManager objectManager, TCObject tcObject, DNA dna, Object pojo) throws IOException , 52 ClassNotFoundException { 53 TreeSet set = (TreeSet ) pojo; 54 DNACursor cursor = dna.getCursor(); 55 56 if (!cursor.next(encoding)) { return; } 57 58 Object action = cursor.getAction(); 59 if (action instanceof PhysicalAction) { 60 PhysicalAction pa = (PhysicalAction) action; 61 Assert.assertEquals(COMPARATOR_FIELDNAME, pa.getFieldName()); 62 setComparator(set, objectManager.lookupObject((ObjectID) pa.getObject())); 63 } else { 64 LogicalAction la = (LogicalAction) action; 65 int method = la.getMethod(); 66 Object [] params = la.getParameters(); 67 super.apply(objectManager, set, method, params); 68 } 69 70 while (cursor.next(encoding)) { 71 LogicalAction la = cursor.getLogicalAction(); 72 int method = la.getMethod(); 73 Object [] params = la.getParameters(); 74 super.apply(objectManager, set, method, params); 75 } 76 } 77 78 private void setComparator(Object treeSet, Object comparator) { 79 try { 80 Object treeMap = TREE_MAP_FIELD.get(treeSet); 81 FieldUtils.tcSet(treeMap, comparator, COMPARATOR_FIELD); 82 } catch (Exception e) { 83 throw new RuntimeException (e); 84 } 85 } 86 87 public void dehydrate(ClientObjectManager objectManager, TCObject tcObject, DNAWriter writer, Object pojo) { 88 TreeSet set = (TreeSet ) pojo; 89 Comparator cmp = set.comparator(); 90 if (cmp != null) { 91 Object cmpObj = getDehydratableObject(cmp, objectManager); 92 if (cmpObj != null) { 93 writer.addPhysicalAction(COMPARATOR_FIELDNAME, cmpObj); 94 } 95 } 96 97 super.dehydrate(objectManager, tcObject, writer, pojo); 98 } 99 100 public TraversedReferences getPortableObjects(Object pojo, TraversedReferences addTo) { 101 TreeSet set = (TreeSet ) pojo; 102 filterPortableObject(set.comparator(), addTo); 103 filterPortableObjects(set, addTo); 104 return addTo; 105 } 106 107 private void filterPortableObjects(Collection objects, TraversedReferences addTo) { 108 for (Iterator i = objects.iterator(); i.hasNext();) { 109 Object value = i.next(); 110 filterPortableObject(value, addTo); 111 } 112 } 113 114 private void filterPortableObject(Object value, TraversedReferences addTo) { 115 if (value != null && isPortableReference(value.getClass())) { 116 addTo.addAnonymousReference(value); 117 } 118 } 119 } 120 | Popular Tags |