模块 jmc.utils
程序包 com.jmc.lang

接口 Operator<T>

类型参数:
T - 具有运算符重载的类型

public interface Operator<T>
运算符重载
从以下版本开始:
2.0
API Note:

 // 书写一个Int类,重载加法,减法和大于运算符
 class Int implements Operator<Int> {
     int value;

     public Int(int value) { this.value = value; }

     @Override
     public Int plus(Int other) {
         return new Int(this.value + other.value);
     }

     @Override
     public Int minus(Int other) {
         return new Int(this.value - other.value);
     }

     @Override
     public boolean greaterThan(Int other) {
         return this.value > other.value;
     }

     public int getValue() {
         return this.value;
     }
 }

 Int a = new Int(3), b = new Int(4);

 // 二元运算
 // c = a + b
 Int c = Operator.calc(a, "+", b);
 Assert.assertEquals(7, c.getValue());

 // 二元运算
 // c = a - b
 c = Operator.calc(a, "-", b);
 Assert.assertEquals(-1, c.getValue());

 // 二元表达式运算
 // c = a + (b - a)
 c = Operator.calc("? + (? - ?)", a, b, a);
 Assert.assertEquals(4, c.getValue());

 // 布尔值运算
 // flag = a > b
 boolean flag = Operator.cmp(a, ">", b);
 Assert.assertFalse(flag);

 // 布尔表达式运算
 // flag = b > a && b - a > a
 flag = Operator.cmp("? > ? && ? - ? > ?", b, a, b, a, a);
 Assert.assertFalse(flag);

 
  • 方法概要

    修饰符和类型
    方法
    说明
    default boolean
    and(T other)
    重载运算符 &&
    default T
    bitAnd(T other)
    重载运算符 &
    default T
    bitAndAssign(T other)
    重载运算符 &=
    default T
    bitOr(T other)
    重载运算符 |
    default T
    bitOrAssign(T other)
    重载运算符 |=
    default T
    重载运算符 ~
    default T
    bitXor(T other)
    重载运算符 ^
    default T
    bitXorAssign(T other)
    重载运算符 ^=
    static <T extends Operator<T>>
    T
    calc(String opr, T a)
    一元前置运算
    static <T extends Operator<T>>
    T
    calc(String exp, T... args)
    表达式运算(只支持二元运算)
    static <T extends Operator<T>>
    T
    calc(T a, String opr)
    一元后置运算
    static <T extends Operator<T>>
    T
    calc(T a, String opr, T b)
    二元运算
    static <T extends Operator<T>>
    boolean
    cmp(String opr, T a)
    一元前置布尔值运算
    static <T extends Operator<T>>
    boolean
    cmp(String exp, T... args)
    表达式布尔运算(只支持二元运算)
    static <T extends Operator<T>>
    boolean
    cmp(T a, String opr, T b)
    二元布尔值运算
    default T
    重载运算符 --(后置)
    default T
    重载运算符 --(前置)
    default T
    div(T other)
    重载运算符 /
    default T
    divAssign(T other)
    重载运算符 /=
    default boolean
    eq(T other)
    重载运算符 ==
    default boolean
    greaterEq(T other)
    重载运算符 >=
    default boolean
    greaterThan(T other)
    重载运算符 >
    default T
    重载运算符 ++(后置)
    default T
    重载运算符 ++(前置)
    default boolean
    lessEq(T other)
    重载运算符 <=
    default boolean
    lessThan(T other)
    重载运算符 <
    default T
    minus(T other)
    重载运算符 -
    default T
    minusAssign(T other)
    重载运算符 -=
    default T
    mod(T other)
    重载运算符 %
    default T
    modAssign(T other)
    重载运算符 %=
    default boolean
    not()
    重载运算符 !
    default boolean
    notEq(T other)
    重载运算符 !
    default boolean
    or(T other)
    重载运算符 ||
    default T
    plus(T other)
    重载运算符 +
    default T
    plusAssign(T other)
    重载运算符 +=
    default T
    shl(T other)
    重载运算符 <<
    default T
    shlAssign(T other)
    重载运算符 <<=
    default T
    shr(T other)
    重载运算符 >>
    default T
    shrAssign(T other)
    重载运算符 >>=
    default T
    times(T other)
    重载运算符 *
    default T
    timesAssign(T other)
    重载运算符 *=
    default T
    重载运算符 -(负号)
    default T
    重载运算符 +(正号)
    default T
    uShr(T other)
    重载运算符 >>>
    default T
    uShrAssign(T other)
    重载运算符 >>>=
  • 方法详细资料

    • plus

      default T plus(T other)
      重载运算符 +
      参数:
      other - 另一个元素
      返回:
      运算结果
    • minus

      default T minus(T other)
      重载运算符 -
      参数:
      other - 另一个元素
      返回:
      运算结果
    • times

      default T times(T other)
      重载运算符 *
      参数:
      other - 另一个元素
      返回:
      运算结果
    • div

      default T div(T other)
      重载运算符 /
      参数:
      other - 另一个元素
      返回:
      运算结果
    • mod

      default T mod(T other)
      重载运算符 %
      参数:
      other - 另一个元素
      返回:
      运算结果
    • plusAssign

      default T plusAssign(T other)
      重载运算符 +=
      参数:
      other - 另一个元素
      返回:
      运算结果
    • minusAssign

      default T minusAssign(T other)
      重载运算符 -=
      参数:
      other - 另一个元素
      返回:
      运算结果
    • timesAssign

      default T timesAssign(T other)
      重载运算符 *=
      参数:
      other - 另一个元素
      返回:
      运算结果
    • divAssign

      default T divAssign(T other)
      重载运算符 /=
      参数:
      other - 另一个元素
      返回:
      运算结果
    • modAssign

      default T modAssign(T other)
      重载运算符 %=
      参数:
      other - 另一个元素
      返回:
      运算结果
    • incPre

      default T incPre()
      重载运算符 ++(前置)
      返回:
      运算结果
    • decPre

      default T decPre()
      重载运算符 --(前置)
      返回:
      运算结果
    • incPost

      default T incPost()
      重载运算符 ++(后置)
      返回:
      运算结果
    • decPost

      default T decPost()
      重载运算符 --(后置)
      返回:
      运算结果
    • unaryPlus

      default T unaryPlus()
      重载运算符 +(正号)
      返回:
      运算结果
    • unaryMinus

      default T unaryMinus()
      重载运算符 -(负号)
      返回:
      运算结果
    • and

      default boolean and(T other)
      重载运算符 &&
      参数:
      other - 另一个元素
      返回:
      运算结果
    • or

      default boolean or(T other)
      重载运算符 ||
      参数:
      other - 另一个元素
      返回:
      运算结果
    • not

      default boolean not()
      重载运算符 !
      返回:
      运算结果
    • eq

      default boolean eq(T other)
      重载运算符 ==
      参数:
      other - 另一个元素
      返回:
      运算结果
    • notEq

      default boolean notEq(T other)
      重载运算符 !=
      参数:
      other - 另一个元素
      返回:
      运算结果
    • lessThan

      default boolean lessThan(T other)
      重载运算符 <
      参数:
      other - 另一个元素
      返回:
      运算结果
    • greaterThan

      default boolean greaterThan(T other)
      重载运算符 >
      参数:
      other - 另一个元素
      返回:
      运算结果
    • lessEq

      default boolean lessEq(T other)
      重载运算符 <=
      参数:
      other - 另一个元素
      返回:
      运算结果
    • greaterEq

      default boolean greaterEq(T other)
      重载运算符 >=
      参数:
      other - 另一个元素
      返回:
      运算结果
    • bitReverse

      default T bitReverse()
      重载运算符 ~
      返回:
      运算结果
    • bitAnd

      default T bitAnd(T other)
      重载运算符 &
      参数:
      other - 另一个元素
      返回:
      运算结果
    • bitOr

      default T bitOr(T other)
      重载运算符 |
      参数:
      other - 另一个元素
      返回:
      运算结果
    • bitXor

      default T bitXor(T other)
      重载运算符 ^
      参数:
      other - 另一个元素
      返回:
      运算结果
    • shl

      default T shl(T other)
      重载运算符 <<
      参数:
      other - 另一个元素
      返回:
      运算结果
    • shr

      default T shr(T other)
      重载运算符 >>
      参数:
      other - 另一个元素
      返回:
      运算结果
    • uShr

      default T uShr(T other)
      重载运算符 >>>
      参数:
      other - 另一个元素
      返回:
      运算结果
    • bitAndAssign

      default T bitAndAssign(T other)
      重载运算符 &=
      参数:
      other - 另一个元素
      返回:
      运算结果
    • bitOrAssign

      default T bitOrAssign(T other)
      重载运算符 |=
      参数:
      other - 另一个元素
      返回:
      运算结果
    • bitXorAssign

      default T bitXorAssign(T other)
      重载运算符 ^=
      参数:
      other - 另一个元素
      返回:
      运算结果
    • shlAssign

      default T shlAssign(T other)
      重载运算符 <<=
      参数:
      other - 另一个元素
      返回:
      运算结果
    • shrAssign

      default T shrAssign(T other)
      重载运算符 >>=
      参数:
      other - 另一个元素
      返回:
      运算结果
    • uShrAssign

      default T uShrAssign(T other)
      重载运算符 >>>=
      参数:
      other - 另一个元素
      返回:
      运算结果
    • calc

      @SafeVarargs static <T extends Operator<T>> T calc(String exp, T... args)
      表达式运算(只支持二元运算)
      类型参数:
      T - 具有运算符重载的类
      参数:
      exp - 算数表达式(?为占位符)
      args - 参数
      返回:
      表达式运算结果
      API Note:
      
       class Int implements Operator<Int> { ... }
       Int a = new Int(3), b = new Int(4);
      
       // 二元表达式运算
       // c = a + (b - a)
       c = Operator.calc("? + (? - ?)", a, b, a);
       
    • calc

      static <T extends Operator<T>> T calc(T a, String opr, T b)
      二元运算
      类型参数:
      T - 具有运算符重载的类
      参数:
      a - 元素a
      opr - 运算符opr
      b - 元素b
      返回:
      a opr b
      API Note:
      
       class Int implements Operator<Int> { ... }
       Int a = new Int(3), b = new Int(4);
      
       // 二元运算
       // c = a + b
       Int c = Operator.calc(a, "+", b);
       
    • calc

      static <T extends Operator<T>> T calc(String opr, T a)
      一元前置运算
      类型参数:
      T - 具有运算符重载的类
      参数:
      opr - 前置运算符
      a - 元素a
      返回:
      opr a
      API Note:
      
       class Int implements Operator<Int> { ... }
       Int a = new Int(3);
      
       // 一元前置运算
       // c = -a
       Int c = Operator.calc("-", a);
       
    • calc

      static <T extends Operator<T>> T calc(T a, String opr)
      一元后置运算
      类型参数:
      T - 具有运算符重载的类
      参数:
      a - 元素a
      opr - 后置运算符
      返回:
      a opr
      API Note:
      
       class Int implements Operator<Int> { ... }
       Int a = new Int(3);
      
       // 一元后置运算
       // c = a++
       Int c = Operator.calc(a, "++");
       
    • cmp

      static <T extends Operator<T>> boolean cmp(String exp, T... args)
      表达式布尔运算(只支持二元运算)
      类型参数:
      T - 具有运算符重载的类
      参数:
      exp - 算数表达式
      args - 参数
      返回:
      表达式结果布尔值
      API Note:
      
       class Int implements Operator<Int> { ... }
       Int a = new Int(3), b = new Int(4);
      
       // 布尔表达式运算
       // flag = b > a && b - a > a
       boolean flag = Operator.cmp("? > ? && ? - ? > ?", b, a, b, a, a);
       
    • cmp

      static <T extends Operator<T>> boolean cmp(T a, String opr, T b)
      二元布尔值运算
      类型参数:
      T - 具有运算符重载的类
      参数:
      a - 元素a
      opr - 运算符
      b - 元素b
      返回:
      a opr b
      API Note:
      
       class Int implements Operator<Int> { ... }
       Int a = new Int(3), b = new Int(4);
      
       // 二元布尔运算
       // flag = a > b
       boolean flag = Operator.cmp(a, ">", b);
       
    • cmp

      static <T extends Operator<T>> boolean cmp(String opr, T a)
      一元前置布尔值运算
      类型参数:
      T - 具有运算符重载的类
      参数:
      opr - 前置运算符
      a - 元素a
      返回:
      opr a
      API Note:
      
       class Int implements Operator<Int> { ... }
       Int a = new Int(3), b = new Int(4);
      
       // 一元前置布尔运算
       // flag = !a
       boolean flag = Operator.cmp("!", a);