KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > common > OIDArray


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

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 JavaDoc;
20 import java.util.Comparator JavaDoc;
21
22 /**
23  * This is an array of OIDs that expands when full. It implements
24  * Externalizable to flatten the OIDs for fast Serialization.
25  */

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     /**
61      * Is oid in our list? This checks using a linear search.
62      */

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 JavaDoc {
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     /**
102      * Sort the OIDs.
103      */

104     public void sort(Comparator JavaDoc 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     /**
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] +
124                     " rgi " + 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 }
154
Popular Tags