tmf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / histogram / HistogramBucket.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Kalray
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Xavier Raynaud - Initial API and implementation
11 * Patrick Tasse - Fix concurrency issue
12 *******************************************************************************/
13 package org.eclipse.tracecompass.tmf.ui.views.histogram;
14
15 import java.util.Arrays;
16
17 /**
18 * This class counts events for a particular time range, taking into account origin of the event.
19 * @author Xavier Raynaud
20 */
21 public class HistogramBucket {
22
23 private int fNbEvents = 0;
24 private int fEvents[];
25
26 /**
27 * Constructor
28 * @param traceCount number of traces of the experiment.
29 */
30 public HistogramBucket(int traceCount) {
31 fEvents = new int[traceCount];
32 }
33
34 /**
35 * Constructor
36 * @param values list of values
37 */
38 public HistogramBucket(int... values) {
39 fEvents = values;
40 for (int i : fEvents) {
41 fNbEvents += i;
42 }
43 }
44
45 /**
46 * Copy Constructor
47 * @param b a HistogramBucket to copy
48 */
49 public HistogramBucket(HistogramBucket b) {
50 add(b);
51 }
52
53 /**
54 * Merge Constructor
55 * @param b1 a HistogramBucket
56 * @param b2 another HistogramBucket
57 */
58 public HistogramBucket(HistogramBucket b1, HistogramBucket b2) {
59 add(b1);
60 add(b2);
61 }
62
63 /**
64 * @return the number of events in this bucket
65 */
66 public int getNbEvents() {
67 return fNbEvents;
68 }
69
70 /**
71 * Add an event in this bucket
72 * @param traceIndex a trace index - see {@link HistogramDataModel#setTrace}.
73 */
74 public synchronized void addEvent(int traceIndex) {
75 ensureCapacity(traceIndex + 1);
76 fEvents[traceIndex]++;
77 fNbEvents++;
78 }
79
80 private void ensureCapacity(int len) {
81 if (fEvents == null) {
82 fEvents = new int[len];
83 } else if (fEvents.length < len) {
84 int[] oldArray = fEvents;
85 fEvents = new int[len];
86 System.arraycopy(oldArray, 0, fEvents, 0, oldArray.length);
87 }
88 }
89
90 /**
91 * Gets the number of event in this bucket belonging to given trace
92 * @param traceIndex a trace index
93 * @return the number of events in this bucket belonging to the given trace
94 */
95 public int getNbEvent(int traceIndex) {
96 if (fEvents == null || fEvents.length<= traceIndex) {
97 return 0;
98 }
99 return fEvents[traceIndex];
100 }
101
102 /**
103 * @return the number of traces in this bucket
104 */
105 public int getNbTraces() {
106 if (fEvents == null) {
107 return 0;
108 }
109 return fEvents.length;
110 }
111
112 /**
113 * Merge the given bucket in this one.
114 * @param histogramBucket a bucket to merge in this one.
115 */
116 public synchronized void add(HistogramBucket histogramBucket) {
117 if (histogramBucket != null && histogramBucket.fNbEvents != 0) {
118 int len = histogramBucket.fEvents.length;
119 ensureCapacity(len);
120 for (int i = 0; i < len; i++) {
121 int nbEvents = histogramBucket.fEvents[i];
122 fEvents[i] += nbEvents;
123 fNbEvents += nbEvents;
124 }
125 }
126 }
127
128 /**
129 * @return <code>true</code> if this bucket contains no event, <code>false</code> otherwise.
130 */
131 public boolean isEmpty() {
132 return fNbEvents == 0;
133 }
134
135 @Override
136 public int hashCode() {
137 final int prime = 31;
138 int result = 1;
139 result = prime * result + Arrays.hashCode(fEvents);
140 result = prime * result + fNbEvents;
141 return result;
142 }
143
144 @Override
145 public boolean equals(Object obj) {
146 if (this == obj) {
147 return true;
148 }
149 if (obj == null) {
150 return false;
151 }
152 if (getClass() != obj.getClass()) {
153 return false;
154 }
155 HistogramBucket other = (HistogramBucket) obj;
156 if (fNbEvents != other.fNbEvents) {
157 return false;
158 }
159 if (fNbEvents != 0 && !Arrays.equals(fEvents, other.fEvents)) {
160 return false;
161 }
162 return true;
163 }
164
165 @Override
166 public String toString() {
167 StringBuilder sb = new StringBuilder();
168 sb.append(fNbEvents);
169 sb.append(": "); //$NON-NLS-1$
170 sb.append(Arrays.toString(fEvents));
171 return sb.toString();
172 }
173
174 }
This page took 0.058309 seconds and 5 git commands to generate.