01: /*
02: * LineSegmentList.java
03: *
04: * Copyright (C) 2002-2004 Peter Graves
05: * $Id: LineSegmentList.java,v 1.4 2004/04/01 18:51:24 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.j;
23:
24: public final class LineSegmentList {
25: private LineSegment[] array = new LineSegment[32];
26: private int size;
27:
28: public final int size() {
29: return size;
30: }
31:
32: public final void clear() {
33: for (int i = size - 1; i >= 0; i--)
34: array[i] = null;
35: size = 0;
36: }
37:
38: // No range checking.
39: public final LineSegment getSegment(int index) {
40: return array[index];
41: }
42:
43: public final LineSegment getLastSegment() {
44: if (size > 0)
45: return array[size - 1];
46: else
47: return null;
48: }
49:
50: public final void addSegment(LineSegment segment) {
51: if (size == array.length)
52: ensureCapacity(size + 1);
53: array[size++] = segment;
54: }
55:
56: public final void addSegment(int index, LineSegment segment) {
57: if (index < 0 || index > size)
58: throw new IndexOutOfBoundsException();
59: if (size == array.length)
60: ensureCapacity(size + 1);
61: if (index != size)
62: System.arraycopy(array, index, array, index + 1, size
63: - index);
64: array[index] = segment;
65: size++;
66: }
67:
68: public final void setSegment(int index, LineSegment segment) {
69: if (index < 0 || index >= size)
70: throw new IndexOutOfBoundsException();
71: array[index] = segment;
72: }
73:
74: public final void removeSegment(LineSegment segment) {
75: for (int i = 0; i < size; i++) {
76: if (array[i] == segment) {
77: if (i != --size)
78: System.arraycopy(array, i + 1, array, i, size - i);
79: array[size] = null;
80: }
81: }
82: }
83:
84: private void ensureCapacity(int minimumCapacity) {
85: int currentCapacity = array.length;
86: if (minimumCapacity > 0 && currentCapacity < minimumCapacity) {
87: final int newCapacity = Math.max(minimumCapacity,
88: currentCapacity * 2 + 2);
89: LineSegment newArray[] = new LineSegment[newCapacity];
90: System.arraycopy(array, 0, newArray, 0, size);
91: array = newArray;
92: }
93: }
94: }
|