Commit | Line | Data |
---|---|---|
2fc582d2 XR |
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 | |
d5338235 | 11 | * Patrick Tasse - Fix concurrency issue |
2fc582d2 XR |
12 | *******************************************************************************/ |
13 | package org.eclipse.linuxtools.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 | * @since 3.0 | |
21 | */ | |
22 | public class HistogramBucket { | |
23 | ||
24 | private int fNbEvents = 0; | |
25 | private int fEvents[]; | |
26 | ||
27 | /** | |
28 | * Constructor | |
29 | * @param traceCount number of traces of the experiment. | |
30 | */ | |
31 | public HistogramBucket(int traceCount) { | |
32 | fEvents = new int[traceCount]; | |
33 | } | |
34 | ||
35 | /** | |
36 | * Constructor | |
37 | * @param values list of values | |
38 | */ | |
39 | public HistogramBucket(int... values) { | |
40 | fEvents = values; | |
d5338235 PT |
41 | for (int i : fEvents) { |
42 | fNbEvents += i; | |
2fc582d2 XR |
43 | } |
44 | } | |
45 | ||
46 | /** | |
47 | * Copy Constructor | |
48 | * @param b a HistogramBucket to copy | |
49 | */ | |
50 | public HistogramBucket(HistogramBucket b) { | |
51 | add(b); | |
52 | } | |
53 | ||
54 | /** | |
55 | * Merge Constructor | |
56 | * @param b1 a HistogramBucket | |
57 | * @param b2 another HistogramBucket | |
58 | */ | |
59 | public HistogramBucket(HistogramBucket b1, HistogramBucket b2) { | |
60 | add(b1); | |
61 | add(b2); | |
62 | } | |
63 | ||
64 | /** | |
65 | * @return the number of events in this bucket | |
66 | */ | |
67 | public int getNbEvents() { | |
68 | return fNbEvents; | |
69 | } | |
70 | ||
71 | /** | |
72 | * Add an event in this bucket | |
dc8f6da2 | 73 | * @param traceIndex a trace index - see {@link HistogramDataModel#setTrace}. |
2fc582d2 | 74 | */ |
d5338235 PT |
75 | public synchronized void addEvent(int traceIndex) { |
76 | ensureCapacity(traceIndex + 1); | |
2fc582d2 | 77 | fEvents[traceIndex]++; |
d5338235 | 78 | fNbEvents++; |
2fc582d2 XR |
79 | } |
80 | ||
81 | private void ensureCapacity(int len) { | |
82 | if (fEvents == null) { | |
83 | fEvents = new int[len]; | |
d5338235 | 84 | } else if (fEvents.length < len) { |
2fc582d2 XR |
85 | int[] oldArray = fEvents; |
86 | fEvents = new int[len]; | |
87 | System.arraycopy(oldArray, 0, fEvents, 0, oldArray.length); | |
88 | } | |
89 | } | |
90 | ||
91 | /** | |
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 | |
95 | */ | |
96 | public int getNbEvent(int traceIndex) { | |
97 | if (fEvents == null || fEvents.length<= traceIndex) { | |
98 | return 0; | |
99 | } | |
100 | return fEvents[traceIndex]; | |
101 | } | |
102 | ||
103 | /** | |
104 | * @return the number of traces in this bucket | |
105 | */ | |
106 | public int getNbTraces() { | |
107 | if (fEvents == null) { | |
108 | return 0; | |
109 | } | |
110 | return fEvents.length; | |
111 | } | |
112 | ||
113 | /** | |
114 | * Merge the given bucket in this one. | |
115 | * @param histogramBucket a bucket to merge in this one. | |
116 | */ | |
d5338235 | 117 | public synchronized void add(HistogramBucket histogramBucket) { |
2fc582d2 | 118 | if (histogramBucket != null && histogramBucket.fNbEvents != 0) { |
d5338235 PT |
119 | int len = histogramBucket.fEvents.length; |
120 | ensureCapacity(len); | |
121 | for (int i = 0; i < len; i++) { | |
122 | int nbEvents = histogramBucket.fEvents[i]; | |
123 | fEvents[i] += nbEvents; | |
124 | fNbEvents += nbEvents; | |
2fc582d2 XR |
125 | } |
126 | } | |
127 | } | |
128 | ||
129 | /** | |
130 | * @return <code>true</code> if this bucket contains no event, <code>false</code> otherwise. | |
131 | */ | |
132 | public boolean isEmpty() { | |
133 | return fNbEvents == 0; | |
134 | } | |
135 | ||
136 | @Override | |
137 | public int hashCode() { | |
138 | final int prime = 31; | |
139 | int result = 1; | |
140 | result = prime * result + Arrays.hashCode(fEvents); | |
141 | result = prime * result + fNbEvents; | |
142 | return result; | |
143 | } | |
144 | ||
145 | @Override | |
146 | public boolean equals(Object obj) { | |
147 | if (this == obj) { | |
148 | return true; | |
149 | } | |
150 | if (obj == null) { | |
151 | return false; | |
152 | } | |
153 | if (getClass() != obj.getClass()) { | |
154 | return false; | |
155 | } | |
156 | HistogramBucket other = (HistogramBucket) obj; | |
157 | if (fNbEvents != other.fNbEvents) { | |
158 | return false; | |
159 | } | |
160 | if (fNbEvents != 0 && !Arrays.equals(fEvents, other.fEvents)) { | |
161 | return false; | |
162 | } | |
163 | return true; | |
164 | } | |
165 | ||
166 | @Override | |
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(); | |
173 | } | |
174 | ||
175 | } |