KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > util > ShapeList


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * --------------
28  * ShapeList.java
29  * --------------
30  * (C) Copyright 2003, 2004, by Object Refinery Limited.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: ShapeList.java,v 1.4 2005/10/18 13:24:19 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 13-Aug-2003 : Version 1 (DG);
40  *
41  */

42
43 package org.jfree.util;
44
45 import java.awt.Shape JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.io.ObjectInputStream JavaDoc;
48 import java.io.ObjectOutputStream JavaDoc;
49
50 import org.jfree.io.SerialUtilities;
51
52
53 /**
54  * A table of {@link Shape} objects.
55  *
56  * @author David Gilbert
57  */

58 public class ShapeList extends AbstractObjectList {
59
60     /**
61      * Creates a new list.
62      */

63     public ShapeList() {
64         super();
65     }
66
67     /**
68      * Returns a {@link Shape} object from the list.
69      *
70      * @param index the index (zero-based).
71      *
72      * @return The object.
73      */

74     public Shape JavaDoc getShape(final int index) {
75         return (Shape JavaDoc) get(index);
76     }
77
78     /**
79      * Sets the {@link Shape} for an item in the list. The list is expanded if necessary.
80      *
81      * @param index the index (zero-based).
82      * @param shape the {@link Shape}.
83      */

84     public void setShape(final int index, final Shape JavaDoc shape) {
85         set(index, shape);
86     }
87
88     /**
89      * Returns an independent copy of the list.
90      *
91      * @return A clone.
92      *
93      * @throws CloneNotSupportedException if an item in the list does not support cloning.
94      */

95     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
96         return super.clone();
97     }
98     
99     /**
100      * Tests the list for equality with another object (typically also a list).
101      *
102      * @param o the other object.
103      *
104      * @return A boolean.
105      */

106     public boolean equals(final Object JavaDoc o) {
107
108         if (o == null) {
109             return false;
110         }
111         
112         if (o == this) {
113             return true;
114         }
115         
116         if (o instanceof ShapeList) {
117             return super.equals(o);
118         }
119
120         return false;
121
122     }
123     
124     /**
125      * Returns a hash code value for the object.
126      *
127      * @return the hashcode
128      */

129     public int hashCode() {
130         return super.hashCode();
131     }
132
133     /**
134      * Provides serialization support.
135      *
136      * @param stream the output stream.
137      *
138      * @throws IOException if there is an I/O error.
139      */

140     private void writeObject(final ObjectOutputStream JavaDoc stream) throws IOException JavaDoc {
141
142         stream.defaultWriteObject();
143         final int count = size();
144         stream.writeInt(count);
145         for (int i = 0; i < count; i++) {
146             final Shape JavaDoc shape = getShape(i);
147             if (shape != null) {
148                 stream.writeInt(i);
149                 SerialUtilities.writeShape(shape, stream);
150             }
151             else {
152                 stream.writeInt(-1);
153             }
154         }
155
156     }
157     
158     /**
159      * Provides serialization support.
160      *
161      * @param stream the input stream.
162      *
163      * @throws IOException if there is an I/O error.
164      * @throws ClassNotFoundException if there is a classpath problem.
165      */

166     private void readObject(final ObjectInputStream JavaDoc stream) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
167
168         stream.defaultReadObject();
169         final int count = stream.readInt();
170         for (int i = 0; i < count; i++) {
171             final int index = stream.readInt();
172             if (index != -1) {
173                 setShape(index, SerialUtilities.readShape(stream));
174             }
175         }
176         
177     }
178
179 }
180
181
Popular Tags