Fix some null warnings
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / internal / tmf / core / statesystem / mipmap / TmfMipmapFeature.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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 package org.eclipse.tracecompass.internal.tmf.core.statesystem.mipmap;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
20 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
21 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
22 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
23 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
24 import org.eclipse.tracecompass.statesystem.core.interval.TmfStateInterval;
25 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
26 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
27
28 /**
29 * The mipmap feature base implementation.
30 *
31 * @author Jean-Christian Kouamé
32 * @author Patrick Tasse
33 *
34 */
35 public abstract class TmfMipmapFeature implements ITmfMipmapFeature {
36
37 /** The current state value */
38 protected @NonNull ITmfStateValue currentValue = TmfStateValue.nullValue();
39 /** The current start time for the state value */
40 protected long currentStartTime;
41 /** The list of ongoing state intervals per mipmap level */
42 protected List<List<ITmfStateInterval>> intervals = new ArrayList<>();
43 /** The state system used to store the mipmap attributes */
44 protected ITmfStateSystemBuilder ss;
45
46 private int mipmapResolution;
47 private int mipmapQuark;
48 private List<Integer> levelQuarks = new ArrayList<>();
49
50 /**
51 * Constructor
52 *
53 * @param baseQuark
54 * The quark of the attribute we want to mipmap
55 * @param mipmapQuark
56 * The quark of the mipmap feature attribute
57 * @param mipmapResolution
58 * The resolution that will be used for the mipmap
59 * @param ss
60 * The state system in which to insert the state changes
61 */
62 public TmfMipmapFeature(int baseQuark, int mipmapQuark, int mipmapResolution, ITmfStateSystemBuilder ss) {
63 this.mipmapQuark = mipmapQuark;
64 this.mipmapResolution = mipmapResolution;
65 this.ss = ss;
66
67 /* store the base attribute quark at level 0 */
68 this.levelQuarks.add(baseQuark);
69
70 /* create the level 0 list */
71 intervals.add(new ArrayList<ITmfStateInterval>(mipmapResolution));
72 }
73
74 @Override
75 public void updateMipmap(ITmfStateValue value, long ts) {
76 /* if the value did not change, ignore it */
77 if (currentValue.equals(value)) {
78 return;
79 }
80
81 /* if the ongoing state value is not null, create and store a state interval */
82 if (!currentValue.isNull()) {
83 ITmfStateInterval interval = new TmfStateInterval(currentStartTime, ts, getLevelQuark(0), currentValue);
84 intervals.get(0).add(interval);
85 }
86
87 /* if the new value is not null, update the mipmap levels that are full */
88 if (!value.isNull()) {
89 int level = 0;
90 while (intervals.get(level).size() == getMipmapResolution()) {
91 updateMipmapLevel(++level, ts);
92 }
93 }
94
95 /* store the new value as the ongoing state value */
96 currentValue = value;
97 currentStartTime = ts;
98 }
99
100 @Override
101 public void updateAndCloseMipmap() {
102 if (!currentValue.isNull()) {
103 ITmfStateInterval interval = new TmfStateInterval(currentStartTime, currentStartTime, getLevelQuark(0), currentValue);
104 intervals.get(0).add(interval);
105 }
106 for (int level = 1; level <= getNbLevels(); level++) {
107 updateMipmapLevel(level, currentStartTime);
108 }
109 }
110
111 /**
112 * Compute and update the mipmap level attribute from the lower-level
113 * state interval list
114 *
115 * @param level
116 * The mipmap level to update
117 * @param endTime
118 * The end timestamp to use for the mipmap interval
119 */
120 protected void updateMipmapLevel(int level, long endTime) {
121 try {
122 /* get the lower-level interval list */
123 List<ITmfStateInterval> lowerIntervals = intervals.get(level - 1);
124 if (lowerIntervals.size() == 0) {
125 return;
126 }
127
128 /* get the start time from the first interval in the lower-level list */
129 long startTime = lowerIntervals.get(0).getStartTime();
130
131 /* compute the mipmap value */
132 ITmfStateValue value = computeMipmapValue(lowerIntervals, startTime, endTime);
133
134 /* clear the lower-level list */
135 lowerIntervals.clear();
136
137 /* get or create the current-level quark */
138 int levelQuark = ss.getQuarkRelativeAndAdd(mipmapQuark, String.valueOf(level));
139 if (!checkLevelExists(level)) {
140 addLevelQuark(levelQuark);
141 ss.updateOngoingState(TmfStateValue.newValueInt(level), mipmapQuark);
142 intervals.add(new ArrayList<ITmfStateInterval>(getMipmapResolution()));
143 }
144
145 /* add new interval to current-level list */
146 ITmfStateInterval interval = new TmfStateInterval(startTime, endTime, levelQuark, value);
147 intervals.get(level).add(interval);
148
149 /* update the current-level attribute */
150 ss.modifyAttribute(startTime, value, levelQuark);
151 } catch (StateValueTypeException e) {
152 e.printStackTrace();
153 } catch (AttributeNotFoundException e) {
154 e.printStackTrace();
155 } catch (TimeRangeException e) {
156 e.printStackTrace();
157 }
158 }
159
160 /**
161 * Compute the mipmap value from a list of lower-level state intervals
162 *
163 * @param lowerIntervals
164 * The list of lower-level state intervals
165 * @param startTime
166 * The start time of the mipmap interval
167 * @param endTime
168 * The end time of the mipmap interval
169 * @return A state value to be stored in the mipmap level attribute
170 */
171 protected abstract @NonNull ITmfStateValue computeMipmapValue(List<ITmfStateInterval> lowerIntervals, long startTime, long endTime);
172
173 /**
174 * Get the mipmap resolution
175 *
176 * @return The mipmap resolution for this feature
177 */
178 protected int getMipmapResolution() {
179 return mipmapResolution;
180 }
181
182 /**
183 * Get the mipmap feature quark. The state value
184 * of this attribute is the mipmap number of levels.
185 * This is the parent attribute of the mipmap level quarks.
186 *
187 * @return The attribute quark for this mipmap feature
188 */
189 protected int getMipmapQuark() {
190 return mipmapQuark;
191 }
192
193 /**
194 * Get the mipmap quark for the specified level.
195 * For level 0 the base attribute quark is returned.
196 *
197 * @param level
198 * The mipmap level (0 for the base attribute)
199 * @return The attribute quark for this mipmap level
200 */
201 protected int getLevelQuark(int level) {
202 return levelQuarks.get(level);
203 }
204
205 /**
206 * Add a new mipmap level quark.
207 *
208 * @param quark
209 * The attribute quark for the new mipmap level
210 */
211 protected void addLevelQuark(int quark) {
212 levelQuarks.add(quark);
213 }
214
215 /**
216 * Get the mipmap number of levels.
217 *
218 * @return The current number of mipmap levels for this feature
219 * (excluding the base attribute)
220 */
221 protected int getNbLevels() {
222 return levelQuarks.size() - 1;
223 }
224
225 /**
226 * Checks if a mipmap level exists.
227 *
228 * @param level
229 * The mipmap level to check
230 * @return true if this level exists, false otherwise
231 */
232 protected boolean checkLevelExists(int level) {
233 if (level >= levelQuarks.size() || level < 0) {
234 return false;
235 }
236 return true;
237 }
238
239 }
This page took 0.035263 seconds and 5 git commands to generate.