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
.nio
.ByteOrder
;
16 import java
.util
.Collections
;
17 import java
.util
.HashSet
;
18 import java
.util
.LinkedList
;
19 import java
.util
.List
;
23 import org
.eclipse
.jdt
.annotation
.Nullable
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.IDefinitionScope
;
28 import com
.google
.common
.collect
.ImmutableMap
;
31 * A CTF enum declaration.
33 * The definition of a enum point basic data type. It will take the data from a
34 * trace and store it (and make it fit) as an integer and a string.
37 * @author Matthew Khouzam
38 * @author Simon Marchi
40 public final class EnumDeclaration
extends Declaration
implements ISimpleDatatypeDeclaration
{
43 * A pair of longs class
47 public static class Pair
{
48 private final long fFirst
;
49 private final long fSecond
;
51 private Pair(long first
, long second
) {
57 * @return the first element
59 public long getFirst() {
64 * @return the second element
66 public long getSecond() {
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
75 private final EnumTable fTable
= new EnumTable();
76 private final IntegerDeclaration fContainerType
;
77 private final Set
<String
> fLabels
= new HashSet
<>();
79 // ------------------------------------------------------------------------
81 // ------------------------------------------------------------------------
86 * @param containerType
87 * the enum is an int, this is the type that the data is
88 * contained in. If you have 1000 possible values, you need at
89 * least a 10 bit enum. If you store 2 values in a 128 bit int,
90 * you are wasting space.
92 public EnumDeclaration(IntegerDeclaration containerType
) {
93 fContainerType
= containerType
;
96 // ------------------------------------------------------------------------
97 // Getters/Setters/Predicates
98 // ------------------------------------------------------------------------
102 * @return The container type
104 public IntegerDeclaration
getContainerType() {
105 return fContainerType
;
109 public long getAlignment() {
110 return this.getContainerType().getAlignment();
114 public int getMaximumSize() {
115 return fContainerType
.getMaximumSize();
122 public boolean isByteOrderSet() {
123 return fContainerType
.isByteOrderSet();
130 public ByteOrder
getByteOrder() {
131 return fContainerType
.getByteOrder();
134 // ------------------------------------------------------------------------
136 // ------------------------------------------------------------------------
139 public EnumDefinition
createDefinition(@Nullable IDefinitionScope definitionScope
, String fieldName
, BitBuffer input
) throws CTFException
{
141 IntegerDefinition value
= getContainerType().createDefinition(definitionScope
, fieldName
, input
);
142 return new EnumDefinition(this, definitionScope
, fieldName
, value
);
146 * Add a value. Do not overlap, this is <em><strong>not</strong></em> an
150 * lowest value that this int can be to have label as a return
153 * highest value that this int can be to have label as a return
156 * the name of the value.
157 * @return was the value be added? true == success
159 public boolean add(long low
, long high
, @Nullable String label
) {
161 return fTable
.add(low
, high
, label
);
165 * Check if the label for a value (enum a{day=0,night=1} would return "day"
169 * the value to lookup
170 * @return the label of that value, can be null
172 public @Nullable String
query(long value
) {
173 return fTable
.query(value
);
177 * Get the lookup table
179 * @return the lookup table
182 public Map
<String
, Pair
> getEnumTable() {
183 ImmutableMap
.Builder
<String
, Pair
> builder
= new ImmutableMap
.Builder
<>();
184 for (LabelAndRange range
: fTable
.ranges
) {
185 builder
.put(range
.getLabel(), new Pair(range
.low
, range
.high
));
187 return builder
.build();
192 * Gets a set of labels of the enum
194 * @return A set of labels of the enum, can be empty but not null
196 public Set
<String
> getLabels() {
197 return Collections
.unmodifiableSet(fLabels
);
201 * Maps integer range -> string. A simple list for now, but feel free to
202 * optimize it. Babeltrace suggests an interval tree.
204 private class EnumTable
{
206 private final List
<LabelAndRange
> ranges
= new LinkedList
<>();
211 public synchronized boolean add(long low
, long high
, @Nullable String label
) {
212 LabelAndRange newRange
= new LabelAndRange(low
, high
, label
);
214 for (LabelAndRange r
: ranges
) {
215 if (r
.intersects(newRange
)) {
220 ranges
.add(newRange
);
226 * Return the first label that matches a value
230 * @return the label corresponding to that value
232 public synchronized @Nullable String
query(long value
) {
233 for (LabelAndRange r
: ranges
) {
234 if (r
.intersects(value
)) {
242 public synchronized int hashCode() {
243 final int prime
= 31;
245 for (LabelAndRange range
: ranges
) {
246 result
= prime
* result
+ range
.hashCode();
252 public synchronized boolean equals(@Nullable Object obj
) {
259 if (getClass() != obj
.getClass()) {
262 EnumTable other
= (EnumTable
) obj
;
263 if (ranges
.size() != other
.ranges
.size()) {
266 for (int i
= 0; i
< ranges
.size(); i
++) {
267 if (!ranges
.get(i
).equals(other
.ranges
.get(i
))) {
276 private static class LabelAndRange
{
278 private final long low
, high
;
279 private final @Nullable String fLabel
;
286 public @Nullable String
getLabel() {
290 public LabelAndRange(long low
, long high
, @Nullable String str
) {
296 public boolean intersects(long i
) {
297 return (i
>= this.low
) && (i
<= this.high
);
300 public boolean intersects(LabelAndRange other
) {
301 return this.intersects(other
.low
)
302 || this.intersects(other
.high
);
306 public int hashCode() {
307 final int prime
= 31;
309 final String label
= fLabel
;
310 result
= prime
* result
+ ((label
== null) ?
0 : label
.hashCode());
311 result
= prime
* result
+ (int) (high ^
(high
>>> 32));
312 result
= prime
* result
+ (int) (low ^
(low
>>> 32));
317 public boolean equals(@Nullable Object obj
) {
324 if (getClass() != obj
.getClass()) {
327 LabelAndRange other
= (LabelAndRange
) obj
;
328 final String label
= fLabel
;
330 if (other
.fLabel
!= null) {
333 } else if (!label
.equals(other
.fLabel
)) {
336 if (high
!= other
.high
) {
339 if (low
!= other
.low
) {
347 public String
toString() {
348 /* Only used for debugging */
349 StringBuilder sb
= new StringBuilder();
350 sb
.append("[declaration] enum["); //$NON-NLS-1$
351 for (String label
: fLabels
) {
352 sb
.append("label:").append(label
).append(' '); //$NON-NLS-1$
354 sb
.append("type:").append(fContainerType
.toString()); //$NON-NLS-1$
356 String string
= sb
.toString();
361 public int hashCode() {
362 final int prime
= 31;
363 int result
= prime
+ fContainerType
.hashCode();
364 for (String label
: fLabels
) {
365 result
= prime
* result
+ label
.hashCode();
367 result
= prime
* result
+ fTable
.hashCode();
372 public boolean equals(@Nullable Object obj
) {
379 if (getClass() != obj
.getClass()) {
382 EnumDeclaration other
= (EnumDeclaration
) obj
;
383 if (!fContainerType
.equals(other
.fContainerType
)) {
386 if (fLabels
.size() != other
.fLabels
.size()) {
389 if (!fLabels
.containsAll(other
.fLabels
)) {
392 if (!fTable
.equals(other
.fTable
)) {
399 public boolean isBinaryEquivalent(@Nullable IDeclaration obj
) {
406 if (getClass() != obj
.getClass()) {
409 EnumDeclaration other
= (EnumDeclaration
) obj
;
410 if (!fContainerType
.isBinaryEquivalent(other
.fContainerType
)) {
413 if (fLabels
.size() != other
.fLabels
.size()) {
416 if (!fLabels
.containsAll(other
.fLabels
)) {
419 if (!fTable
.equals(other
.fTable
)) {