Commit | Line | Data |
---|---|---|
866e5b51 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Simon Marchi - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.ctf.core.event.types; | |
14 | ||
15 | import java.util.LinkedList; | |
16 | import java.util.List; | |
17 | ||
18 | /** | |
19 | * <b><u>EnumDeclaration</u></b> | |
20 | */ | |
21 | public class EnumDeclaration implements IDeclaration { | |
22 | ||
23 | // ------------------------------------------------------------------------ | |
24 | // Attributes | |
25 | // ------------------------------------------------------------------------ | |
26 | ||
27 | private final EnumTable table = new EnumTable(); | |
28 | private IntegerDeclaration containerType = null; | |
29 | ||
30 | // ------------------------------------------------------------------------ | |
31 | // Constructors | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
34 | public EnumDeclaration(IntegerDeclaration containerType) { | |
35 | this.containerType = containerType; | |
36 | } | |
37 | ||
38 | // ------------------------------------------------------------------------ | |
39 | // Getters/Setters/Predicates | |
40 | // ------------------------------------------------------------------------ | |
41 | ||
42 | public IntegerDeclaration getContainerType() { | |
43 | return containerType; | |
44 | } | |
45 | ||
46 | // ------------------------------------------------------------------------ | |
47 | // Operations | |
48 | // ------------------------------------------------------------------------ | |
49 | ||
50 | @Override | |
51 | public EnumDefinition createDefinition(IDefinitionScope definitionScope, | |
52 | String fieldName) { | |
53 | return new EnumDefinition(this, definitionScope, fieldName); | |
54 | } | |
55 | ||
56 | public boolean add(long low, long high, String label) { | |
57 | return table.add(low, high, label); | |
58 | } | |
59 | ||
60 | public String query(long value) { | |
61 | return table.query(value); | |
62 | } | |
63 | ||
64 | public String getLabel(long i) { | |
65 | return table.getLabel(i); | |
66 | } | |
67 | ||
68 | /* | |
69 | * Maps integer range -> string. A simple list for now, but feel free to | |
70 | * optimize it. Babeltrace suggests an interval tree. | |
71 | */ | |
72 | static private class EnumTable { | |
73 | ||
74 | List<Range> ranges = new LinkedList<Range>(); | |
75 | ||
76 | public EnumTable() { | |
77 | } | |
78 | ||
79 | public String getLabel(long i) { | |
80 | for (Range r : ranges) { | |
81 | if (r.intersects(i)) { | |
82 | return r.str; | |
83 | } | |
84 | } | |
85 | return null; | |
86 | } | |
87 | ||
88 | public boolean add(long low, long high, String label) { | |
89 | Range newRange = new Range(low, high, label); | |
90 | ||
91 | for (Range r : ranges) { | |
92 | if (r.intersects(newRange)) { | |
93 | return false; | |
94 | } | |
95 | } | |
96 | ||
97 | ranges.add(newRange); | |
98 | ||
99 | return true; | |
100 | } | |
101 | ||
102 | public String query(long value) { | |
103 | for (Range r : ranges) { | |
104 | if (r.intersects(value)) { | |
105 | return r.str; | |
106 | } | |
107 | } | |
108 | ||
109 | return null; | |
110 | } | |
111 | ||
112 | static private class Range { | |
113 | ||
114 | long low, high; | |
115 | String str; | |
116 | ||
117 | public Range(long low, long high, String str) { | |
118 | this.low = low; | |
119 | this.high = high; | |
120 | this.str = str; | |
121 | } | |
122 | ||
123 | public boolean intersects(long i) { | |
124 | return (i >= this.low) && (i <= this.high); | |
125 | } | |
126 | ||
127 | public boolean intersects(Range other) { | |
128 | return this.intersects(other.low) | |
129 | || this.intersects(other.high); | |
130 | } | |
131 | } | |
132 | } | |
133 | ||
134 | @Override | |
135 | public String toString() { | |
136 | /* Only used for debugging */ | |
137 | return "[declaration] enum[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$ | |
138 | } | |
139 | ||
140 | } |