ctf: remove redundant null check in StructDeclaration
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / EnumDeclaration.java
CommitLineData
866e5b51 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
866e5b51
FC
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
13package org.eclipse.linuxtools.ctf.core.event.types;
14
816fde81
MK
15import java.util.Collections;
16import java.util.HashSet;
866e5b51
FC
17import java.util.LinkedList;
18import java.util.List;
816fde81 19import java.util.Set;
866e5b51 20
a4fa4e36
MK
21import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
22import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
23import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
24
866e5b51 25/**
d37aaa7f 26 * A CTF enum declaration.
0594c61c 27 *
816fde81
MK
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.
d37aaa7f
FC
30 *
31 * @version 1.0
32 * @author Matthew Khouzam
33 * @author Simon Marchi
866e5b51 34 */
8fa270f6 35public final class EnumDeclaration extends Declaration implements ISimpleDatatypeDeclaration {
866e5b51
FC
36
37 // ------------------------------------------------------------------------
38 // Attributes
39 // ------------------------------------------------------------------------
40
816fde81
MK
41 private final EnumTable fTable = new EnumTable();
42 private final IntegerDeclaration fContainerType;
3de23137 43 private final Set<String> fLabels = new HashSet<>();
866e5b51
FC
44
45 // ------------------------------------------------------------------------
46 // Constructors
47 // ------------------------------------------------------------------------
48
9ac2eb62
MK
49 /**
50 * constructor
51 *
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.
57 */
866e5b51 58 public EnumDeclaration(IntegerDeclaration containerType) {
816fde81 59 fContainerType = containerType;
866e5b51
FC
60 }
61
62 // ------------------------------------------------------------------------
63 // Getters/Setters/Predicates
64 // ------------------------------------------------------------------------
65
9ac2eb62
MK
66 /**
67 *
68 * @return The container type
69 */
866e5b51 70 public IntegerDeclaration getContainerType() {
816fde81 71 return fContainerType;
866e5b51
FC
72 }
73
fd74e6c1
MK
74 @Override
75 public long getAlignment() {
76 return this.getContainerType().getAlignment();
77 }
9ac2eb62 78
a4fa4e36
MK
79 /**
80 * @since 3.0
81 */
82 @Override
83 public int getMaximumSize() {
84 return fContainerType.getMaximumSize();
85 }
86
866e5b51
FC
87 // ------------------------------------------------------------------------
88 // Operations
89 // ------------------------------------------------------------------------
90
a4fa4e36
MK
91 /**
92 * @since 3.0
93 */
866e5b51 94 @Override
a4fa4e36
MK
95 public EnumDefinition createDefinition(IDefinitionScope definitionScope, String fieldName, BitBuffer input) throws CTFReaderException {
96 alignRead(input);
97 IntegerDefinition value = getContainerType().createDefinition(definitionScope, fieldName, input);
98 return new EnumDefinition(this, definitionScope, fieldName, value);
866e5b51
FC
99 }
100
9ac2eb62 101 /**
8fa270f6
MK
102 * Add a value. Do not overlap, this is <em><strong>not</strong></em> an
103 * interval tree.
816fde81
MK
104 *
105 * @param low
106 * lowest value that this int can be to have label as a return
107 * string
108 * @param high
109 * highest value that this int can be to have label as a return
110 * string
111 * @param label
112 * the name of the value.
9ac2eb62
MK
113 * @return was the value be added? true == success
114 */
866e5b51 115 public boolean add(long low, long high, String label) {
816fde81
MK
116 fLabels.add(label);
117 return fTable.add(low, high, label);
866e5b51
FC
118 }
119
9ac2eb62 120 /**
816fde81
MK
121 * Check if the label for a value (enum a{day=0,night=1} would return "day"
122 * for query(0)
123 *
124 * @param value
125 * the value to lookup
9ac2eb62
MK
126 * @return the label of that value, can be null
127 */
866e5b51 128 public String query(long value) {
816fde81
MK
129 return fTable.query(value);
130 }
131
132 /**
133 * Gets a set of labels of the enum
134 *
135 * @return A set of labels of the enum, can be empty but not null
c4767854 136 * @since 3.0
816fde81
MK
137 */
138 public Set<String> getLabels() {
139 return Collections.unmodifiableSet(fLabels);
866e5b51
FC
140 }
141
866e5b51
FC
142 /*
143 * Maps integer range -> string. A simple list for now, but feel free to
144 * optimize it. Babeltrace suggests an interval tree.
145 */
816fde81 146 private class EnumTable {
866e5b51 147
3de23137 148 private final List<LabelAndRange> ranges = new LinkedList<>();
866e5b51
FC
149
150 public EnumTable() {
151 }
152
866e5b51 153 public boolean add(long low, long high, String label) {
816fde81 154 LabelAndRange newRange = new LabelAndRange(low, high, label);
866e5b51 155
816fde81 156 for (LabelAndRange r : ranges) {
866e5b51
FC
157 if (r.intersects(newRange)) {
158 return false;
159 }
160 }
161
162 ranges.add(newRange);
163
164 return true;
165 }
166
9ac2eb62 167 /**
a511da0d 168 * Return the first label that matches a value
816fde81
MK
169 *
170 * @param value
171 * the value to query
9ac2eb62
MK
172 * @return the label corresponding to that value
173 */
866e5b51 174 public String query(long value) {
816fde81 175 for (LabelAndRange r : ranges) {
866e5b51 176 if (r.intersects(value)) {
816fde81 177 return r.fLabel;
866e5b51
FC
178 }
179 }
866e5b51
FC
180 return null;
181 }
182
816fde81 183 }
866e5b51 184
816fde81 185 private class LabelAndRange {
866e5b51 186
816fde81
MK
187 private final long low, high;
188 private final String fLabel;
866e5b51 189
816fde81
MK
190 public LabelAndRange(long low, long high, String str) {
191 this.low = low;
192 this.high = high;
193 this.fLabel = str;
194 }
866e5b51 195
816fde81
MK
196 public boolean intersects(long i) {
197 return (i >= this.low) && (i <= this.high);
198 }
199
200 public boolean intersects(LabelAndRange other) {
201 return this.intersects(other.low)
202 || this.intersects(other.high);
866e5b51
FC
203 }
204 }
205
206 @Override
207 public String toString() {
208 /* Only used for debugging */
209 return "[declaration] enum[" + Integer.toHexString(hashCode()) + ']'; //$NON-NLS-1$
210 }
211
212}
This page took 0.052069 seconds and 5 git commands to generate.