1 /*******************************************************************************
2 * Copyright (c) 2011-2012 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
.LinkedList
;
16 import java
.util
.List
;
19 * <b><u>EnumDeclaration</u></b>
21 public class EnumDeclaration
implements IDeclaration
{
23 // ------------------------------------------------------------------------
25 // ------------------------------------------------------------------------
27 private final EnumTable table
= new EnumTable();
28 private IntegerDeclaration containerType
= null;
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
34 public EnumDeclaration(IntegerDeclaration containerType
) {
35 this.containerType
= containerType
;
38 // ------------------------------------------------------------------------
39 // Getters/Setters/Predicates
40 // ------------------------------------------------------------------------
42 public IntegerDeclaration
getContainerType() {
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 public EnumDefinition
createDefinition(IDefinitionScope definitionScope
,
53 return new EnumDefinition(this, definitionScope
, fieldName
);
56 public boolean add(long low
, long high
, String label
) {
57 return table
.add(low
, high
, label
);
60 public String
query(long value
) {
61 return table
.query(value
);
64 public String
getLabel(long i
) {
65 return table
.getLabel(i
);
69 * Maps integer range -> string. A simple list for now, but feel free to
70 * optimize it. Babeltrace suggests an interval tree.
72 static private class EnumTable
{
74 List
<Range
> ranges
= new LinkedList
<Range
>();
79 public String
getLabel(long i
) {
80 for (Range r
: ranges
) {
81 if (r
.intersects(i
)) {
88 public boolean add(long low
, long high
, String label
) {
89 Range newRange
= new Range(low
, high
, label
);
91 for (Range r
: ranges
) {
92 if (r
.intersects(newRange
)) {
102 public String
query(long value
) {
103 for (Range r
: ranges
) {
104 if (r
.intersects(value
)) {
112 static private class Range
{
117 public Range(long low
, long high
, String str
) {
123 public boolean intersects(long i
) {
124 return (i
>= this.low
) && (i
<= this.high
);
127 public boolean intersects(Range other
) {
128 return this.intersects(other
.low
)
129 || this.intersects(other
.high
);
135 public String
toString() {
136 /* Only used for debugging */
137 return "[declaration] enum[" + Integer
.toHexString(hashCode()) + ']'; //$NON-NLS-1$