001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2020, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 */ 028 029package org.jfree.chart.util; 030 031import java.awt.Stroke; 032import java.io.IOException; 033import java.io.ObjectInputStream; 034import java.io.ObjectOutputStream; 035 036/** 037 * A table of {@link Stroke} objects. 038 */ 039public class StrokeList extends AbstractObjectList { 040 041 /** 042 * Creates a new list. 043 */ 044 public StrokeList() { 045 super(); 046 } 047 048 /** 049 * Returns a {@link Stroke} object from the list. 050 * 051 * @param index the index (zero-based). 052 * 053 * @return The object. 054 */ 055 public Stroke getStroke(int index) { 056 return (Stroke) get(index); 057 } 058 059 /** 060 * Sets the {@link Stroke} for an item in the list. The list is expanded if necessary. 061 * 062 * @param index the index (zero-based). 063 * @param stroke the {@link Stroke}. 064 */ 065 public void setStroke(int index, Stroke stroke) { 066 set(index, stroke); 067 } 068 069 /** 070 * Returns an independent copy of the list. 071 * 072 * @return A clone. 073 * 074 * @throws CloneNotSupportedException if an item in the list cannot be cloned. 075 */ 076 @Override 077 public Object clone() throws CloneNotSupportedException { 078 return super.clone(); 079 } 080 081 /** 082 * Tests the list for equality with another object (typically also a list). 083 * 084 * @param o the other object. 085 * 086 * @return A boolean. 087 */ 088 @Override 089 public boolean equals(Object o) { 090 091 if (o == null) { 092 return false; 093 } 094 095 if (o == this) { 096 return true; 097 } 098 099 if (o instanceof StrokeList) { 100 return super.equals(o); 101 } 102 103 return false; 104 105 } 106 107 /** 108 * Returns a hash code value for the object. 109 * 110 * @return the hashcode 111 */ 112 @Override 113 public int hashCode() { 114 return super.hashCode(); 115 } 116 117 /** 118 * Provides serialization support. 119 * 120 * @param stream the output stream. 121 * 122 * @throws IOException if there is an I/O error. 123 */ 124 private void writeObject(ObjectOutputStream stream) throws IOException { 125 126 stream.defaultWriteObject(); 127 final int count = size(); 128 stream.writeInt(count); 129 for (int i = 0; i < count; i++) { 130 final Stroke stroke = getStroke(i); 131 if (stroke != null) { 132 stream.writeInt(i); 133 SerialUtils.writeStroke(stroke, stream); 134 } 135 else { 136 stream.writeInt(-1); 137 } 138 } 139 140 } 141 142 /** 143 * Provides serialization support. 144 * 145 * @param stream the input stream. 146 * 147 * @throws IOException if there is an I/O error. 148 * @throws ClassNotFoundException if there is a classpath problem. 149 */ 150 private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { 151 152 stream.defaultReadObject(); 153 int count = stream.readInt(); 154 for (int i = 0; i < count; i++) { 155 int index = stream.readInt(); 156 if (index != -1) { 157 setStroke(index, SerialUtils.readStroke(stream)); 158 } 159 } 160 161 } 162 163} 164 165