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
;
22 import org
.eclipse
.jdt
.annotation
.Nullable
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.io
.BitBuffer
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.scope
.IDefinitionScope
;
27 import com
.google
.common
.collect
.ImmutableMap
;
30 * A CTF enum declaration.
32 * The definition of a enum point basic data type. It will take the data from a
33 * trace and store it (and make it fit) as an integer and a string.
36 * @author Matthew Khouzam
37 * @author Simon Marchi
39 public final class EnumDeclaration
extends Declaration
implements ISimpleDatatypeDeclaration
{
42 * A pair of longs class
46 public static class Pair
{
47 private final long fFirst
;
48 private final long fSecond
;
50 private Pair(long first
, long second
) {
56 * @return the first element
58 public long getFirst() {
63 * @return the second element
65 public long getSecond() {
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
74 private final EnumTable fTable
= new EnumTable();
75 private final IntegerDeclaration fContainerType
;
76 private final Set
<String
> fLabels
= new HashSet
<>();
78 // ------------------------------------------------------------------------
80 // ------------------------------------------------------------------------
85 * @param containerType
86 * the enum is an int, this is the type that the data is
87 * contained in. If you have 1000 possible values, you need at
88 * least a 10 bit enum. If you store 2 values in a 128 bit int,
89 * you are wasting space.
91 public EnumDeclaration(IntegerDeclaration containerType
) {
92 fContainerType
= containerType
;
95 // ------------------------------------------------------------------------
96 // Getters/Setters/Predicates
97 // ------------------------------------------------------------------------
101 * @return The container type
103 public IntegerDeclaration
getContainerType() {
104 return fContainerType
;
108 public long getAlignment() {
109 return this.getContainerType().getAlignment();
113 public int getMaximumSize() {
114 return fContainerType
.getMaximumSize();
117 // ------------------------------------------------------------------------
119 // ------------------------------------------------------------------------
122 public EnumDefinition
createDefinition(@Nullable IDefinitionScope definitionScope
, String fieldName
, BitBuffer input
) throws CTFException
{
124 IntegerDefinition value
= getContainerType().createDefinition(definitionScope
, fieldName
, input
);
125 return new EnumDefinition(this, definitionScope
, fieldName
, value
);
129 * Add a value. Do not overlap, this is <em><strong>not</strong></em> an
133 * lowest value that this int can be to have label as a return
136 * highest value that this int can be to have label as a return
139 * the name of the value.
140 * @return was the value be added? true == success
142 public boolean add(long low
, long high
, @Nullable String label
) {
144 return fTable
.add(low
, high
, label
);
148 * Check if the label for a value (enum a{day=0,night=1} would return "day"
152 * the value to lookup
153 * @return the label of that value, can be null
155 public @Nullable String
query(long value
) {
156 return fTable
.query(value
);
160 * Get the lookup table
162 * @return the lookup table
165 public Map
<String
, Pair
> getEnumTable() {
166 ImmutableMap
.Builder
<String
, Pair
> builder
= new ImmutableMap
.Builder
<>();
167 for (LabelAndRange range
: fTable
.ranges
) {
168 builder
.put(range
.getLabel(), new Pair(range
.low
, range
.high
));
170 return builder
.build();
175 * Gets a set of labels of the enum
177 * @return A set of labels of the enum, can be empty but not null
179 public Set
<String
> getLabels() {
180 return Collections
.unmodifiableSet(fLabels
);
184 * Maps integer range -> string. A simple list for now, but feel free to
185 * optimize it. Babeltrace suggests an interval tree.
187 private class EnumTable
{
189 private final List
<LabelAndRange
> ranges
= new LinkedList
<>();
194 public synchronized boolean add(long low
, long high
, @Nullable String label
) {
195 LabelAndRange newRange
= new LabelAndRange(low
, high
, label
);
197 for (LabelAndRange r
: ranges
) {
198 if (r
.intersects(newRange
)) {
203 ranges
.add(newRange
);
209 * Return the first label that matches a value
213 * @return the label corresponding to that value
215 public synchronized @Nullable String
query(long value
) {
216 for (LabelAndRange r
: ranges
) {
217 if (r
.intersects(value
)) {
225 public synchronized int hashCode() {
226 final int prime
= 31;
228 for (LabelAndRange range
: ranges
) {
229 result
= prime
* result
+ range
.hashCode();
235 public synchronized boolean equals(@Nullable Object obj
) {
242 if (getClass() != obj
.getClass()) {
245 EnumTable other
= (EnumTable
) obj
;
246 if (ranges
.size() != other
.ranges
.size()) {
249 for (int i
= 0; i
< ranges
.size(); i
++) {
250 if (!ranges
.get(i
).equals(other
.ranges
.get(i
))) {
259 private static class LabelAndRange
{
261 private final long low
, high
;
262 private final @Nullable String fLabel
;
269 public @Nullable String
getLabel() {
273 public LabelAndRange(long low
, long high
, @Nullable String str
) {
279 public boolean intersects(long i
) {
280 return (i
>= this.low
) && (i
<= this.high
);
283 public boolean intersects(LabelAndRange other
) {
284 return this.intersects(other
.low
)
285 || this.intersects(other
.high
);
289 public int hashCode() {
290 final int prime
= 31;
292 final String label
= fLabel
;
293 result
= prime
* result
+ ((label
== null) ?
0 : label
.hashCode());
294 result
= prime
* result
+ (int) (high ^
(high
>>> 32));
295 result
= prime
* result
+ (int) (low ^
(low
>>> 32));
300 public boolean equals(@Nullable Object obj
) {
307 if (getClass() != obj
.getClass()) {
310 LabelAndRange other
= (LabelAndRange
) obj
;
311 final String label
= fLabel
;
313 if (other
.fLabel
!= null) {
316 } else if (!label
.equals(other
.fLabel
)) {
319 if (high
!= other
.high
) {
322 if (low
!= other
.low
) {
330 public String
toString() {
331 /* Only used for debugging */
332 StringBuilder sb
= new StringBuilder();
333 sb
.append("[declaration] enum["); //$NON-NLS-1$
334 for (String label
: fLabels
) {
335 sb
.append("label:").append(label
).append(' '); //$NON-NLS-1$
337 sb
.append("type:").append(fContainerType
.toString()); //$NON-NLS-1$
339 String string
= sb
.toString();
344 public int hashCode() {
345 final int prime
= 31;
346 int result
= prime
+ fContainerType
.hashCode();
347 for (String label
: fLabels
) {
348 result
= prime
* result
+ label
.hashCode();
350 result
= prime
* result
+ fTable
.hashCode();
355 public boolean equals(@Nullable Object obj
) {
362 if (getClass() != obj
.getClass()) {
365 EnumDeclaration other
= (EnumDeclaration
) obj
;
366 if (!fContainerType
.equals(other
.fContainerType
)) {
369 if (fLabels
.size() != other
.fLabels
.size()) {
372 if (!fLabels
.containsAll(other
.fLabels
)) {
375 if (!fTable
.equals(other
.fTable
)) {
382 public boolean isBinaryEquivalent(@Nullable IDeclaration obj
) {
389 if (getClass() != obj
.getClass()) {
392 EnumDeclaration other
= (EnumDeclaration
) obj
;
393 if (!fContainerType
.isBinaryEquivalent(other
.fContainerType
)) {
396 if (fLabels
.size() != other
.fLabels
.size()) {
399 if (!fLabels
.containsAll(other
.fLabels
)) {
402 if (!fTable
.equals(other
.fTable
)) {