Source for org.jfree.util.StrokeList

   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:  * StrokeList.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: StrokeList.java,v 1.5 2005/10/18 13:24:19 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 19-Aug-2003 : Version 1 (DG);
  40:  * 
  41:  */
  42: 
  43: package org.jfree.util;
  44: 
  45: import java.awt.Stroke;
  46: import java.io.IOException;
  47: import java.io.ObjectInputStream;
  48: import java.io.ObjectOutputStream;
  49: 
  50: import org.jfree.io.SerialUtilities;
  51: 
  52: /**
  53:  * A table of {@link Stroke} objects.
  54:  *
  55:  * @author David Gilbert
  56:  */
  57: public class StrokeList extends AbstractObjectList {
  58: 
  59:     /**
  60:      * Creates a new list.
  61:      */
  62:     public StrokeList() {
  63:         super();
  64:     }
  65: 
  66:     /**
  67:      * Returns a {@link Stroke} object from the list.
  68:      *
  69:      * @param index the index (zero-based).
  70:      *
  71:      * @return The object.
  72:      */
  73:     public Stroke getStroke(final int index) {
  74:         return (Stroke) get(index);
  75:     }
  76: 
  77:     /**
  78:      * Sets the {@link Stroke} for an item in the list.  The list is expanded if necessary.
  79:      *
  80:      * @param index  the index (zero-based).
  81:      * @param stroke  the {@link Stroke}.
  82:      */
  83:     public void setStroke(final int index, final Stroke stroke) {
  84:         set(index, stroke);
  85:     }
  86: 
  87:     /**
  88:      * Returns an independent copy of the list.
  89:      * 
  90:      * @return A clone.
  91:      * 
  92:      * @throws CloneNotSupportedException if an item in the list cannot be cloned.
  93:      */
  94:     public Object clone() throws CloneNotSupportedException {
  95:         return super.clone();
  96:     }
  97:     
  98:     /**
  99:      * Tests the list for equality with another object (typically also a list).
 100:      *
 101:      * @param o  the other object.
 102:      *
 103:      * @return A boolean.
 104:      */
 105:     public boolean equals(final Object o) {
 106: 
 107:         if (o == null) {
 108:             return false;
 109:         }
 110:         
 111:         if (o == this) {
 112:             return true;
 113:         }
 114:         
 115:         if (o instanceof StrokeList) {
 116:             return super.equals(o);
 117:         }
 118: 
 119:         return false;
 120: 
 121:     }
 122:     
 123:     /**
 124:      * Returns a hash code value for the object.
 125:      *
 126:      * @return the hashcode
 127:      */
 128:     public int hashCode() {
 129:         return super.hashCode();
 130:     }
 131: 
 132:     /**
 133:      * Provides serialization support.
 134:      *
 135:      * @param stream  the output stream.
 136:      *
 137:      * @throws IOException  if there is an I/O error.
 138:      */
 139:     private void writeObject(final ObjectOutputStream stream) throws IOException {
 140: 
 141:         stream.defaultWriteObject();
 142:         final int count = size();
 143:         stream.writeInt(count);
 144:         for (int i = 0; i < count; i++) {
 145:             final Stroke stroke = getStroke(i);
 146:             if (stroke != null) {
 147:                 stream.writeInt(i);
 148:                 SerialUtilities.writeStroke(stroke, stream);
 149:             }
 150:             else {
 151:                 stream.writeInt(-1);
 152:             }
 153:         }
 154: 
 155:     }
 156:     
 157:     /**
 158:      * Provides serialization support.
 159:      *
 160:      * @param stream  the input stream.
 161:      *
 162:      * @throws IOException  if there is an I/O error.
 163:      * @throws ClassNotFoundException  if there is a classpath problem.
 164:      */
 165:     private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
 166: 
 167:         stream.defaultReadObject();
 168:         final int count = stream.readInt();
 169:         for (int i = 0; i < count; i++) {
 170:             final int index = stream.readInt();
 171:             if (index != -1) {
 172:                 setStroke(index, SerialUtilities.readStroke(stream));
 173:             }
 174:         }
 175:         
 176:     }
 177: 
 178: }