resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/queries/operators/MatemathicsOperator.java

78 lines
2.3 KiB
Java

package org.gcube.informationsystem.resourceregistry.queries.operators;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author Luca Frosini (ISTI - CNR)
*
* OrientDB supports the eval() function to execute complex operations. Example:
*
* SELECT eval( "amount * 120 / 100 - discount" ) as finalPrice from Order
*
* See https://www.orientdb.com/docs/3.0.x/sql/SQL-Where.html#mathematics-operators
* https://www.orientdb.com/docs/3.0.x/sql/SQL-Syntax.html#math-operators
* https://www.orientdb.com/docs/3.0.x/sql/SQL-Syntax.html#math-operators-precedence
* https://www.orientdb.com/docs/3.0.x/sql/SQL-Syntax.html#array-concatenation
*/
public enum MatemathicsOperator {
SUM("_sum", " + ", ""),
MINUS("_minus", " - ", ""),
MULTIPLY("_multiply", " * ", ""),
DIVIDE("_divide", " / ", ""),
MODULE("_mod", " % ", ""),
BITWISE_RIGHT_SHIFT("_bitrshift", " >> ", ""),
BITWISE_LEFT_SHIFT("_bitlshift", " << ", ""),
BITWISE_AND("_bitand", " & ", ""),
BITWISE_OR("_bitor", " | ", ""),
BITWISE_XOR("_bitxor", " ^ ", ""),
ARRAY_CONCATENATION("_arrayconcat", " || ", "");
protected final String operator;
protected final String matemathicsOperator;
protected final String description;
private MatemathicsOperator(String operator, String matemathicsOperator, String description) {
this.operator = operator;
this.matemathicsOperator = matemathicsOperator;
this.description = description;
}
public String getOperator() {
return operator;
}
public String getMatemathicsOperator() {
return matemathicsOperator;
}
public String getDescription() {
return description;
}
private static Set<String> operators;
private static Map<String,MatemathicsOperator> operatorByKey;
static {
MatemathicsOperator.operators = new HashSet<>();
MatemathicsOperator.operatorByKey = new HashMap<>();
for(MatemathicsOperator queryLogicalOperator : MatemathicsOperator.values()) {
MatemathicsOperator.operators.add(queryLogicalOperator.getOperator());
MatemathicsOperator.operatorByKey.put(queryLogicalOperator.getOperator(), queryLogicalOperator);
}
}
public static Set<String> getOperators() {
return MatemathicsOperator.operators;
}
public static MatemathicsOperator getQueryLogicalOperator(String key) {
return operatorByKey.get(key);
}
}