1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
4 * All rights reserved. This program and the accompanying materials are made
5 * 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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.ctf
.core
.event
.types
;
15 import java
.util
.Collections
;
16 import java
.util
.HashSet
;
17 import java
.util
.LinkedList
;
18 import java
.util
.List
;
21 import org
.eclipse
.jdt
.annotation
.Nullable
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.CTFReaderException
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.IDefinitionScope
;
27 * A CTF enum declaration.
29 * The definition of a enum point basic data type. It will take the data from a
30 * trace and store it (and make it fit) as an integer and a string.
33 * @author Matthew Khouzam
34 * @author Simon Marchi
36 public final class EnumDeclaration
extends Declaration
implements ISimpleDatatypeDeclaration
{
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
42 private final EnumTable fTable
= new EnumTable();
43 private final IntegerDeclaration fContainerType
;
44 private final Set
<String
> fLabels
= new HashSet
<>();
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
53 * @param containerType
54 * the enum is an int, this is the type that the data is
55 * contained in. If you have 1000 possible values, you need at
56 * least a 10 bit enum. If you store 2 values in a 128 bit int,
57 * you are wasting space.
59 public EnumDeclaration(IntegerDeclaration containerType
) {
60 fContainerType
= containerType
;
63 // ------------------------------------------------------------------------
64 // Getters/Setters/Predicates
65 // ------------------------------------------------------------------------
69 * @return The container type
71 public IntegerDeclaration
getContainerType() {
72 return fContainerType
;
76 public long getAlignment() {
77 return this.getContainerType().getAlignment();
81 public int getMaximumSize() {
82 return fContainerType
.getMaximumSize();
85 // ------------------------------------------------------------------------
87 // ------------------------------------------------------------------------
90 public EnumDefinition
createDefinition(@Nullable IDefinitionScope definitionScope
, String fieldName
, BitBuffer input
) throws CTFReaderException
{
92 IntegerDefinition value
= getContainerType().createDefinition(definitionScope
, fieldName
, input
);
93 return new EnumDefinition(this, definitionScope
, fieldName
, value
);
97 * Add a value. Do not overlap, this is <em><strong>not</strong></em> an
101 * lowest value that this int can be to have label as a return
104 * highest value that this int can be to have label as a return
107 * the name of the value.
108 * @return was the value be added? true == success
110 public boolean add(long low
, long high
, @Nullable String label
) {
112 return fTable
.add(low
, high
, label
);
116 * Check if the label for a value (enum a{day=0,night=1} would return "day"
120 * the value to lookup
121 * @return the label of that value, can be null
123 public @Nullable String
query(long value
) {
124 return fTable
.query(value
);
128 * Gets a set of labels of the enum
130 * @return A set of labels of the enum, can be empty but not null
132 public Set
<String
> getLabels() {
133 return Collections
.unmodifiableSet(fLabels
);
137 * Maps integer range -> string. A simple list for now, but feel free to
138 * optimize it. Babeltrace suggests an interval tree.
140 private class EnumTable
{
142 private final List
<LabelAndRange
> ranges
= new LinkedList
<>();
147 public synchronized boolean add(long low
, long high
, @Nullable String label
) {
148 LabelAndRange newRange
= new LabelAndRange(low
, high
, label
);
150 for (LabelAndRange r
: ranges
) {
151 if (r
.intersects(newRange
)) {
156 ranges
.add(newRange
);
162 * Return the first label that matches a value
166 * @return the label corresponding to that value
168 public synchronized @Nullable String
query(long value
) {
169 for (LabelAndRange r
: ranges
) {
170 if (r
.intersects(value
)) {
178 public synchronized int hashCode() {
179 final int prime
= 31;
181 for (LabelAndRange range
: ranges
) {
182 result
= prime
* result
+ range
.hashCode();
188 public synchronized boolean equals(@Nullable Object obj
) {
195 if (getClass() != obj
.getClass()) {
198 EnumTable other
= (EnumTable
) obj
;
199 if (ranges
.size() != other
.ranges
.size()) {
202 for (int i
= 0; i
< ranges
.size(); i
++) {
203 if (!ranges
.get(i
).equals(other
.ranges
.get(i
))) {
212 private static class LabelAndRange
{
214 private final long low
, high
;
215 private final @Nullable String fLabel
;
222 public @Nullable String
getLabel() {
226 public LabelAndRange(long low
, long high
, @Nullable String str
) {
232 public boolean intersects(long i
) {
233 return (i
>= this.low
) && (i
<= this.high
);
236 public boolean intersects(LabelAndRange other
) {
237 return this.intersects(other
.low
)
238 || this.intersects(other
.high
);
242 public int hashCode() {
243 final int prime
= 31;
245 final String label
= fLabel
;
246 result
= prime
* result
+ ((label
== null) ?
0 : label
.hashCode());
247 result
= prime
* result
+ (int) (high ^
(high
>>> 32));
248 result
= prime
* result
+ (int) (low ^
(low
>>> 32));
253 public boolean equals(@Nullable Object obj
) {
260 if (getClass() != obj
.getClass()) {
263 LabelAndRange other
= (LabelAndRange
) obj
;
264 final String label
= fLabel
;
266 if (other
.fLabel
!= null) {
269 } else if (!label
.equals(other
.fLabel
)) {
272 if (high
!= other
.high
) {
275 if (low
!= other
.low
) {
283 public String
toString() {
284 /* Only used for debugging */
285 StringBuilder sb
= new StringBuilder();
286 sb
.append("[declaration] enum["); //$NON-NLS-1$
287 for (String label
: fLabels
) {
288 sb
.append("label:").append(label
).append(' '); //$NON-NLS-1$
290 sb
.append("type:").append(fContainerType
.toString()); //$NON-NLS-1$
292 String string
= sb
.toString();
293 if (string
== null) {
294 throw new IllegalStateException();
300 public int hashCode() {
301 final int prime
= 31;
302 int result
= prime
+ fContainerType
.hashCode();
303 for (String label
: fLabels
) {
304 result
= prime
* result
+ label
.hashCode();
306 result
= prime
* result
+ fTable
.hashCode();
311 public boolean equals(@Nullable Object obj
) {
318 if (getClass() != obj
.getClass()) {
321 EnumDeclaration other
= (EnumDeclaration
) obj
;
322 if (!fContainerType
.equals(other
.fContainerType
)) {
325 if (fLabels
.size() != other
.fLabels
.size()) {
328 if (!fLabels
.containsAll(other
.fLabels
)) {
331 if (!fTable
.equals(other
.fTable
)) {
338 public boolean isBinaryEquivalent(@Nullable IDeclaration obj
) {
345 if (getClass() != obj
.getClass()) {
348 EnumDeclaration other
= (EnumDeclaration
) obj
;
349 if (!fContainerType
.isBinaryEquivalent(other
.fContainerType
)) {
352 if (fLabels
.size() != other
.fLabels
.size()) {
355 if (!fLabels
.containsAll(other
.fLabels
)) {
358 if (!fTable
.equals(other
.fTable
)) {