Debian Codegraph @

src/main/java/net/sf/antcontrib/math/Numeric.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.antcontrib.math;
17
18 import org.apache.tools.ant.BuildException;
19
20
21 /**
22 * A numeric value that implements Evaluateable.
23 *
24 * @author inger
25 */
26
27
28 public class Numeric
29 implements Evaluateable
30 {
31 private String datatype;
32 private String value;
33
34 /**
35 * Set the value for this number. This string must parse to the set
36 * datatype, for example, setting value to "7.992" and datatype to INT
37 * will cause a number format exception to be thrown. Supports two special
38 * numbers, "E" and "PI".
39 *
40 * @param value the value for this number
41 */
42 public void setValue(String value)
43 {
44 if (value.equals("E"))
45 value = String.valueOf(java.lang.Math.E);
46 else if (value.equals("PI"))
47 value = String.valueOf(java.lang.Math.PI);
48 this.value = value;
49 }
50
51 /**
52 * @return the value for this number as a Number. Cast as appropriate to
53 * Integer, Long, Float, or Double.
54 */
55 public Number evaluate()
56 {
57 if (datatype == null)
58 datatype = "double";
59 if (datatype.equals("int"))
60 return new Integer(value);
61 if (datatype.equals("long"))
62 return new Long(value);
63 if (datatype.equals("float"))
64 return new Float(value);
65 if (datatype.equals("double"))
66 return new Double(value);
67 throw new BuildException("Invalid datatype.");
68 }
69
70 /**
71 * Sets the datatype of this number. Allowed values are
72 * "int", "long", "float", or "double".
73 */
74 public void setDatatype(String p)
75 {
76 datatype = p;
77 }
78
79 /**
80 * @return the datatype as one of the defined types.
81 */
82 public String getDatatype()
83 {
84 if (datatype == null)
85 datatype = "double";
86 return datatype;
87 }
88
89 public String toString()
90 {
91 return "Numeric[value=" + value
92 + ";datatype=" + datatype
93 + "]";
94 }
95 }