create changelog entry
[debian/openrocket] / core / src / de / congrace / exp4j / CalculationToken.java
1 /*
2    Copyright 2011 frank asseg
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  */
17 package de.congrace.exp4j;
18
19 import java.util.Arrays;
20 import java.util.Stack;
21
22 abstract class CalculationToken extends Token {
23
24         CalculationToken(String value) {
25                 super(value);
26         }
27
28         abstract void mutateStackForCalculation(Stack<Variable> stack, VariableSet variables);
29
30         /*
31          * Given an array of variables, check if any are arrays and if so expand any other of the given variables to arrays of the same length.
32          * Doubles are turned into arrays of all the same value as original. Arrays of other lengths are padded with zeros. 
33          */
34         public Variable[] expandVariables(Variable[] values){
35                 // Check if any variables have preferred representation as arrays
36                 int maxLength = 0;
37                 for (Variable v : values){
38                         if (v.getPrimary() == Variable.Primary.ARRAY && v.getArrayValue().length > maxLength){
39                                 maxLength = v.getArrayValue().length;
40                         }
41                 }
42                 
43                 // if necessary, expand any non-array variables to maximum length
44                 if (maxLength > 0) {
45                         for (int n = 0; n<values.length; n++){
46                                 Variable v = values[n];
47                                 if (v.getPrimary() == Variable.Primary.DOUBLE){
48                                         double[] a = new double[maxLength];
49                                         Arrays.fill(a, v.getDoubleValue());
50                                         values[n] = new Variable(v.getName(), a);
51                                 }
52                                 else if (v.getPrimary() == Variable.Primary.ARRAY){
53                                         // inlining Arrays.copyOf to provide compatibility with Froyo
54                                         double[] a = new double[maxLength];
55                                         int i = 0;
56                                         double[] vArrayValues = v.getArrayValue();
57                                         while( i < vArrayValues.length && i < maxLength ) {
58                                                 a[i] = vArrayValues[i];
59                                                 i++;
60                                         }
61                                         while ( i< maxLength ) {
62                                                 a[i] = 0.0;
63                                                 i++;
64                                         }
65                                         values[n] = new Variable(v.getName(), a);
66                                 } 
67                                 else {
68                                         // Should not happen, if it does return invalid variable
69                                         return new Variable[] { new Variable("Invalid")};
70                                 }
71                         }
72                 }
73                 
74                 return values;
75         }
76 }