1 /*******************************************************************************
2 * Copyright (c) 2014 Kalray
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
10 * Xavier Raynaud - Initial API and implementation
11 * Patrick Tasse - Fix concurrency issue
12 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.views
.histogram
;
15 import java
.util
.Arrays
;
18 * This class counts events for a particular time range, taking into account origin of the event.
19 * @author Xavier Raynaud
21 public class HistogramBucket
{
23 private int fNbEvents
= 0;
24 private int fEvents
[];
28 * @param traceCount number of traces of the experiment.
30 public HistogramBucket(int traceCount
) {
31 fEvents
= new int[traceCount
];
36 * @param values list of values
38 public HistogramBucket(int... values
) {
40 for (int i
: fEvents
) {
47 * @param b a HistogramBucket to copy
49 public HistogramBucket(HistogramBucket b
) {
55 * @param b1 a HistogramBucket
56 * @param b2 another HistogramBucket
58 public HistogramBucket(HistogramBucket b1
, HistogramBucket b2
) {
64 * @return the number of events in this bucket
66 public int getNbEvents() {
71 * Add an event in this bucket
72 * @param traceIndex a trace index - see {@link HistogramDataModel#setTrace}.
74 public synchronized void addEvent(int traceIndex
) {
75 ensureCapacity(traceIndex
+ 1);
76 fEvents
[traceIndex
]++;
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
);
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
95 public int getNbEvent(int traceIndex
) {
96 if (fEvents
== null || fEvents
.length
<= traceIndex
) {
99 return fEvents
[traceIndex
];
103 * @return the number of traces in this bucket
105 public int getNbTraces() {
106 if (fEvents
== null) {
109 return fEvents
.length
;
113 * Merge the given bucket in this one.
114 * @param histogramBucket a bucket to merge in this one.
116 public synchronized void add(HistogramBucket histogramBucket
) {
117 if (histogramBucket
!= null && histogramBucket
.fNbEvents
!= 0) {
118 int len
= histogramBucket
.fEvents
.length
;
120 for (int i
= 0; i
< len
; i
++) {
121 int nbEvents
= histogramBucket
.fEvents
[i
];
122 fEvents
[i
] += nbEvents
;
123 fNbEvents
+= nbEvents
;
129 * @return <code>true</code> if this bucket contains no event, <code>false</code> otherwise.
131 public boolean isEmpty() {
132 return fNbEvents
== 0;
136 public int hashCode() {
137 final int prime
= 31;
139 result
= prime
* result
+ Arrays
.hashCode(fEvents
);
140 result
= prime
* result
+ fNbEvents
;
145 public boolean equals(Object obj
) {
152 if (getClass() != obj
.getClass()) {
155 HistogramBucket other
= (HistogramBucket
) obj
;
156 if (fNbEvents
!= other
.fNbEvents
) {
159 if (fNbEvents
!= 0 && !Arrays
.equals(fEvents
, other
.fEvents
)) {
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();