Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SOMDataItem |
|
| 3.8181818181818183;3.818 |
1 | /* ======================================================================= | |
2 | * A visualisation library extension for JFreeChart. Please see JFreeChart | |
3 | * for further information. | |
4 | * ======================================================================= | |
5 | * Copyright (C) 2006 University of Helsinki, Department of Computer Science | |
6 | * | |
7 | * This library is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2.1 of the License, or (at your option) any later version. | |
11 | * | |
12 | * This library 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 GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
18 | * License along with this library; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | * ----------------------------- | |
21 | * Contact: ohtu@cs.helsinki.fi | |
22 | * ----------------------------- | |
23 | * | |
24 | */ | |
25 | ||
26 | ||
27 | package org.jfree.data.som; | |
28 | ||
29 | import java.awt.Color; | |
30 | import java.io.Serializable; | |
31 | ||
32 | /** | |
33 | * A simple data-storage object to be used in a | |
34 | * {@link org.jfree.chart.data.som.SOMDataset}. This class collects the | |
35 | * data | |
36 | * from a single SOM-map cell. | |
37 | */ | |
38 | public class SOMDataItem implements Cloneable, Serializable { | |
39 | ||
40 | /** The color associated with a dataitem. */ | |
41 | private Color color; | |
42 | ||
43 | /** Textual descriptions and names of a dataitem. */ | |
44 | private String[] descriptions; | |
45 | ||
46 | /** Numerical data contained in a dataitem. */ | |
47 | private double[] neuronWeights; | |
48 | ||
49 | /** Identifier for on-screen cell-selection. */ | |
50 | private boolean selected; | |
51 | ||
52 | ||
53 | /** | |
54 | * Creates a new SOMDataItem. | |
55 | * | |
56 | * @param color the color of the cell. | |
57 | * @param descriptions the textual descriptions and/or names of this dataitem. | |
58 | * @param neuronWeights the numerical data contained in a cell. | |
59 | * @throws NullPointerException If any parameter is null. | |
60 | * @throws IllegalArgumentException If descriptions or neuronWeights do not contain any items. | |
61 | */ | |
62 | public SOMDataItem(Color color, String descriptions[], double[] neuronWeights) | |
63 | 163 | throws NullPointerException { |
64 | 163 | if (color == null || descriptions == null || neuronWeights == null) |
65 | 3 | throw new NullPointerException("color/descriptions/neuronWeights given to SOMDataItem was null."); |
66 | 160 | if (descriptions.length == 0) |
67 | 1 | throw new IllegalArgumentException("descriptions given to SOMDataItem did not contain any strings."); |
68 | 159 | if (neuronWeights.length == 0) |
69 | 1 | throw new IllegalArgumentException("neuronWeights given to SOMDataItem did not contain any values."); |
70 | 158 | this.color = color; |
71 | 158 | this.descriptions = descriptions; |
72 | 158 | this.neuronWeights = neuronWeights; |
73 | 158 | this.selected = false; |
74 | 158 | } |
75 | ||
76 | /** | |
77 | * Returns the color of a dataitem. | |
78 | * | |
79 | * @return The dataitem color. | |
80 | */ | |
81 | public Color getColor() { | |
82 | 198 | return color; |
83 | } | |
84 | ||
85 | /** | |
86 | * Returns the textual descriptions of a dataitem. | |
87 | * | |
88 | * @return The textual descriptions. | |
89 | */ | |
90 | public String[] getDescriptions() { | |
91 | 108 | return descriptions; |
92 | } | |
93 | ||
94 | /** | |
95 | * Returns the numerical data of a dataitem. | |
96 | * | |
97 | * @return The numerical data | |
98 | */ | |
99 | public double[] getNeuronWeights() { | |
100 | 18 | return neuronWeights; |
101 | } | |
102 | ||
103 | /** | |
104 | * Sets the color of a dataitem. | |
105 | * | |
106 | * @param color the color. | |
107 | */ | |
108 | public void setColor(Color color) { | |
109 | 46 | if (color == null) |
110 | 1 | throw new NullPointerException("color given to setColor() was null."); |
111 | 45 | this.color = color; |
112 | 45 | } |
113 | ||
114 | /** | |
115 | * Sets the textual descriptions of a dataitem. | |
116 | * | |
117 | * @param descriptions the descriptions or names. | |
118 | */ | |
119 | public void setDescriptions(String[] descriptions) { | |
120 | 2 | if (descriptions == null) |
121 | 1 | throw new NullPointerException("descriptions given to setDescriptions() was null."); |
122 | 1 | this.descriptions = descriptions; |
123 | 1 | } |
124 | ||
125 | /** | |
126 | * Sets the numerical datavalues of a dataitem. | |
127 | * | |
128 | * @param neuronWeights the numerical data. | |
129 | */ | |
130 | public void setNeuronWeights(double[] neuronWeights) { | |
131 | 2 | if (neuronWeights == null) |
132 | 1 | throw new NullPointerException("neuronWeights given to setNeuronWeights() was null."); |
133 | 1 | this.neuronWeights = neuronWeights; |
134 | 1 | } |
135 | ||
136 | /** | |
137 | * Returns the boolean value indicating whether a dataitem is | |
138 | * selected on-screen or not. | |
139 | * | |
140 | * @return A boolean. | |
141 | */ | |
142 | public boolean isSelected() { | |
143 | 104 | return selected; |
144 | } | |
145 | ||
146 | /** | |
147 | * Sets the boolean value indicating whether a dataitem is | |
148 | * selected on-screen or not. | |
149 | * | |
150 | * @param selected A boolean. | |
151 | */ | |
152 | public void setSelected(boolean selected) { | |
153 | 96 | this.selected = selected; |
154 | 96 | } |
155 | ||
156 | /** | |
157 | * Tests if this object is equal to another. | |
158 | * | |
159 | * @param obj the other object. | |
160 | * | |
161 | * @return A boolean. | |
162 | */ | |
163 | public boolean equals(Object obj) { | |
164 | 60 | if (obj == this) { |
165 | 25 | return true; |
166 | } | |
167 | ||
168 | 35 | if (!(obj instanceof SOMDataItem)) { |
169 | 1 | return false; |
170 | } | |
171 | ||
172 | 34 | SOMDataItem that = (SOMDataItem) obj; |
173 | ||
174 | 34 | if (this.color.equals(that.color) == false) |
175 | 12 | return false; |
176 | ||
177 | 22 | if (this.selected != that.selected) |
178 | 1 | return false; |
179 | ||
180 | 21 | if (this.descriptions.length != that.descriptions.length) |
181 | 4 | return false; |
182 | ||
183 | 44 | for (int i=0; i < this.descriptions.length; ++i) { |
184 | 31 | if (this.descriptions[i] == null && that.descriptions[i] == null) { |
185 | ; | |
186 | 1 | } |
187 | 30 | else if (this.descriptions[i] == null || |
188 | that.descriptions[i] == null || | |
189 | this.descriptions[i].equals(that.descriptions[i]) == false) | |
190 | 4 | return false; |
191 | } | |
192 | ||
193 | 13 | if (this.neuronWeights.length != that.neuronWeights.length) |
194 | 2 | return false; |
195 | ||
196 | 34 | for (int i=0; i < this.neuronWeights.length; ++i) { |
197 | 25 | if (this.neuronWeights[i] != that.neuronWeights[i]) |
198 | 2 | return false; |
199 | } | |
200 | ||
201 | 9 | return true; |
202 | } | |
203 | ||
204 | /** | |
205 | * Returns a clone of the dataset. | |
206 | * | |
207 | * @return A clone. | |
208 | * | |
209 | * @throws CloneNotSupportedException This class will not throw this | |
210 | * exception, but subclasses (if any) might. | |
211 | */ | |
212 | public Object clone() throws CloneNotSupportedException { | |
213 | 1 | SOMDataItem clone = (SOMDataItem) super.clone(); |
214 | 1 | clone.descriptions = (String[]) this.descriptions.clone(); |
215 | 1 | clone.neuronWeights = (double[]) this.neuronWeights.clone(); |
216 | 1 | return clone; |
217 | } | |
218 | ||
219 | } | |
220 |