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
.linuxtools
.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
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.IDefinitionScope
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
26 * A CTF enum declaration.
28 * The definition of a enum point basic data type. It will take the data from a
29 * trace and store it (and make it fit) as an integer and a string.
32 * @author Matthew Khouzam
33 * @author Simon Marchi
35 public final class EnumDeclaration
extends Declaration
implements ISimpleDatatypeDeclaration
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
41 private final EnumTable fTable
= new EnumTable();
42 private final IntegerDeclaration fContainerType
;
43 private final Set
<String
> fLabels
= new HashSet
<>();
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
52 * @param containerType
53 * the enum is an int, this is the type that the data is
54 * contained in. If you have 1000 possible values, you need at
55 * least a 10 bit enum. If you store 2 values in a 128 bit int,
56 * you are wasting space.
58 public EnumDeclaration(IntegerDeclaration containerType
) {
59 fContainerType
= containerType
;
62 // ------------------------------------------------------------------------
63 // Getters/Setters/Predicates
64 // ------------------------------------------------------------------------
68 * @return The container type
70 public IntegerDeclaration
getContainerType() {
71 return fContainerType
;
75 public long getAlignment() {
76 return this.getContainerType().getAlignment();
83 public int getMaximumSize() {
84 return fContainerType
.getMaximumSize();
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
95 public EnumDefinition
createDefinition(IDefinitionScope definitionScope
, String fieldName
, BitBuffer input
) throws CTFReaderException
{
97 IntegerDefinition value
= getContainerType().createDefinition(definitionScope
, fieldName
, input
);
98 return new EnumDefinition(this, definitionScope
, fieldName
, value
);
102 * Add a value. Do not overlap, this is <em><strong>not</strong></em> an
106 * lowest value that this int can be to have label as a return
109 * highest value that this int can be to have label as a return
112 * the name of the value.
113 * @return was the value be added? true == success
115 public boolean add(long low
, long high
, String label
) {
117 return fTable
.add(low
, high
, label
);
121 * Check if the label for a value (enum a{day=0,night=1} would return "day"
125 * the value to lookup
126 * @return the label of that value, can be null
128 public String
query(long value
) {
129 return fTable
.query(value
);
133 * Gets a set of labels of the enum
135 * @return A set of labels of the enum, can be empty but not null
138 public Set
<String
> getLabels() {
139 return Collections
.unmodifiableSet(fLabels
);
143 * Maps integer range -> string. A simple list for now, but feel free to
144 * optimize it. Babeltrace suggests an interval tree.
146 private class EnumTable
{
148 private final List
<LabelAndRange
> ranges
= new LinkedList
<>();
153 public boolean add(long low
, long high
, String label
) {
154 LabelAndRange newRange
= new LabelAndRange(low
, high
, label
);
156 for (LabelAndRange r
: ranges
) {
157 if (r
.intersects(newRange
)) {
162 ranges
.add(newRange
);
168 * Return the first label that matches a value
172 * @return the label corresponding to that value
174 public String
query(long value
) {
175 for (LabelAndRange r
: ranges
) {
176 if (r
.intersects(value
)) {
185 private static class LabelAndRange
{
187 private final long low
, high
;
188 private final String fLabel
;
195 public String
getLabel() {
199 public LabelAndRange(long low
, long high
, String str
) {
205 public boolean intersects(long i
) {
206 return (i
>= this.low
) && (i
<= this.high
);
209 public boolean intersects(LabelAndRange other
) {
210 return this.intersects(other
.low
)
211 || this.intersects(other
.high
);
216 public String
toString() {
217 /* Only used for debugging */
218 return "[declaration] enum[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$