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