1 2 12 package com.versant.core.common; 13 14 import com.versant.core.metadata.ClassMetaData; 15 import com.versant.core.util.OIDObjectOutput; 16 import com.versant.core.util.OIDObjectInput; 17 18 import java.io.*; 19 import java.util.Arrays ; 20 import java.util.Comparator ; 21 22 26 public final class OIDArray { 27 28 public OID[] oids = new OID[20]; 29 private int size; 30 31 public OIDArray() { 32 } 33 34 public OIDArray(OIDArray toCopy) { 35 size = toCopy.size; 36 oids = new OID[size]; 37 System.arraycopy(toCopy.oids, 0, oids, 0, size); 38 } 39 40 public void add(OID oid) { 41 if (size == oids.length) { 42 OID[] t = new OID[size * 2]; 43 System.arraycopy(oids, 0, t, 0, size); 44 oids = t; 45 } 46 oids[size++] = oid; 47 } 48 49 public void add(OID[] a, int offset, int length) { 50 if (size + length > oids.length) { 51 int n = size + length; 52 OID[] t = new OID[n]; 53 System.arraycopy(oids, 0, t, 0, size); 54 oids = t; 55 } 56 System.arraycopy(a, offset, oids, size, length); 57 size += length; 58 } 59 60 63 public boolean contains(OID oid) { 64 for (int i = size - 1; i >= 0; i--) { 65 if (oids[i].equals(oid)) { 66 return true; 67 } 68 } 69 return false; 70 } 71 72 public void readExternal(OIDObjectInput in) throws IOException, 73 ClassNotFoundException { 74 size = in.readInt(); 75 oids = new OID[size]; 76 for (int i = 0; i < size; i++) { 77 oids[i] = in.readOID(); 78 } 79 } 80 81 public void writeExternal(OIDObjectOutput out) throws IOException { 82 out.writeInt(size); 83 for (int i = 0; i < size; i++) { 84 out.write(oids[i]); 85 } 86 } 87 88 public void clear() { 89 Arrays.fill(oids, null); 90 size = 0; 91 } 92 93 public int size() { 94 return size; 95 } 96 97 public boolean isEmpty() { 98 return size == 0; 99 } 100 101 104 public void sort(Comparator comp) { 105 if (size <= 1) return; 106 if (size == 2) { 107 if (comp.compare(oids[0], oids[1]) > 0) { 108 OID t = oids[0]; 109 oids[0] = oids[1]; 110 oids[1] = t; 111 } 112 return; 113 } 114 Arrays.sort(oids, 0, size, comp); 115 } 116 117 120 public void dump() { 121 for (int i = 0; i < size; i++) { 122 ClassMetaData c = oids[i].getAvailableClassMetaData(); 123 System.out.println("[" + i + "] = " + oids[i] + 124 " rgi " + c.referenceGraphIndex + " index " + c.index); 125 } 126 } 127 128 131 public OID[] getOIDs() { 132 OID[] tmpOIDs = new OID[size]; 133 for (int i = 0; i < size; i++) { 134 tmpOIDs[i] = oids[i]; 135 } 136 return tmpOIDs; 137 } 138 139 142 public void copy(OID[] dest, int index) { 143 System.arraycopy(oids, 0, dest, index, size); 144 } 145 146 149 public OID get(int index) { 150 return oids[index]; 151 } 152 153 } 154 | Popular Tags |