-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExpressionBuilder.java
More file actions
234 lines (209 loc) · 8.38 KB
/
ExpressionBuilder.java
File metadata and controls
234 lines (209 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Copyright 2014 Frank Asseg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Modifications Licensing Copyright
*
* Factory class for instances.
* Copyright (C) 2021 DZ-FSDev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.dz_fs_dev.expr4j;
import com.dz_fs_dev.expr4j.function.Functions;
import com.dz_fs_dev.expr4j.function.Function;
import com.dz_fs_dev.expr4j.operator.Operator;
import com.dz_fs_dev.expr4j.shuntingyard.ShuntingYard;
import java.util.*;
/**
* Factory class for {@link Expression} instances. This class is the main API entrypoint. Users should create new
* {@link Expression} instances using this factory class.
*
* @author DZ-FSDev, Frank Asseg
* @since 17.0.1
* @version 0.0.2
*/
public class ExpressionBuilder {
private final String expression;
private final Map<String, Function> userFunctions;
private final Map<String, Operator> userOperators;
private final Set<String> variableNames;
private boolean implicitMultiplication = true;
/**
* Create a new ExpressionBuilder instance and initialize it with a given expression string.
*
* @param expression the expression to be parsed
*/
public ExpressionBuilder(String expression) {
if (expression == null || expression.trim().length() == 0) {
throw new IllegalArgumentException("Expression can not be empty");
}
this.expression = expression;
this.userOperators = new HashMap<>(4);
this.userFunctions = new HashMap<>(4);
this.variableNames = new HashSet<>(4);
}
/**
* Add a {@link net.objecthunter.exp4j.function.Function} implementation available for use in the expression
*
* @param function the custom {@link net.objecthunter.exp4j.function.Function} implementation that should be available for use in the expression.
* @return the ExpressionBuilder instance
*/
public ExpressionBuilder function(Function function) {
this.userFunctions.put(function.getName(), function);
return this;
}
/**
* Add multiple {@link net.objecthunter.exp4j.function.Function} implementations available for use in the expression
*
* @param functions the custom {@link net.objecthunter.exp4j.function.Function} implementations
* @return the ExpressionBuilder instance
*/
public ExpressionBuilder functions(Function... functions) {
for (Function f : functions) {
this.userFunctions.put(f.getName(), f);
}
return this;
}
/**
* Add multiple {@link net.objecthunter.exp4j.function.Function} implementations available for use in the expression
*
* @param functions A {@link java.util.List} of custom {@link net.objecthunter.exp4j.function.Function} implementations
* @return the ExpressionBuilder instance
*/
public ExpressionBuilder functions(List<Function> functions) {
for (Function f : functions) {
this.userFunctions.put(f.getName(), f);
}
return this;
}
/**
* Declare variable names used in the expression
*
* @param variableNames the variables used in the expression
* @return the ExpressionBuilder instance
*/
public ExpressionBuilder variables(Set<String> variableNames) {
this.variableNames.addAll(variableNames);
return this;
}
/**
* Declares variable names used in the expression.
*
* @param variableNames The variables used in the expression.
* @return The ExpressionBuilder instance.
*/
public ExpressionBuilder variables(String... variableNames) {
Collections.addAll(this.variableNames, variableNames);
return this;
}
/**
* Declares a variable used in the expression.
*
* @param variableName The variable used in the expression.
* @return The ExpressionBuilder instance.
*/
public ExpressionBuilder variable(String variableName) {
this.variableNames.add(variableName);
return this;
}
/**
* Toggles implicit multiplication mode. When enabled, variables next to coefficients
* will be evaluated as a product without requiring an explicit multiplication operator.
*
* @param enabled The mode to toggle implicit multiplication to.
* @return The ExpressionBuilder instance.
*/
public ExpressionBuilder implicitMultiplication(boolean enabled) {
this.implicitMultiplication = enabled;
return this;
}
/**
* Add an {@link net.objecthunter.exp4j.operator.Operator} which should be available for use in the expression
*
* @param operator the custom {@link net.objecthunter.exp4j.operator.Operator} to add
* @return the ExpressionBuilder instance
*/
public ExpressionBuilder operator(Operator operator) {
this.checkOperatorSymbol(operator);
this.userOperators.put(operator.getSymbol(), operator);
return this;
}
private void checkOperatorSymbol(Operator op) {
String name = op.getSymbol();
for (char ch : name.toCharArray()) {
if (!Operator.isAllowedOperatorChar(ch)) {
throw new IllegalArgumentException("The operator symbol '" + name + "' is invalid");
}
}
}
/**
* Add multiple {@link net.objecthunter.exp4j.operator.Operator} implementations which should be available for use in the expression
*
* @param operators the set of custom {@link net.objecthunter.exp4j.operator.Operator} implementations to add
* @return the ExpressionBuilder instance
*/
public ExpressionBuilder operator(Operator... operators) {
for (Operator o : operators) {
this.operator(o);
}
return this;
}
/**
* Add multiple {@link net.objecthunter.exp4j.operator.Operator} implementations which should be available for use in the expression
*
* @param operators the {@link java.util.List} of custom {@link net.objecthunter.exp4j.operator.Operator} implementations to add
* @return the ExpressionBuilder instance
*/
public ExpressionBuilder operator(List<Operator> operators) {
for (Operator o : operators) {
this.operator(o);
}
return this;
}
/**
* Build the {@link Expression} instance using the custom operators and functions set.
*
* @return an {@link Expression} instance which can be used to evaluate the result of the expression
*/
public Expression build() {
if (expression.length() == 0) {
throw new IllegalArgumentException("The expression can not be empty");
}
/* set the constants' varibale names */
variableNames.add("pi");
variableNames.add("π");
variableNames.add("e");
variableNames.add("φ");
/* Check if there are duplicate vars/functions */
for (String var : variableNames) {
if (Functions.getBuiltinFunction(var) != null || userFunctions.containsKey(var)) {
throw new IllegalArgumentException("A variable can not have the same name as a function [" + var + "]");
}
}
return new Expression(ShuntingYard.convertToRPN(this.expression, this.userFunctions, this.userOperators,
this.variableNames, this.implicitMultiplication), this.userFunctions.keySet());
}
}