| 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package de.fzi.se.accuracy.transformation; |
| 5 | |
| 6 | import org.apache.log4j.Logger; |
| 7 | |
| 8 | import de.fzi.se.quality.qualityannotation.ExactlyAsSpecifiedPrecision; |
| 9 | import de.fzi.se.quality.qualityannotation.LimitedDeviationPrecision; |
| 10 | import de.fzi.se.quality.qualityannotation.NoPrecision; |
| 11 | import de.fzi.se.quality.qualityannotation.util.QualityAnnotationSwitch; |
| 12 | |
| 13 | import static java.lang.Math.*; |
| 14 | |
| 15 | /**Provides the minimum long value for a given absolute long value and validation precision. |
| 16 | * @author groenda |
| 17 | * |
| 18 | */ |
| 19 | public class ValidationPrecisionToMinimumLongValue extends |
| 20 | QualityAnnotationSwitch<Long> { |
| 21 | /** Logger for this class. */ |
| 22 | private static final Logger logger = Logger.getLogger(PCMRandomVariableSpecificationAccuracyMinimumTrafo.class); |
| 23 | |
| 24 | /** Absolute value used to calculate the minimum value. */ |
| 25 | private Long absoluteValue; |
| 26 | |
| 27 | /**Sets the absolute value. |
| 28 | * Value must be set prior to calling {@link #doSwitch(org.eclipse.emf.ecore.EObject)}. |
| 29 | * Value is reset after an invocation of {@link #doSwitch(org.eclipse.emf.ecore.EObject)}. |
| 30 | * @param absoluteValue Absolute value. |
| 31 | */ |
| 32 | public void setAbsoluteValue(Long absoluteValue) { |
| 33 | this.absoluteValue = absoluteValue; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public Long caseNoPrecision(NoPrecision object) { |
| 38 | checkAbsoluteValue(); |
| 39 | absoluteValue = null; |
| 40 | return Long.MIN_VALUE; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public Long caseExactlyAsSpecifiedPrecision( |
| 45 | ExactlyAsSpecifiedPrecision object) { |
| 46 | checkAbsoluteValue(); |
| 47 | long abs = absoluteValue; |
| 48 | absoluteValue = null; |
| 49 | return abs; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Long caseLimitedDeviationPrecision(LimitedDeviationPrecision object) { |
| 54 | checkAbsoluteValue(); |
| 55 | long min = min((long) (absoluteValue-ceil(object.getAbsolute())), |
| 56 | (long) (absoluteValue-ceil(((double)absoluteValue*object.getRelative())))); |
| 57 | absoluteValue = null; |
| 58 | return min; |
| 59 | } |
| 60 | |
| 61 | /**Ensure that the absolute value is not null. |
| 62 | */ |
| 63 | private void checkAbsoluteValue() { |
| 64 | if (absoluteValue == null) { |
| 65 | String msg = "You have to set the absolute value before invoking doSwitch(). Note: The absolute value is reset after an execution of doSwitch() to prevent accidental re-use."; |
| 66 | logger.error(msg); |
| 67 | throw new IllegalArgumentException(msg); |
| 68 | } |
| 69 | } |
| 70 | } |