ss: Replace AttributeNotFoundException with IOOBE for quark parameters
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / internal / tmf / core / statesystem / mipmap / AbstractTmfMipmapStateProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2016 Ericsson
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:
10 * Jean-Christian Kouamé - Initial API and implementation
11 * Patrick Tasse - Updates to mipmap feature
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.internal.tmf.core.statesystem.mipmap;
15
16 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
17
18 import java.util.HashMap;
19 import java.util.LinkedHashSet;
20 import java.util.Map;
21 import java.util.Set;
22
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.tracecompass.internal.tmf.core.Activator;
25 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
26 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
27 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
28 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
29 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue.Type;
30 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
31 import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider;
32 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
33
34 /**
35 * This is an abstract state provider that allows attributes to be mipmapped
36 * for one or more of the supported mipmap features (min, max, average).
37 *
38 * Extend this class for a specific implementation
39 */
40 public abstract class AbstractTmfMipmapStateProvider extends AbstractTmfStateProvider {
41
42 /**
43 * Feature bit for the maximum mipmap feature (value is 1<<1).
44 */
45 public static final int MAX = 1 << 1;
46
47 /**
48 * Feature bit for the minimum mipmap feature (value is 1&lt;&lt;2).
49 */
50 public static final int MIN = 1 << 2;
51
52 /**
53 * Feature bit for the average mipmap feature (value is 1&lt;&lt;3).
54 */
55 public static final int AVG = 1 << 3;
56
57 /**
58 * The string for maximum mipmap feature sub-attribute.
59 * This attribute value is the mipmap number of levels.
60 * It has sub-attributes for every level ("1", "2", etc.)
61 */
62 public static final String MAX_STRING = "max"; //$NON-NLS-1$
63
64 /**
65 * The string for minimum mipmap feature sub-attribute.
66 * This attribute value is the mipmap number of levels.
67 * It has sub-attributes for every level ("1", "2", etc.)
68 */
69 public static final String MIN_STRING = "min"; //$NON-NLS-1$
70
71 /**
72 * The string for average mipmap feature sub-attribute.
73 * This attribute value is the mipmap number of levels.
74 * It has sub-attributes for every level ("1", "2", etc.)
75 */
76 public static final String AVG_STRING = "avg"; //$NON-NLS-1$
77
78 /**
79 * Map of mipmap features per attribute. The map's key is the base attribute quark.
80 */
81 private Map<Integer, Set<ITmfMipmapFeature>> featureMap = new HashMap<>();
82
83 // ------------------------------------------------------------------------
84 // Constructor
85 // ------------------------------------------------------------------------
86
87 /**
88 * Constructor
89 *
90 * @param trace
91 * The trace directory
92 * @param id
93 * The name given to this state change input. Only used
94 * internally.
95 */
96 public AbstractTmfMipmapStateProvider(@NonNull ITmfTrace trace,
97 @NonNull String id) {
98 super(trace, id);
99 }
100
101 // ------------------------------------------------------------------------
102 // Public methods
103 // ------------------------------------------------------------------------
104
105 @Override
106 public void dispose() {
107 waitForEmptyQueue();
108 for (Set<ITmfMipmapFeature> features : featureMap.values()) {
109 for (ITmfMipmapFeature feature : features) {
110 feature.updateAndCloseMipmap();
111 }
112 }
113 super.dispose();
114 }
115
116 /**
117 * Modify a mipmap attribute. The base attribute is modified and the mipmap
118 * attributes for the feature(s) specified in the mipmap feature bitmap are
119 * created and/or updated.<br>
120 * Note: The mipmapFeatureBits and resolution are only used on the first
121 * call of this method for a particular attribute, and the mipmap features
122 * for this attribute are then activated until the end of the trace.<br>
123 * Note: The base attribute should only be modified by calling this method.
124 *
125 * @param ts
126 * The timestamp of the event
127 * @param value
128 * The value of the base attribute
129 * @param baseQuark
130 * The quark of the base attribute
131 * @param mipmapFeatureBits
132 * The mipmap feature bit(s)
133 * @param resolution
134 * The mipmap resolution (must be greater than 1)
135 * @throws TimeRangeException
136 * If the requested time is outside of the trace's range
137 * @throws StateValueTypeException
138 * If the inserted state value's type does not match what is
139 * already assigned to this attribute.
140 * @see #MAX
141 * @see #MIN
142 * @see #AVG
143 */
144 public void modifyMipmapAttribute(long ts, ITmfStateValue value, int baseQuark, int mipmapFeatureBits, int resolution)
145 throws TimeRangeException, StateValueTypeException {
146 ITmfStateSystemBuilder ss = checkNotNull(getStateSystemBuilder());
147 ss.modifyAttribute(ts, value, baseQuark);
148 if (value.getType() == Type.LONG || value.getType() == Type.INTEGER || value.getType() == Type.DOUBLE || value.isNull()) {
149 Set<ITmfMipmapFeature> features = getFeatureSet(baseQuark, ts, value, mipmapFeatureBits, resolution);
150 for (ITmfMipmapFeature mf : features) {
151 mf.updateMipmap(value, ts);
152 }
153 }
154 }
155
156 // ------------------------------------------------------------------------
157 // Private methods
158 // ------------------------------------------------------------------------
159
160 private Set<ITmfMipmapFeature> getFeatureSet(int baseQuark, long ts, ITmfStateValue value, int mipmapFeatureBits, int resolution) {
161 ITmfStateSystemBuilder ss = checkNotNull(getStateSystemBuilder());
162
163 Set<ITmfMipmapFeature> features = featureMap.get(baseQuark);
164 if (features != null) {
165 return features;
166 }
167 features = new LinkedHashSet<>();
168 if (value.isNull()) {
169 return features;
170 }
171 featureMap.put(baseQuark, features);
172 if (resolution > 1) {
173 try {
174 if ((mipmapFeatureBits & MAX) != 0) {
175 int featureQuark = ss.getQuarkRelativeAndAdd(baseQuark, MAX_STRING);
176 ss.modifyAttribute(ts, TmfStateValue.newValueInt(0), featureQuark);
177 MaxMipmapFeature mf = new MaxMipmapFeature(baseQuark, featureQuark, resolution, ss);
178 features.add(mf);
179 }
180 if ((mipmapFeatureBits & MIN) != 0) {
181 int featureQuark = ss.getQuarkRelativeAndAdd(baseQuark, MIN_STRING);
182 ss.modifyAttribute(ts, TmfStateValue.newValueInt(0), featureQuark);
183 MinMipmapFeature mf = new MinMipmapFeature(baseQuark, featureQuark, resolution, ss);
184 features.add(mf);
185 }
186 if ((mipmapFeatureBits & AVG) != 0) {
187 int featureQuark = ss.getQuarkRelativeAndAdd(baseQuark, AVG_STRING);
188 ss.modifyAttribute(ts, TmfStateValue.newValueInt(0), featureQuark);
189 AvgMipmapFeature mf = new AvgMipmapFeature(baseQuark, featureQuark, resolution, ss);
190 features.add(mf);
191 }
192 } catch (TimeRangeException e) {
193 Activator.logError("MipMapProvider : Time stamp outside of time range of state system", e); //$NON-NLS-1$
194 } catch (StateValueTypeException e) {
195 Activator.logError("MipMapProvider : Wrong state value type", e); //$NON-NLS-1$
196 }
197 }
198 return features;
199 }
200 }
This page took 0.035877 seconds and 5 git commands to generate.