name:
src/main/java/io/protobase/f7/formulas/text/CONCAT.java
-rw-r--r--
1477
1package io.protobase.f7.formulas.text;
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;
8
9import java.util.function.BiFunction;
10import java.util.function.Function;
11
12/**
13 * TODO/HACK
14 * In Google Sheets: =CONCAT({"Hello","One"}, {"There","Two"}) results in "HelloThere"
15 * In Excel: =CONCAT({"Hello","One"}, {"There","Two"}) results in "HelloOneThereTwo"
16 * <p>
17 * "Fixing" this is tricky. Which one should be considered correct? Are we integrating with both?
18 */
19public class CONCAT extends AbstractFormula {
20 public static FormulaName NAME = FormulaName.CONCAT;
21 public static CONCAT SELF = new CONCAT();
22
23 public CONCAT() {
24 super();
25 }
26
27 public CONCAT(Function<Object, Object> lookup, BiFunction<GridColumnRowKey, Object, Object> collateralLookup) {
28 super(lookup, collateralLookup);
29 }
30
31 @Override
32 public Object internal(GridColumnRowKey origin, Object... values) {
33 checkLength(values.length, 2, NAME);
34 Object first = Converters.first(collateralLookup.apply(origin, values[0]));
35 Object second = Converters.first(collateralLookup.apply(origin, values[1]));
36 if (first instanceof F7Exception) {
37 return first;
38 }
39 if (second instanceof F7Exception) {
40 return second;
41 }
42 return Converters.toText(first) + Converters.toText(second);
43 }
44}