Commit | Line | Data |
---|---|---|
c068a752 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2013, 2014 École Polytechnique de Montréal |
c068a752 GB |
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 | * Contributors: | |
10 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.tests.stubs.analysis; |
c068a752 GB |
14 | |
15 | import org.eclipse.core.runtime.IProgressMonitor; | |
ba27dd38 | 16 | import org.eclipse.jdt.annotation.NonNull; |
2bdf0193 AM |
17 | import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule; |
18 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
c068a752 GB |
19 | |
20 | /** | |
21 | * Simple analysis type for test | |
22 | */ | |
23 | public class TestAnalysis extends TmfAbstractAnalysisModule { | |
24 | ||
c068a752 GB |
25 | /** |
26 | * Test parameter. If set, simulate cancellation | |
27 | */ | |
ba27dd38 GB |
28 | public static final @NonNull String PARAM_TEST = "test"; |
29 | ||
30 | private int fOutput = 0; | |
c068a752 GB |
31 | |
32 | /** | |
33 | * Constructor | |
34 | */ | |
35 | public TestAnalysis() { | |
36 | super(); | |
37 | } | |
38 | ||
39 | @Override | |
40 | public boolean canExecute(ITmfTrace trace) { | |
41 | return true; | |
42 | } | |
43 | ||
44 | @Override | |
45 | protected boolean executeAnalysis(final IProgressMonitor monitor) { | |
3127c6b8 MK |
46 | Object parameter = getParameter(PARAM_TEST); |
47 | if (!(parameter instanceof Integer)) { | |
c068a752 GB |
48 | throw new RuntimeException("The parameter should be set"); |
49 | } | |
3127c6b8 | 50 | int integer = ((Integer) parameter).intValue(); |
c068a752 | 51 | /* If PARAM_TEST is set to 0, simulate cancellation */ |
3127c6b8 MK |
52 | fOutput = integer; |
53 | if (integer == 0) { | |
c068a752 | 54 | return false; |
3127c6b8 | 55 | } else if (integer == 999) { |
c068a752 GB |
56 | /* just stay in an infinite loop until cancellation */ |
57 | while (!monitor.isCanceled()) { | |
3127c6b8 MK |
58 | try { |
59 | Thread.sleep(0); | |
60 | } catch (InterruptedException e) { | |
61 | break; | |
62 | } | |
c068a752 GB |
63 | } |
64 | return !monitor.isCanceled(); | |
65 | } | |
c068a752 GB |
66 | return true; |
67 | } | |
68 | ||
69 | @Override | |
70 | protected void canceling() { | |
ba27dd38 | 71 | fOutput = -1; |
c068a752 GB |
72 | } |
73 | ||
74 | @Override | |
3127c6b8 | 75 | public synchronized Object getParameter(String name) { |
c068a752 GB |
76 | Object value = super.getParameter(name); |
77 | if ((value != null) && name.equals(PARAM_TEST) && (value instanceof String)) { | |
0126a8ca | 78 | return Integer.decode((String) value); |
c068a752 GB |
79 | } |
80 | return value; | |
81 | } | |
82 | ||
83 | @Override | |
84 | protected void parameterChanged(String name) { | |
85 | schedule(); | |
86 | } | |
87 | ||
88 | /** | |
89 | * Get the analysis output value | |
90 | * | |
91 | * @return The analysis output | |
92 | */ | |
93 | public int getAnalysisOutput() { | |
ba27dd38 | 94 | return fOutput; |
c068a752 GB |
95 | } |
96 | ||
97 | } |