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
.linuxtools
.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
22 public class HistogramBucket
{
24 private int fNbEvents
= 0;
25 private int fEvents
[];
29 * @param traceCount number of traces of the experiment.
31 public HistogramBucket(int traceCount
) {
32 fEvents
= new int[traceCount
];
37 * @param values list of values
39 public HistogramBucket(int... values
) {
41 for (int i
: fEvents
) {
48 * @param b a HistogramBucket to copy
50 public HistogramBucket(HistogramBucket b
) {
56 * @param b1 a HistogramBucket
57 * @param b2 another HistogramBucket
59 public HistogramBucket(HistogramBucket b1
, HistogramBucket b2
) {
65 * @return the number of events in this bucket
67 public int getNbEvents() {
72 * Add an event in this bucket
73 * @param traceIndex a trace index - see {@link HistogramDataModel#setTrace}.
75 public synchronized void addEvent(int traceIndex
) {
76 ensureCapacity(traceIndex
+ 1);
77 fEvents
[traceIndex
]++;
81 private void ensureCapacity(int len
) {
82 if (fEvents
== null) {
83 fEvents
= new int[len
];
84 } else if (fEvents
.length
< len
) {
85 int[] oldArray
= fEvents
;
86 fEvents
= new int[len
];
87 System
.arraycopy(oldArray
, 0, fEvents
, 0, oldArray
.length
);
92 * Gets the number of event in this bucket belonging to given trace
93 * @param traceIndex a trace index
94 * @return the number of events in this bucket belonging to the given trace
96 public int getNbEvent(int traceIndex
) {
97 if (fEvents
== null || fEvents
.length
<= traceIndex
) {
100 return fEvents
[traceIndex
];
104 * @return the number of traces in this bucket
106 public int getNbTraces() {
107 if (fEvents
== null) {
110 return fEvents
.length
;
114 * Merge the given bucket in this one.
115 * @param histogramBucket a bucket to merge in this one.
117 public synchronized void add(HistogramBucket histogramBucket
) {
118 if (histogramBucket
!= null && histogramBucket
.fNbEvents
!= 0) {
119 int len
= histogramBucket
.fEvents
.length
;
121 for (int i
= 0; i
< len
; i
++) {
122 int nbEvents
= histogramBucket
.fEvents
[i
];
123 fEvents
[i
] += nbEvents
;
124 fNbEvents
+= nbEvents
;
130 * @return <code>true</code> if this bucket contains no event, <code>false</code> otherwise.
132 public boolean isEmpty() {
133 return fNbEvents
== 0;
137 public int hashCode() {
138 final int prime
= 31;
140 result
= prime
* result
+ Arrays
.hashCode(fEvents
);
141 result
= prime
* result
+ fNbEvents
;
146 public boolean equals(Object obj
) {
153 if (getClass() != obj
.getClass()) {
156 HistogramBucket other
= (HistogramBucket
) obj
;
157 if (fNbEvents
!= other
.fNbEvents
) {
160 if (fNbEvents
!= 0 && !Arrays
.equals(fEvents
, other
.fEvents
)) {
167 public String
toString() {
168 StringBuilder sb
= new StringBuilder();
169 sb
.append(fNbEvents
);
170 sb
.append(": "); //$NON-NLS-1$
171 sb
.append(Arrays
.toString(fEvents
));
172 return sb
.toString();