| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013, 2015 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.tmf.core.tests.statesystem.mipmap; |
| 15 | |
| 16 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
| 17 | |
| 18 | import org.eclipse.jdt.annotation.NonNull; |
| 19 | import org.eclipse.tracecompass.internal.tmf.core.Activator; |
| 20 | import org.eclipse.tracecompass.internal.tmf.core.statesystem.mipmap.AbstractTmfMipmapStateProvider; |
| 21 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder; |
| 22 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; |
| 23 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; |
| 24 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; |
| 25 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; |
| 26 | import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue; |
| 27 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
| 28 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; |
| 29 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventType; |
| 30 | import org.eclipse.tracecompass.tmf.core.event.TmfEvent; |
| 31 | import org.eclipse.tracecompass.tmf.core.event.TmfEventField; |
| 32 | import org.eclipse.tracecompass.tmf.core.event.TmfEventType; |
| 33 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; |
| 34 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp; |
| 35 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; |
| 36 | import org.eclipse.tracecompass.tmf.tests.stubs.trace.TmfTraceStub; |
| 37 | |
| 38 | /** |
| 39 | * A mipmap state provider for test |
| 40 | * |
| 41 | * @author Jean-Christian Kouamé |
| 42 | */ |
| 43 | class TmfMipmapStateProviderStub extends AbstractTmfMipmapStateProvider { |
| 44 | /** test attribute name */ |
| 45 | public final static String TEST_ATTRIBUTE_NAME = "test_attribute"; //$NON-NLS-1$ |
| 46 | |
| 47 | private int resolution; |
| 48 | private ITmfStateValue.Type type; |
| 49 | private static final @NonNull String MIPMAP_ID = "MIPMAP_ID"; //$NON-NLS-1$ |
| 50 | |
| 51 | private final String ERROR_ATTRIBUTE_NOT_FOUND = "Error : Impossible to find the attribute"; //$NON-NLS-1$ |
| 52 | private final String ERROR_INVALID_STATE_VALUE = "Error : Invalid state value"; //$NON-NLS-1$ |
| 53 | private final String ERROR_INVALID_TIMESTAMP = "Error : Invalid timestamp"; //$NON-NLS-1$ |
| 54 | |
| 55 | /** |
| 56 | * Constructor |
| 57 | * |
| 58 | * @param resolution |
| 59 | * the mipmap resolution array (max, min, avg) |
| 60 | * @param type |
| 61 | * the type of value to use |
| 62 | */ |
| 63 | public TmfMipmapStateProviderStub(int resolution, ITmfStateValue.Type type) { |
| 64 | super(new TmfTraceStub(), MIPMAP_ID); |
| 65 | this.resolution = resolution; |
| 66 | this.type = type; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | protected void eventHandle(ITmfEvent ev) { |
| 71 | ITmfStateSystemBuilder ss = checkNotNull(getStateSystemBuilder()); |
| 72 | final long ts = ev.getTimestamp().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); |
| 73 | try { |
| 74 | int quark = ss.getQuarkAbsoluteAndAdd(TEST_ATTRIBUTE_NAME); |
| 75 | ITmfStateValue value = (ITmfStateValue) ev.getContent().getValue(); |
| 76 | modifyMipmapAttribute(ts, value, quark, MIN | MAX | AVG, resolution); |
| 77 | } catch (TimeRangeException e) { |
| 78 | Activator.logError(ERROR_INVALID_TIMESTAMP, e); |
| 79 | } catch (AttributeNotFoundException e) { |
| 80 | Activator.logError(ERROR_ATTRIBUTE_NOT_FOUND, e); |
| 81 | } catch (StateValueTypeException e) { |
| 82 | Activator.logError(ERROR_INVALID_STATE_VALUE, e); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public int getVersion() { |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public TmfMipmapStateProviderStub getNewInstance() { |
| 93 | return new TmfMipmapStateProviderStub(resolution, type); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param time |
| 98 | * The event type |
| 99 | * @param longVal |
| 100 | * The event value or null |
| 101 | * @return A new TmfEvent |
| 102 | */ |
| 103 | public @NonNull ITmfEvent createEvent(long time, Long longVal) { |
| 104 | ITmfStateValue value; |
| 105 | if (longVal == null) { |
| 106 | value = TmfStateValue.nullValue(); |
| 107 | } else if (type == ITmfStateValue.Type.LONG) { |
| 108 | value = TmfStateValue.newValueLong(longVal); |
| 109 | } else if (type == ITmfStateValue.Type.INTEGER) { |
| 110 | value = TmfStateValue.newValueInt(longVal.intValue()); |
| 111 | } else if (type == ITmfStateValue.Type.DOUBLE) { |
| 112 | value = TmfStateValue.newValueDouble(longVal.doubleValue()); |
| 113 | } else { |
| 114 | value = TmfStateValue.nullValue(); |
| 115 | } |
| 116 | ITmfTimestamp timestamp = new TmfNanoTimestamp(time); |
| 117 | ITmfEventType eventType = new TmfEventType(MIPMAP_ID, null); |
| 118 | ITmfEventField content = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, value, null); |
| 119 | ITmfEvent event = new TmfEvent(null, ITmfContext.UNKNOWN_RANK, timestamp, eventType, content); |
| 120 | return event; |
| 121 | } |
| 122 | } |