Skip to content

Latest commit

 

History

History
executable file
·
100 lines (74 loc) · 5.39 KB

README-EN.md

File metadata and controls

executable file
·
100 lines (74 loc) · 5.39 KB

Aviator

Build Status Code Quality: Java Maven Central

📖 English Documentation | 📖 中文文档


Aviator is a lightweight, high performance expression evaluator for java. It compiles expression to byte code and evaluate it on the fly.

Feature Intro

  1. Support almost all operators, including arithmetic operators, relational operators, logical operators, bitwise operators, regular expression matching operators(=~), ternary expressions(?:).
  2. Support operator precedence, or use parentheses to specify precedence explicitly.
  3. Support assignment.
  4. Logical operators support short-circuit.
  5. Support for rich types such as nil, integers and floats, strings, regular expressions, dates, variables, etc.; Supports automatic type conversion and promotion.
  6. Support lambda anonymous functions and closures.
  7. A powerful set of commonly used function libraries is built-in.
  8. Support user-customized function, easy to extend, support function call point parameter list capture and import java class methods as custom functions.
  9. Support operator overload.
  10. Support big numbers(BigInteger) and high precision(BigDecimal) operations.
  11. Support multi-line expression and customized evaluator instance.
  12. Lightweight and high performance, offer a wide range of customization options.
  13. Function missing mechanism just like ruby's method msising.

See home page for details.

News

  • 4.2.5, an usefull feature: function missing.
  • 4.2.4, adds annotations for importing java class methods and some new features.

Dependency

<dependency>
  <groupId>com.googlecode.aviator</groupId>
  <artifactId>aviator</artifactId>
  <version>{version}</version>
</dependency>

Check available versions at search.maven.org.

Quick Start

int[] a = ...;
Map<String, Object> env = new HashMap<String, Object>();
env.put("a", a);

AviatorEvaluator.execute("1 + 2 + 3");
AviatorEvaluator.execute("a[1] + 100", env);
AviatorEvaluator.execute("'a[1]=' + a[1]", env);

// get the length of the array
AviatorEvaluator.execute("count(a)", env);

// compute the sum of the array
AviatorEvaluator.execute("reduce(a, +, 0)", env);

// check each element of array is between 0 <= e < 10.
AviatorEvaluator.execute("seq.every(a, seq.and(seq.ge(0), seq.lt(10)))", env);

// compute the sum by Lambda
AviatorEvaluator.execute("reduce(a, lambda(x,y) -> x + y end, 0)", env);

// import string instance methods
AviatorEvaluator.addInstanceFunctions("s", String.class);
AviatorEvaluator.execute("s.indexOf('hello', 'l')");
AviatorEvaluator.execute("s.replaceAll('hello', 'l', 'x')");

// import static methods
AviatorEvaluator.addStaticFunctions("sutil", StringUtils.class);
AviatorEvaluator.execute("sutil.isBlank('hello')");

// Call any public instance methods by reflection
AviatorEvaluator.setFunctionMissing(JavaMethodReflectionFunctionMissing.getInstance());
// Calling String#indexOf by reflection
System.out.println(AviatorEvaluator.execute("indexOf('hello world', 'w')"));
// Calling Long#floatValue by reflection
System.out.println(AviatorEvaluator.execute("floatValue(3)"));
// Calling BigDecimal#add by reflection
System.out.println(AviatorEvaluator.execute("add(3M, 4M)"));

See user guide for details.

Links