1 11 package org.eclipse.core.internal.watson; 12 13 import java.io.*; 14 import java.util.*; 15 import org.eclipse.core.internal.dtree.*; 16 import org.eclipse.core.runtime.*; 17 18 33 public class ElementTreeWriter { 34 37 public static final int CURRENT_FORMAT = 1; 38 39 42 public static final int D_INFINITE = DataTreeWriter.D_INFINITE; 43 44 47 protected DataTreeWriter dataTreeWriter; 48 49 53 public ElementTreeWriter(final IElementInfoFlattener flattener) { 54 55 56 IDataFlattener f = new IDataFlattener() { 57 public void writeData(IPath path, Object data, DataOutput output) throws IOException { 58 if (!Path.ROOT.equals(path)) { 61 flattener.writeElement(path, data, output); 62 } 63 } 64 65 public Object readData(IPath path, DataInput input) { 66 return null; 67 } 68 }; 69 dataTreeWriter = new DataTreeWriter(f); 70 } 71 72 81 protected ElementTree[] sortTrees(ElementTree[] trees, DataOutput output) throws IOException { 82 83 84 int numTrees = trees.length; 85 ElementTree[] sorted = new ElementTree[numTrees]; 86 int[] order = new int[numTrees]; 87 88 89 HashMap table = new HashMap(numTrees * 2 + 1); 90 for (int i = 0; i < trees.length; i++) { 91 List indices = (List) table.get(trees[i]); 92 if (indices == null) { 93 indices = new ArrayList(); 94 table.put(trees[i], indices); 95 } 96 indices.add(new Integer (i)); 97 } 98 99 100 ElementTree oldest = trees[ElementTree.findOldest(trees)]; 101 102 106 int i = numTrees - 1; 107 while (i >= 0) { 108 109 List indices = (List) table.remove(oldest); 110 for (Enumeration e = Collections.enumeration(indices); e.hasMoreElements();) { 111 Integer next = (Integer ) e.nextElement(); 112 sorted[i] = oldest; 113 order[i] = next.intValue(); 114 i--; 115 } 116 if (i >= 0) { 117 118 ElementTree parent = oldest.getParent(); 119 while (table.get(parent) == null) { 120 parent = parent.getParent(); 121 } 122 oldest = parent; 123 } 124 } 125 126 127 for (i = 0; i < numTrees; i++) { 128 writeNumber(order[i], output); 129 } 130 return sorted; 131 } 132 133 143 public void writeDelta(ElementTree olderTree, ElementTree newerTree, IPath path, int depth, final DataOutput output, IElementComparator comparator) throws IOException { 144 145 146 writeNumber(CURRENT_FORMAT, output); 147 148 153 DeltaDataTree completeTree = newerTree.getDataTree(); 154 DeltaDataTree derivedTree = olderTree.getDataTree(); 155 DeltaDataTree deltaToWrite = null; 156 157 deltaToWrite = completeTree.forwardDeltaWith(derivedTree, comparator); 158 159 Assert.isTrue(deltaToWrite.isImmutable()); 160 dataTreeWriter.writeTree(deltaToWrite, path, depth, output); 161 } 162 163 174 public void writeDeltaChain(ElementTree[] trees, IPath path, int depth, DataOutput output, IElementComparator comparator) throws IOException { 175 176 writeNumber(CURRENT_FORMAT, output); 177 178 179 int treeCount = trees.length; 180 writeNumber(treeCount, output); 181 182 if (treeCount <= 0) { 183 return; 184 } 185 186 190 ElementTree[] sortedTrees = sortTrees(trees, output); 191 192 193 writeTree(sortedTrees[0], path, depth, output); 194 195 196 for (int i = 1; i < treeCount; i++) { 197 writeDelta(sortedTrees[i], sortedTrees[i - 1], path, depth, output, comparator); 198 } 199 } 200 201 206 protected void writeNumber(int number, DataOutput output) throws IOException { 207 if (number >= 0 && number < 0xff) { 208 output.writeByte(number); 209 } else { 210 output.writeByte(0xff); 211 output.writeInt(number); 212 } 213 } 214 215 227 public void writeTree(ElementTree tree, IPath path, int depth, final DataOutput output) throws IOException { 228 229 230 writeNumber(CURRENT_FORMAT, output); 231 232 233 DeltaDataTree subtree = new DeltaDataTree(tree.getDataTree().copyCompleteSubtree(Path.ROOT)); 234 235 dataTreeWriter.writeTree(subtree, path, depth, output); 236 } 237 } 238 | Popular Tags |