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/utils/Mappers.java
-rw-r--r--
2616
 1package io.protobase.f7.utils;
 2
 3import io.protobase.f7.errors.F7Exception;
 4import io.protobase.f7.models.Grid;
 5
 6import java.util.stream.Stream;
 7import java.util.stream.StreamSupport;
 8
 9public class Mappers {
10  /**
11   * Flatten possibly nested lists into a flat stream of objects that are either not a list, or a list of size 0.
12   *
13   * @param value - to return as a stream.
14   * @return - stream of objects that are Number, Boolean, String, F7Exception, or List of size 0.
15   */
16  public static Stream<Object> toFlatStream(Object value) {
17    if (value instanceof Grid) {
18      return StreamSupport.stream(((Grid<Object>) value).spliterator(), false);
19    }
20    return Stream.of(value);
21  }
22
23  /**
24   * Flatten possibly nested lists/grids into a flat stream of objects that are either not a list, or a list of size 0,
25   * along the way convert second-degree streams/lists to streams/lists with ONLY number literals. No booleans,
26   * coercable strings, or errors.
27   * <p>
28   * Examples:
29   * 6, 7, {1, 2, "3"} would become {6}, {7}, {1, 2}.
30   * {1, 2, "NA"} would become {1, 2}.
31   * {1, 2, true} would become {1, 2}.
32   *
33   * @param value - to return as stream.
34   * @return stream of objects.
35   */
36  public static Stream<Object> toFlatStreamGridFilterOnlyNumberLiterals(Object value) {
37    if (value instanceof Grid) {
38      return StreamSupport.stream(((Grid<Object>) value).spliterator(), false)
39          .filter(Filters::isLiteralNumber);
40    }
41    return Stream.of(value);
42  }
43
44  /**
45   * Flatten possibly nested lists/grids into a flat stream of objects that are either not a list, or a list of size 0,
46   * along the way convert second-degree streams/lists to streams/lists with ONLY number literals and boolean literals.
47   * No strings.
48   * <p>
49   * Examples:
50   * 6, 7, {1, 2, "3"} would become {6}, {7}, {1, 2}.
51   * {1, 2, "NA"} would become {1, 2}.
52   * {1, 2, true} would become {1, 2, true}.
53   *
54   * @param value - to return as stream.
55   * @return stream of objects.
56   */
57  public static Stream<Object> toFlatStreamFilterOnlyNumeric(Object value) {
58    if (value instanceof Grid) {
59      return StreamSupport.stream(((Grid<Object>) value).spliterator(), false)
60          .map(Converters::textToZero)
61          .filter(Filters::isNumeric);
62    }
63    return Stream.of(value);
64  }
65
66  /**
67   * Return object if it's not an exception. Throw the exception if it exists.
68   *
69   * @param value - to check.
70   * @return object.
71   */
72  public static Object throwIfException(Object value) {
73    if (value instanceof F7Exception) {
74      throw ((F7Exception) value);
75    }
76    return value;
77  }
78
79}