create changelog entry
[debian/openrocket] / core / src / de / congrace / exp4j / Variable.java
1 package de.congrace.exp4j;
2
3 /*
4  * Represents a generic variable which can have double or array values.
5  * Optionally the start and step values corresponding to each array index can be specified for array values
6  * Tries to do something sensible if you try and apply a regular function / operator to an array
7  * and vice-versa.
8  */
9 public class Variable {
10         
11         // The primary or preferred representation 
12         public enum Primary {DOUBLE, ARRAY, PLACEHOLDER};
13         private final Primary primary;
14         
15         private final String name;
16         
17         private final double doubleValue;  
18         private final double[] arrayValue; 
19         
20         private final double start, step;
21         
22         /*
23          * Initialize a new variable with a name only. This can be used as a place holder
24          */
25         public Variable(String name){
26                 this.name = name;
27                 this.primary = Primary.PLACEHOLDER;
28                 this.doubleValue = Double.NaN;
29                 this.arrayValue = new double[] {Double.NaN};
30                 this.start = Double.NaN;
31                 this.step = Double.NaN;
32         }
33         
34         /*
35          * Initialize a new double variable
36          */
37         public Variable(String name, double d){
38                 this.doubleValue = d;
39                 this.arrayValue = new double[] {d};
40                 this.name = name;
41                 this.primary = Primary.DOUBLE;
42                 this.start = Double.NaN;
43                 this.step = Double.NaN;
44         }
45         
46         /*
47          * Initialize a new array variable
48          */
49         public Variable(String name, double[] d){
50                 this.arrayValue = d;
51                 this.doubleValue = d[0];
52                 this.name = name;
53                 this.primary = Primary.ARRAY;
54                 this.start = Double.NaN;
55                 this.step = Double.NaN;
56         }
57         
58         /*
59          * Initialize a new array variable, specifying the start and step values
60          */
61         public Variable(String name, double[] d, double start, double step){
62                 this.arrayValue = d;
63                 this.doubleValue = d[0];
64                 this.name = name;
65                 this.primary = Primary.ARRAY;
66                 this.start = start;
67                 this.step = step;
68         }
69         
70         public String getName(){
71                 return name;
72         }
73         
74         public Primary getPrimary(){
75                 return this.primary;
76         }
77         
78         public double getDoubleValue(){
79                 return doubleValue;
80         }
81         
82         public double[] getArrayValue(){
83                 return arrayValue;
84         }
85         
86         public double getStep(){
87                 return step;
88         }
89         
90         public double getStart(){
91                 return start;
92         }
93         
94         public String toString(){
95                 if ( arrayValue.length > 1 ){
96                         String out = name + " is Array (length " + new Integer(arrayValue.length).toString() + ") : {";
97                         for (double x : arrayValue){
98                                 out = out + x + ",";
99                         }
100                         out = out.substring(0, out.length()-1);
101                         return out + "}";
102                 }
103                 else{
104                         return name + " is double : {" + new Double(doubleValue).toString() + "}";
105                 }
106         }
107 }