View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: Statistic.java
13   * Created: 2007-00-00
14   * Author: apohllo
15   * $Id: Statistic.java 3105 2009-08-03 12:21:28Z czerwin $
16   */
17  
18  package pl.edu.agh.cast.model.visual.backward;
19  
20  /**
21   * The Statistics class represents single row of statistics of given domain model, which corresponds to single graph
22   * node. The important properties of the default statistics are:
23   * <ul>
24   * <li>the name of the node (accessible by {@link #getNodeName()})
25   * <li>the number of target edges (i.e. the node is their end; accessible by {@link #getTarget()})
26   * <li>the number of source edges (i.e. the node is their beginning; accessible by {@link #getSource()})
27   * <li>the sum of edges (i.e. target + source edges; accessible by {@link #getTotal()})
28   * </ul>
29   *
30   * There are helper methods which provide string representations of the properties, which are numbers.
31   *
32   * Domain statistics may provide different meaning for these properties (e.g. the number of sent and received short
33   * messages, the amount of money transfered, etc.)
34   *
35   * There is also one special type of statistics - the summary row. It doesn't correspond to any graph node, but
36   * summarizes the statistics. It appears at the end of the statistics, and by default contains the total number of nodes
37   * and total number of source and target edges. This row is indicated by the <tt>isSummary</tt> flag. The row is
38   * constructed by the statistics dialog, so should not be provided by domain-specific statistic providers.
39   *
40   *
41   * @author AGH CAST Team
42   */
43  public class Statistic {
44  
45  	private String nodeName;
46  
47  	private double sourceValue;
48  
49  	private double targetValue;
50  
51  	private boolean isSummary;
52  
53  	/**
54  	 * Creates new statistics of integer values.
55  	 *
56  	 * @param nodeName
57  	 *            the name of the node which correspond to this statistics row; may not be null
58  	 * @param outgoing
59  	 *            the value of the statistics for the source edges
60  	 * @param incoming
61  	 *            the value of the statistics for the target edges
62  	 * @throws IllegalArgumentException
63  	 *             if node name is null
64  	 */
65  	public Statistic(String nodeName, int outgoing, int incoming) {
66  		setNodeName(nodeName);
67  		this.sourceValue = outgoing;
68  		this.targetValue = incoming;
69  	}
70  
71  	/**
72  	 * Creates new statistics of double values.
73  	 *
74  	 * @param nodeName
75  	 *            the name of the node which correspond to this statistics row; may not be null
76  	 * @param outgoing
77  	 *            the value of the statistics for the source edges
78  	 * @param incoming
79  	 *            the value of the statistics for the target edges
80  	 * @throws IllegalArgumentException
81  	 *             if node name is null
82  	 */
83  	public Statistic(String nodeName, double outgoing, double incoming) {
84  		super();
85  		setNodeName(nodeName);
86  		this.sourceValue = outgoing;
87  		this.targetValue = incoming;
88  	}
89  
90  	/**
91  	 * Creates new statistics with no values.
92  	 *
93  	 * @param nodeName
94  	 *            the name of the node which correspond to this statistics row
95  	 */
96  	public Statistic(String nodeName) {
97  		setNodeName(nodeName);
98  	}
99  
100 	/**
101 	 * Set the summary flag.
102 	 *
103 	 * @param value
104 	 *            the new flag value
105 	 */
106 	public void setSummary(boolean value) {
107 		this.isSummary = value;
108 	}
109 
110 	/**
111 	 * Is the statistic row a special (summary) row? It will always appear at the end of the statistics dialog.
112 	 *
113 	 * @return the summary flag
114 	 */
115 	public boolean isSummary() {
116 		return isSummary;
117 	}
118 
119 	/**
120 	 * Returns the name of the node which correspond to this statistics row.
121 	 *
122 	 * @return the name of the node which correspond to this statistics row
123 	 */
124 	public String getNodeName() {
125 		return nodeName;
126 	}
127 
128 	/**
129 	 * Sets the name of the node which correspond to this statistics row.
130 	 *
131 	 * @param nodeName
132 	 *            the name of the node which correspond to this statistics row; may not be null
133 	 * @throws IllegalArgumentException
134 	 *             if node name is null
135 	 */
136 	public void setNodeName(String nodeName) {
137 		if (nodeName == null) {
138 			throw new IllegalArgumentException("node name may not be null"); //$NON-NLS-1$
139 		}
140 		this.nodeName = nodeName;
141 	}
142 
143 	/**
144 	 * Returns the value of the statistics for the source edges.
145 	 *
146 	 * @return the value of the statistics for the source edges
147 	 */
148 	public double getSource() {
149 		return sourceValue;
150 	}
151 
152 	/**
153 	 * Sets the value of the statistics for the source edges.
154 	 *
155 	 * @param outgoing
156 	 *            the value of the statistics for the source edges
157 	 */
158 	public void setSource(double outgoing) {
159 		this.sourceValue = outgoing;
160 	}
161 
162 	/**
163 	 * Returns the value of the statistics for the target edges.
164 	 *
165 	 * @return the value of the statistics for the target edges
166 	 */
167 	public double getTarget() {
168 		return targetValue;
169 	}
170 
171 	/**
172 	 * Sets the value of the statistics for the target edges.
173 	 *
174 	 * @param incoming
175 	 *            the value of the statistics for the target edges
176 	 */
177 	public void setTarget(double incoming) {
178 		this.targetValue = incoming;
179 	}
180 
181 	/**
182 	 * {@inheritDoc}
183 	 *
184 	 * @see java.lang.Object#hashCode()
185 	 */
186 	@Override
187 	public int hashCode() {
188 		return (int)Math.floor(targetValue * 13 + sourceValue * 17 + nodeName.hashCode() * 19);
189 	}
190 
191 	/**
192 	 * {@inheritDoc}
193 	 *
194 	 * @see java.lang.Object#equals(java.lang.Object)
195 	 */
196 	@Override
197 	public boolean equals(Object other) {
198 		if (this == other) {
199 			return true;
200 		}
201 		if (!(other instanceof Statistic)) {
202 			return false;
203 		}
204 
205 		Statistic that = (Statistic)other;
206 
207 		if (this.targetValue != that.targetValue) {
208 			return false;
209 		}
210 		if (this.sourceValue != that.sourceValue) {
211 			return false;
212 		}
213 		if (!this.nodeName.equals(that.nodeName)) {
214 			return false;
215 		}
216 		return true;
217 	}
218 
219 	/**
220 	 * {@inheritDoc}
221 	 *
222 	 * @see java.lang.Object#toString()
223 	 */
224 	@Override
225 	public String toString() {
226 		return String.format("%1$s:%2$s:%3$s", nodeName, targetValue, sourceValue); //$NON-NLS-1$
227 	}
228 
229 	/**
230 	 * Returns the label (i.e. string representation) of the value of the statistics of the target edges.
231 	 *
232 	 * @return the label of the statistics for the target edge
233 	 */
234 	public String getTargetLabel() {
235 		return valueRepresentation(targetValue);
236 	}
237 
238 	/**
239 	 * Returns the label (i.e. string representation) of the value of the statistics of the source edges.
240 	 *
241 	 * @return the label of the statistics for the source edge
242 	 */
243 	public String getSourceLabel() {
244 		return valueRepresentation(sourceValue);
245 	}
246 
247 	/**
248 	 * Converts given double value to pretty string.
249 	 *
250 	 * @param value
251 	 *            the value to be converted
252 	 * @return formatted value
253 	 */
254 	private String valueRepresentation(double value) {
255 		String result = String.valueOf(value);
256 		if (value - Math.floor(value) == 0) {
257 			// this is an integer - cut the dot and zero
258 			return result.substring(0, result.length() - 2);
259 		}
260 		return result;
261 	}
262 
263 	/**
264 	 * Returns the total value of the statistics row.
265 	 *
266 	 * @return the total value of the statistics row
267 	 */
268 	public double getTotal() {
269 		return getSource() + getTarget();
270 	}
271 
272 	/**
273 	 * Returns the label of the total value of the statistics row.
274 	 *
275 	 * @return the label of the total value of the statistics row
276 	 */
277 	public String getTotalLabel() {
278 		return valueRepresentation(getTotal());
279 	}
280 
281 }