f7
f7 is a spreadsheet formula execution library
git clone https://git.vogt.world/f7.git
Log | Files | README.md | LICENSE.md
← All files
name: src/main/java/io/protobase/f7/formulas/math/SIGN.java
-rw-r--r--
1133
 1package io.protobase.f7.formulas.math;
 2
 3import io.protobase.f7.errors.F7Exception;
 4import io.protobase.f7.formulas.AbstractFormula;
 5import io.protobase.f7.formulas.FormulaName;
 6import io.protobase.f7.models.GridColumnRowKey;
 7import io.protobase.f7.utils.Converters;
 8import io.protobase.f7.utils.NumberUtils;
 9
10import java.util.function.BiFunction;
11import java.util.function.Function;
12
13public class SIGN extends AbstractFormula {
14  public static FormulaName NAME = FormulaName.SIGN;
15  public static SIGN SELF = new SIGN();
16
17  public SIGN() {
18    super();
19  }
20
21  public SIGN(Function<Object, Object> lookup, BiFunction<GridColumnRowKey, Object, Object> collateralLookup) {
22    super(lookup, collateralLookup);
23  }
24
25  @Override
26  public Object internal(GridColumnRowKey origin, Object... values) {
27    checkLength(values.length, 1, NAME);
28    Object value = Converters.first(collateralLookup.apply(origin, values[0]));
29    if (value instanceof F7Exception) {
30      return value;
31    }
32    double number = Converters.toDouble(value);
33    if (NumberUtils.isZero(number)) {
34      return 0.0;
35    }
36    return number > 0 ? 1.0 : -1.0;
37  }
38}