1 | package de.uka.ipd.sdq.simucomframework.variables.functions; |
2 | |
3 | import java.util.List; |
4 | |
5 | import org.apache.log4j.Logger; |
6 | |
7 | /**Implements the minimum function as in @link{@link Math}, e.g. {@link Math#min(double, double)}. |
8 | * @author groenda |
9 | */ |
10 | public class MinFunction implements IFunction { |
11 | /** Logger for this class. */ |
12 | private static final Logger logger = Logger.getLogger(MinFunction.class); |
13 | |
14 | /** Name used in the stochastic expression for this function. */ |
15 | public static final String MIN_FUNCTION_NAME = "Min"; |
16 | |
17 | /* (non-Javadoc) |
18 | * @see de.uka.ipd.sdq.simucomframework.variables.functions.IFunction#checkParameters(java.util.List) |
19 | */ |
20 | public boolean checkParameters(List<Object> parameters) { |
21 | if (parameters.size() != 2) |
22 | return false; |
23 | boolean valid = false; |
24 | valid = parameterIsNumber(parameters.get(0)) || parameters.get(0) instanceof String; |
25 | valid &= parameterIsNumber(parameters.get(1)); |
26 | valid &= parameters.get(0).getClass().isInstance(parameters.get(1)); |
27 | return valid; |
28 | } |
29 | |
30 | /**Checks if the given parameter is a number. |
31 | * Valid numbers are of the types {@link Integer}, {@link Long}, {@link Float}, and {@link Double}. |
32 | * @param obj Parameter. |
33 | * @return {@code true} if, and only if, the parameter is a number. |
34 | */ |
35 | private boolean parameterIsNumber(Object obj) { |
36 | boolean valid = false; |
37 | if (obj instanceof Integer |
38 | || obj instanceof Long |
39 | || obj instanceof Float |
40 | || obj instanceof Double) { |
41 | valid = true; |
42 | } |
43 | return valid; |
44 | } |
45 | |
46 | /* (non-Javadoc) |
47 | * @see de.uka.ipd.sdq.simucomframework.variables.functions.IFunction#evaluate(java.util.List) |
48 | */ |
49 | public Object evaluate(List<Object> parameters) { |
50 | if (parameters.get(0) instanceof Double || parameters.get(1) instanceof Double) { |
51 | return Math.min((Double) parameters.get(0), (Double) parameters.get(1)); |
52 | } else if (parameters.get(0) instanceof Long || parameters.get(1) instanceof Long) { |
53 | return Math.min((Long) parameters.get(0), (Long) parameters.get(1)); |
54 | } else if (parameters.get(0) instanceof Integer || parameters.get(1) instanceof Integer) { |
55 | return Math.min((Integer) parameters.get(0), (Integer) parameters.get(1)); |
56 | } else if (parameters.get(0) instanceof Float || parameters.get(1) instanceof Float) { |
57 | return Math.min((Float) parameters.get(0), (Float) parameters.get(1)); |
58 | } else if (parameters.get(0) instanceof String) { |
59 | logger.debug("String/Enum-based parameter was provided to Min function. The parameter will be returned directly."); |
60 | return parameters.get(0); |
61 | } else { |
62 | throw new IllegalArgumentException("Both parameters must have the same type and have one of the type Integer, Long, Float, or Double."); |
63 | } |
64 | } |
65 | |
66 | } |