KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > indexing > FieldArray


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

11 package org.eclipse.core.internal.indexing;
12
13 class FieldArray {
14
15     protected Buffer buffer; // buffer over which this array is defined
16
protected int offset; // offset within the buffer of the first field in the array
17
protected int length; // length of a field
18
protected int stride; // the amount of space from one field to the next
19
protected int count; // the number of fields in the array
20

21     /**
22      * Constructor.
23      * buffer is the underlying Buffer object.
24      * offset is the offset within that buffer.
25      * length is the length of each field in the array
26      * stride the number of bytes from the beginning of one element to the beginning of the next
27      * count is the number of elements in the array
28      */

29     public FieldArray(Buffer buffer, int offset, int length, int stride, int count) {
30         this.buffer = buffer;
31         this.offset = offset;
32         this.length = length;
33         this.stride = stride;
34         this.count = count;
35     }
36
37     /**
38      * Returns the ith field of the array.
39      */

40     public Field fieldAt(int i) {
41         if (i >= count)
42             throw new ArrayIndexOutOfBoundsException JavaDoc();
43         return new Field(buffer, offset + (i * stride), length);
44     }
45
46     /**
47      * Inserts a new "empty" field before index i.
48      */

49     public Field insert(int i) {
50         count++;
51         if (i >= count)
52             throw new ArrayIndexOutOfBoundsException JavaDoc();
53         int s = offset + (i * stride); // source offset
54
int t = s + stride; // target offset
55
int n = (count - (i + 1)) * stride; // number of bytes to move
56
buffer.copyInternal(s, t, n);
57         return fieldAt(i).clear();
58     }
59
60     /**
61      * Removes the entry at index i and "squeezes the space up". Clears the last entry.
62      */

63     public void remove(int i) {
64         if (i >= count)
65             throw new ArrayIndexOutOfBoundsException JavaDoc();
66         int s = offset + ((i + 1) * stride); // source offset
67
int t = s - stride; // target offset
68
int n = (count - (i + 1)) * stride; // number of bytes to move
69
buffer.copyInternal(s, t, n);
70         fieldAt(count - 1).clear();
71         count--;
72     }
73 }
74
Popular Tags