KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > LineSegmentList


1 /*
2  * LineSegmentList.java
3  *
4  * Copyright (C) 2002-2004 Peter Graves
5  * $Id: LineSegmentList.java,v 1.4 2004/04/01 18:51:24 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * 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 {
26     private LineSegment[] array = new LineSegment[32];
27     private int size;
28
29     public final int size()
30     {
31         return size;
32     }
33
34     public final void clear()
35     {
36         for (int i = size-1; i >= 0; i--)
37             array[i] = null;
38         size = 0;
39     }
40
41     // No range checking.
42
public final LineSegment getSegment(int index)
43     {
44         return array[index];
45     }
46
47     public final LineSegment getLastSegment()
48     {
49         if (size > 0)
50             return array[size-1];
51         else
52             return null;
53     }
54
55     public final void addSegment(LineSegment segment)
56     {
57         if (size == array.length)
58             ensureCapacity(size+1);
59         array[size++] = segment;
60     }
61
62     public final void addSegment(int index, LineSegment segment)
63     {
64         if (index < 0 || index > size)
65             throw new IndexOutOfBoundsException JavaDoc();
66         if (size == array.length)
67             ensureCapacity(size+1);
68         if (index != size)
69             System.arraycopy(array, index, array, index+1, size-index);
70         array[index] = segment;
71         size++;
72     }
73
74     public final void setSegment(int index, LineSegment segment)
75     {
76         if (index < 0 || index >= size)
77             throw new IndexOutOfBoundsException JavaDoc();
78         array[index] = segment;
79     }
80
81     public final void removeSegment(LineSegment segment)
82     {
83         for (int i = 0; i < size; i++) {
84             if (array[i] == segment) {
85                 if (i != --size)
86                     System.arraycopy(array, i+1, array, i, size-i);
87                 array[size] = null;
88             }
89         }
90     }
91
92     private void ensureCapacity(int minimumCapacity)
93     {
94         int currentCapacity = array.length;
95         if (minimumCapacity > 0 && currentCapacity < minimumCapacity) {
96             final int newCapacity = Math.max(minimumCapacity, currentCapacity*2+2);
97             LineSegment newArray[] = new LineSegment[newCapacity];
98             System.arraycopy(array, 0, newArray, 0, size);
99             array = newArray;
100         }
101     }
102 }
103
Popular Tags