ParsedFunction defines a function of a single varianble using a String. This function is immutable. That is, once an instance is created with a particular function string, the function cannot be changed. Because immutable objects cannot change, they are thread safe and can be freely shared in a J
| 18 | * @author Wolfgang Christian |
| 19 | */ |
| 20 | public final class ParsedFunction implements Function { |
| 21 | private final String fStr; |
| 22 | private final Function function; |
| 23 | |
| 24 | /** |
| 25 | * Constructs a function x with from the given string. |
| 26 | * |
| 27 | * @param fStr the function |
| 28 | * @throws ParserException |
| 29 | */ |
| 30 | public ParsedFunction(String fStr) throws ParserException { |
| 31 | this(fStr, "x"); //$NON-NLS-1$ |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Constructs a ParsedFunction from the given string and independent variable. |
| 36 | * |
| 37 | * @param _fStr the function |
| 38 | * @param var the independent variable |
| 39 | * @throws ParserException |
| 40 | */ |
| 41 | public ParsedFunction(String _fStr, String var) throws ParserException { |
| 42 | fStr = _fStr; |
| 43 | SuryonoParser parser = null; |
| 44 | parser = new SuryonoParser(fStr, var); |
| 45 | function = parser; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Evaluates the function, f. |
| 50 | * |
| 51 | * @param x the value of the independent variable |
| 52 | * |
| 53 | * @return the value of the function |
| 54 | */ |
| 55 | @Override |
| 56 | public double evaluate(double x) { |
| 57 | return function.evaluate(x); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Represents the function as a string. |
| 62 | * |
| 63 | * @return the string |
| 64 | */ |
| 65 | @Override |
| 66 | public String toString() { |
| 67 | return "f(x) = "+fStr; //$NON-NLS-1$ |
| 68 | } |
| 69 | |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Open Source Physics software is free software; you can redistribute |
nothing calls this directly
no outgoing calls
no test coverage detected