001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2022, by David Gilbert 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 * TaskSeries.java 029 * --------------- 030 * (C) Copyright 2002-2022, by David Gilbert. 031 * 032 * Original Author: David Gilbert; 033 * Contributor(s): Tracy Hiltbrand; 034 * 035 */ 036 037package org.jfree.data.gantt; 038 039import java.util.ArrayList; 040import java.util.Collections; 041import java.util.List; 042import java.util.Objects; 043import org.jfree.chart.internal.Args; 044import org.jfree.chart.internal.CloneUtils; 045 046import org.jfree.data.general.Series; 047 048/** 049 * A series that contains zero, one or many {@link Task} objects. 050 * <P> 051 * This class is used as a building block for the {@link TaskSeriesCollection} 052 * class that can be used to construct basic Gantt charts. 053 */ 054public class TaskSeries<K extends Comparable<K>> extends Series<K> { 055 056 /** Storage for the tasks in the series. */ 057 private List<Task> tasks; 058 059 /** 060 * Constructs a new series with the specified name. 061 * 062 * @param name the series name ({@code null} not permitted). 063 */ 064 public TaskSeries(K name) { 065 super(name); 066 this.tasks = new ArrayList<>(); 067 } 068 069 /** 070 * Adds a task to the series and sends a 071 * {@link org.jfree.data.general.SeriesChangeEvent} to all registered 072 * listeners. 073 * 074 * @param task the task ({@code null} not permitted). 075 */ 076 public void add(Task task) { 077 Args.nullNotPermitted(task, "task"); 078 this.tasks.add(task); 079 fireSeriesChanged(); 080 } 081 082 /** 083 * Removes a task from the series and sends 084 * a {@link org.jfree.data.general.SeriesChangeEvent} 085 * to all registered listeners. 086 * 087 * @param task the task. 088 */ 089 public void remove(Task task) { 090 this.tasks.remove(task); 091 fireSeriesChanged(); 092 } 093 094 /** 095 * Removes all tasks from the series and sends 096 * a {@link org.jfree.data.general.SeriesChangeEvent} 097 * to all registered listeners. 098 */ 099 public void removeAll() { 100 this.tasks.clear(); 101 fireSeriesChanged(); 102 } 103 104 /** 105 * Returns the number of items in the series. 106 * 107 * @return The item count. 108 */ 109 @Override 110 public int getItemCount() { 111 return this.tasks.size(); 112 } 113 114 /** 115 * Returns a task from the series. 116 * 117 * @param index the task index (zero-based). 118 * 119 * @return The task. 120 */ 121 public Task get(int index) { 122 return this.tasks.get(index); 123 } 124 125 /** 126 * Returns the task in the series that has the specified description. 127 * 128 * @param description the name ({@code null} not permitted). 129 * 130 * @return The task (possibly {@code null}). 131 */ 132 public Task get(String description) { 133 Task result = null; 134 int count = this.tasks.size(); 135 for (int i = 0; i < count; i++) { 136 Task t = this.tasks.get(i); 137 if (t.getDescription().equals(description)) { 138 result = t; 139 break; 140 } 141 } 142 return result; 143 } 144 145 /** 146 * Returns an unmodifialble list of the tasks in the series. 147 * 148 * @return The tasks. 149 */ 150 public List<Task> getTasks() { 151 return Collections.unmodifiableList(this.tasks); 152 } 153 154 /** 155 * Tests this object for equality with an arbitrary object. 156 * 157 * @param obj the object to test against ({@code null} permitted). 158 * 159 * @return A boolean. 160 */ 161 @Override 162 public boolean equals(Object obj) { 163 if (obj == this) { 164 return true; 165 } 166 if (!(obj instanceof TaskSeries)) { 167 return false; 168 } 169 if (!super.equals(obj)) { 170 return false; 171 } 172 TaskSeries that = (TaskSeries) obj; 173 if (!this.tasks.equals(that.tasks)) { 174 return false; 175 } 176 return true; 177 } 178 179 @Override 180 public int hashCode(){ 181 int hash = 5; 182 hash = 67 * hash + Objects.hashCode(this.tasks); 183 return hash; 184 } 185 186 /** 187 * Returns an independent copy of this series. 188 * 189 * @return A clone of the series. 190 * 191 * @throws CloneNotSupportedException if there is some problem cloning 192 * the dataset. 193 */ 194 @Override 195 public Object clone() throws CloneNotSupportedException { 196 TaskSeries clone = (TaskSeries) super.clone(); 197 clone.tasks = CloneUtils.cloneList(this.tasks); 198 return clone; 199 } 200 201}