bfc692ac6d7604535b50455d24f122452d98d6b6
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / stubs / org / eclipse / tracecompass / tmf / tests / stubs / analysis / TestStateSystemProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2016 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made 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
10 package org.eclipse.tracecompass.tmf.tests.stubs.analysis;
11
12 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.util.concurrent.locks.Condition;
15 import java.util.concurrent.locks.Lock;
16 import java.util.concurrent.locks.ReentrantLock;
17
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
21 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
22 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
23 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
24 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
25 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
26 import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider;
27 import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider;
28 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
29
30 /**
31 * Stub test provider for test state system analysis module
32 *
33 * @author Geneviève Bastien
34 */
35 public class TestStateSystemProvider extends AbstractTmfStateProvider {
36
37 /**
38 * This interface allows unit tests to provide only the event handling part
39 * of the state provider, without having to extend the analysis and the
40 * classes
41 */
42 @FunctionalInterface
43 public static interface TestStateProviderHandler {
44 /**
45 * Handles the event
46 *
47 * @param ss
48 * The state system builder
49 * @param event
50 * The event to handler
51 * @return <code>true</code> if everything went fine, or <code>false</code> to cancel
52 */
53 boolean eventHandle(@NonNull ITmfStateSystemBuilder ss, ITmfEvent event);
54 }
55
56 private static final int VERSION = 1;
57 private static final String fString = "[]";
58 private static int fCount = 0;
59 private static final @NonNull TestStateProviderHandler DEFAULT_HANDLER = (ss, event) -> {
60 /* Just need something to fill the state system */
61 if (fString.equals(event.getContent().getValue())) {
62 try {
63 int quarkId = ss.getQuarkAbsoluteAndAdd("String");
64 int quark = ss.getQuarkRelativeAndAdd(quarkId, fString);
65 ss.modifyAttribute(event.getTimestamp().getValue(), TmfStateValue.newValueInt(fCount++), quark);
66 } catch (TimeRangeException | AttributeNotFoundException | StateValueTypeException e) {
67
68 }
69 }
70 return true;
71 };
72 private static @NonNull TestStateProviderHandler sfHandler = DEFAULT_HANDLER;
73
74 /**
75 * Set the event handler for the state provider
76 *
77 * @param handler
78 * The class containing the event handler for this state provider
79 */
80 public static void setEventHandler(TestStateProviderHandler handler) {
81 if (handler == null) {
82 sfHandler = DEFAULT_HANDLER;
83 return;
84 }
85 sfHandler = handler;
86 }
87
88 private final Lock fLock = new ReentrantLock();
89 private @Nullable Condition fNextEventSignal = null;
90
91 /**
92 * Constructor
93 *
94 * @param trace
95 * The LTTng 2.0 kernel trace directory
96 */
97 public TestStateSystemProvider(@NonNull ITmfTrace trace) {
98 super(trace, "Stub State System");
99 }
100
101 @Override
102 public int getVersion() {
103 return VERSION;
104 }
105
106 @Override
107 public ITmfStateProvider getNewInstance() {
108 return new TestStateSystemProvider(this.getTrace());
109 }
110
111 @Override
112 protected void eventHandle(ITmfEvent event) {
113 ITmfStateSystemBuilder ss = checkNotNull(getStateSystemBuilder());
114 sfHandler.eventHandle(ss, event);
115 }
116
117
118
119 @Override
120 public void processEvent(@NonNull ITmfEvent event) {
121 fLock.lock();
122 try {
123 Condition cond = fNextEventSignal;
124 if (cond != null) {
125 cond.await();
126 }
127 } catch (InterruptedException e) {
128
129 } finally {
130 super.processEvent(event);
131 fLock.unlock();
132 }
133 }
134
135 /**
136 * Set the processing of event to be one event at a time instead of the
137 * default behavior. It will block until the next call to
138 * {@link #signalNextEvent()} method call.
139 *
140 * @param throttleEvent
141 * Whether to wait for a signal to process the next event
142 */
143 public void setThrottling(boolean throttleEvent) {
144 fLock.lock();
145 try {
146 if (throttleEvent) {
147 Condition cond = fNextEventSignal;
148 // If called for the first time, create a condition
149 if (cond == null) {
150 cond = fLock.newCondition();
151 fNextEventSignal = cond;
152 }
153
154 } else {
155 Condition cond = fNextEventSignal;
156 if (cond != null) {
157 fNextEventSignal = null;
158 cond.signalAll();
159 }
160 }
161 } finally {
162 fLock.unlock();
163 }
164
165 }
166
167 /**
168 * Signal for the next event to be processed. Calling this method makes
169 * sense only if {@link #setThrottling(boolean)} has been set to true
170 */
171 public void signalNextEvent() {
172 fLock.lock();
173 try {
174 Condition cond = fNextEventSignal;
175 if (cond != null) {
176 cond.signalAll();
177 }
178 } finally {
179 fLock.unlock();
180 }
181 }
182
183 }
This page took 0.037827 seconds and 5 git commands to generate.