Skip to content

Computing SimpleUsage

anymaker edited this page Apr 3, 2021 · 1 revision

First you need to create an instance of a2u.tn.utils.computer.calcobj.ObjCalcEngine. ObjCalcEngine is a very configurable class. Therefore, creating an instance of it is hard work and requires a lot of memory and processor. Do not create more instances than necessary. Reuse what you have. Place an instance in a static field or singleton class.

import a2u.tn.utils.computer.calcobj.ObjCalcEngine;

public class Main {
  private static ObjCalcEngine engine = new ObjCalcEngine();
  ...

Quite simply

In the simplest case, you will need to calculate using a previously created formula. This formula can be obtained from a database or from another source, and can contain all the values needed to calculate.

(1+2)*5 + 2*(3+2)

You can use one of four methods to calculate:

Object calc(String query)
Object calc(Formula formula)
<T> T calc(String query, Class<? extends T> cls)
<T> T calc(Formula formula, Class<? extends T> cls)

The query parameter is a string formula of type java.util.String.
The formula parameter is a pre-parsed formula string. Use this method if the formula never changes.
The cls parameter indicates the type to which you want to cast the total value. Conversion takes place in accordance with the configured converters of type.

Example

// simple
long resultSimple = (Long) engine.calc("(1+2)*5 + 2*(3+2)");

// pre-defined
Formula formula = new Formula("(1+2)*5 + 2*(3+2)");
long resultPredefined = (Long) engine.calc(formula);

// casted result
int resultCastedInt = engine.calc(formula, Integer.class);
String resultCastedString = engine.calc(formula, String.class);

Calculating with extract values by code from start object

In this case, you probably already have an object containing all the data required for the calculation. It can be any type of object, pojo, map, or collection. The main thing is that it contains fields with any access modifier containing the necessary data.

class DataDTO {
  long id;
  String title;
  Object Value;
}
Map<String, Object> dto = new HashMap<String, Object>();
dto.put("id", 123);
dto.put("title", "Object title");
dto.put("value", 54321);

You can run calculation with the following methods:

Object calc(String query, Object startObj)
Object calc(Formula formula, Object startObj)
<T> T calc(String query, Object startObj, Class<? extends T> cls)
<T> T calc(Formula formula, Object startObj, Class<? extends T> cls)

Here the startObj parameter has been added in which you need to pass the start object.

Map<String, Object> dto = new HashMap<String, Object>();
dto.put("id", 123);
dto.put("title", "Object title");
dto.put("value", 54321);
String result = engine.calc("  toString(.id) +'('+ .title +')='+ .value  ", dto, String.class);
System.out.println(result);

Output

123(Object title)=54321

Full example

Calculating with extract values by code from any objects such as plain object and Map or List instances

import a2u.tn.utils.computer.calcobj.ObjCalcEngine;

public class Main {
  private static ObjCalcEngine engine = new ObjCalcEngine();

  private int     fieldA = 123;
  private String  fieldB = "BBB";
  private Main    fieldC;

  public static void main(String[] args) {
    Main startObj = new Main();
    startObj.fieldC = startObj;

    System.out.println(engine.calc("  '1: ' + .fieldA  ",                           startObj));
    System.out.println(engine.calc("  '2: ' + .fieldB  ",                           startObj));
    System.out.println(engine.calc("  '3: ' + .fieldC  ",                           startObj));
    System.out.println(engine.calc("  '4: ' + .fieldC.fieldA * 10  ",               startObj));
    System.out.println(engine.calc("  '5: ' + .fieldA + .fieldC.fieldA  ",          startObj));
    System.out.println(engine.calc("  '6: ' + (.fieldA + .fieldC.fieldA)  ",        startObj));
    System.out.println(engine.calc("  '7: ' + .fieldB + ' -> ' + .fieldC.fieldA  ", startObj));
  }

  @Override
  public String toString() {
    return fieldA + ", " + fieldB;
  }
}

Output

1: 123
2: BBB
3: 123, BBB
4: 1230
5: 123123
6: 246
7: BBB -> 123
Clone this wiki locally