1 2 12 package com.versant.core.common; 13 14 import com.versant.core.metadata.ClassMetaData; 15 import com.versant.core.metadata.ModelMetaData; 16 import com.versant.core.util.OIDObjectOutput; 17 import com.versant.core.util.OIDObjectInput; 18 19 import java.io.*; 20 import java.util.Comparator ; 21 import java.util.Arrays ; 22 import java.util.Iterator ; 23 24 32 public final class DeletePacket { 33 34 private static final int INITIAL_CAPACITY = 20; 35 private static final int GROW_FACTOR = 2; 36 37 private ModelMetaData jmd; 38 private boolean keepStates; 39 public OID[] oids = new OID[INITIAL_CAPACITY]; 40 public State[] states; 41 42 private int size; 43 44 47 public DeletePacket() { 48 } 49 50 public DeletePacket(ModelMetaData jmd) { 51 this.jmd =jmd; 52 keepStates = jmd.sendStateOnDelete; 53 if (keepStates) states = new State[INITIAL_CAPACITY]; 54 } 55 56 59 public void add(OID oid, State state) { 60 if (Debug.DEBUG) { 61 if (oid.isNew()) { 62 BindingSupportImpl.getInstance().internal("oid is new: " + oid); 63 } 64 if (oid.getAvailableClassMetaData() == null) { 66 BindingSupportImpl.getInstance().internal("oid is untyped: " + 67 oid); 68 } 69 } 70 resize(); 71 if (keepStates) states[size] = state; 72 oids[size++] = oid; 73 } 74 75 78 public int size() { 79 return size; 80 } 81 82 85 public boolean isEmpty() { 86 return size == 0; 87 } 88 89 92 private void resize() { 93 int length = oids.length; 94 if (length == size) { 95 int growTo = (length + 1) * GROW_FACTOR; 96 OID[] tmpOIDs = new OID[growTo]; 97 System.arraycopy(oids, 0, tmpOIDs, 0, length); 98 oids = tmpOIDs; 99 if (keepStates) { 100 State[] tmpStates = new State[growTo]; 101 System.arraycopy(states, 0, tmpStates, 0, length); 102 states = tmpStates; 103 } 104 } 105 } 106 107 110 public void clear() { 111 oids = new OID[INITIAL_CAPACITY]; 112 if (keepStates) { 113 states = new State[INITIAL_CAPACITY]; 114 } 115 size = 0; 116 } 117 118 public boolean isKeepStates() { 119 return keepStates; 120 } 121 122 125 public void sortOIDs(Comparator comp) { 126 if (keepStates) return; 127 states = null; 128 if (size <= 1) return; 129 if (size == 2) { 130 if (comp.compare(oids[0], oids[1]) > 0) { 131 OID t = oids[0]; 132 oids[0] = oids[1]; 133 oids[1] = t; 134 } 135 return; 136 } 137 Arrays.sort(oids, 0, size, comp); 138 } 139 140 public void dump() { 141 StringBuffer sb = new StringBuffer ("DeleteOIDStateContainer: "); 142 for (int i = 0; i < oids.length; i++) { 143 OID oid = oids[i]; 144 if (oid == null) break; 145 sb.append("\nOID = " + oid.toSString()); 146 if (keepStates) { 147 sb.append("\nState = " + states[i]); 148 } 149 sb.append("\nNext"); 150 } 151 System.out.println(sb.toString()); 152 } 153 154 public void writeExternal(OIDObjectOutput out) throws IOException { 155 out.writeBoolean(keepStates); 156 out.writeInt(size); 157 if (keepStates) { 158 for (int i = 0; i < size; i++) { 159 oids[i].resolve(states[i]); 160 out.writeShort(states[i].getClassIndex()); 161 out.writeWithoutCMD(oids[i]); 162 states[i].writeExternal(out); 163 } 164 } else { 165 for (int i = 0; i < size; i++) { 166 out.write(oids[i]); 167 } 168 } 169 } 170 171 public void readExternal(OIDObjectInput in) throws IOException, 172 ClassNotFoundException { 173 jmd = in.getModelMetaData(); 174 keepStates = in.readBoolean(); 175 size = in.readInt(); 176 oids = new OID[size]; 177 if (keepStates) { 178 states = new State[size]; 179 for (int i = 0; i < size; i++) { 180 ClassMetaData cmd = jmd.classes[in.readShort()]; 181 oids[i] = in.readOID(cmd); 182 states[i] = cmd.createState(); 183 states[i].readExternal(in); 184 } 185 } else { 186 for (int i = 0; i < size; i++) { 187 oids[i] = in.readOID(); 188 } 189 } 190 } 191 192 195 public Iterator iterator() { 196 return new Iter(); 197 } 198 199 public class Iter implements Iterator { 200 201 private int pos; 202 203 public boolean hasNext() { 204 return pos < size - 1; 205 } 206 207 public Object next() { 208 return oids[pos++]; 209 } 210 211 public void remove() { 212 throw new UnsupportedOperationException (); 213 } 214 215 } 216 217 } 218 | Popular Tags |