001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.common;
012:
013: import com.versant.core.metadata.ClassMetaData;
014: import com.versant.core.util.OIDObjectOutput;
015: import com.versant.core.util.OIDObjectInput;
016:
017: import java.io.*;
018: import java.util.Arrays;
019: import java.util.Comparator;
020:
021: /**
022: * This is an array of OIDs that expands when full. It implements
023: * Externalizable to flatten the OIDs for fast Serialization.
024: */
025: public final class OIDArray {
026:
027: public OID[] oids = new OID[20];
028: private int size;
029:
030: public OIDArray() {
031: }
032:
033: public OIDArray(OIDArray toCopy) {
034: size = toCopy.size;
035: oids = new OID[size];
036: System.arraycopy(toCopy.oids, 0, oids, 0, size);
037: }
038:
039: public void add(OID oid) {
040: if (size == oids.length) {
041: OID[] t = new OID[size * 2];
042: System.arraycopy(oids, 0, t, 0, size);
043: oids = t;
044: }
045: oids[size++] = oid;
046: }
047:
048: public void add(OID[] a, int offset, int length) {
049: if (size + length > oids.length) {
050: int n = size + length;
051: OID[] t = new OID[n];
052: System.arraycopy(oids, 0, t, 0, size);
053: oids = t;
054: }
055: System.arraycopy(a, offset, oids, size, length);
056: size += length;
057: }
058:
059: /**
060: * Is oid in our list? This checks using a linear search.
061: */
062: public boolean contains(OID oid) {
063: for (int i = size - 1; i >= 0; i--) {
064: if (oids[i].equals(oid)) {
065: return true;
066: }
067: }
068: return false;
069: }
070:
071: public void readExternal(OIDObjectInput in) throws IOException,
072: ClassNotFoundException {
073: size = in.readInt();
074: oids = new OID[size];
075: for (int i = 0; i < size; i++) {
076: oids[i] = in.readOID();
077: }
078: }
079:
080: public void writeExternal(OIDObjectOutput out) throws IOException {
081: out.writeInt(size);
082: for (int i = 0; i < size; i++) {
083: out.write(oids[i]);
084: }
085: }
086:
087: public void clear() {
088: Arrays.fill(oids, null);
089: size = 0;
090: }
091:
092: public int size() {
093: return size;
094: }
095:
096: public boolean isEmpty() {
097: return size == 0;
098: }
099:
100: /**
101: * Sort the OIDs.
102: */
103: public void sort(Comparator comp) {
104: if (size <= 1)
105: 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: /**
118: * Dump to sysout.
119: */
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] + " rgi "
124: + c.referenceGraphIndex + " index " + c.index);
125: }
126: }
127:
128: /**
129: * Get our OIDs into a new array.
130: */
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: /**
140: * Copy our OIDs into an array at position index.
141: */
142: public void copy(OID[] dest, int index) {
143: System.arraycopy(oids, 0, dest, index, size);
144: }
145:
146: /**
147: * Get the OID at index.
148: */
149: public OID get(int index) {
150: return oids[index];
151: }
152:
153: }
|