| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013, 2014 É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 | * Contributors: |
| 10 | * Geneviève Bastien - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.tmf.tests.stubs.analysis; |
| 14 | |
| 15 | import org.eclipse.core.runtime.IProgressMonitor; |
| 16 | import org.eclipse.jdt.annotation.NonNull; |
| 17 | import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule; |
| 18 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 19 | |
| 20 | /** |
| 21 | * Simple analysis type for test |
| 22 | */ |
| 23 | public class TestAnalysis extends TmfAbstractAnalysisModule { |
| 24 | |
| 25 | /** |
| 26 | * Test parameter. If set, simulate cancellation |
| 27 | */ |
| 28 | public static final @NonNull String PARAM_TEST = "test"; |
| 29 | |
| 30 | private int fOutput = 0; |
| 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) { |
| 46 | if (getParameter(PARAM_TEST) == null) { |
| 47 | throw new RuntimeException("The parameter should be set"); |
| 48 | } |
| 49 | /* If PARAM_TEST is set to 0, simulate cancellation */ |
| 50 | if ((Integer) getParameter(PARAM_TEST) == 0) { |
| 51 | fOutput = 0; |
| 52 | return false; |
| 53 | } else if ((Integer) getParameter(PARAM_TEST) == 999) { |
| 54 | /* just stay in an infinite loop until cancellation */ |
| 55 | while (!monitor.isCanceled()) { |
| 56 | |
| 57 | } |
| 58 | return !monitor.isCanceled(); |
| 59 | } |
| 60 | Object obj = getParameter(PARAM_TEST); |
| 61 | if (obj == null) { |
| 62 | throw new IllegalStateException(); |
| 63 | } |
| 64 | fOutput = (Integer) obj; |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | protected void canceling() { |
| 70 | fOutput = -1; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Object getParameter(String name) { |
| 75 | Object value = super.getParameter(name); |
| 76 | if ((value != null) && name.equals(PARAM_TEST) && (value instanceof String)) { |
| 77 | return Integer.decode((String) value); |
| 78 | } |
| 79 | return value; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected void parameterChanged(String name) { |
| 84 | schedule(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get the analysis output value |
| 89 | * |
| 90 | * @return The analysis output |
| 91 | */ |
| 92 | public int getAnalysisOutput() { |
| 93 | return fOutput; |
| 94 | } |
| 95 | |
| 96 | } |