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/models/ListNode.java
-rw-r--r--
1767
 1package io.protobase.f7.models;
 2
 3import com.google.common.base.MoreObjects;
 4
 5import java.util.HashMap;
 6import java.util.Map;
 7import java.util.Objects;
 8
 9public class ListNode extends BaseObject implements Node {
10  private Grid<Node> grid;
11
12  public ListNode() {
13    grid = new Grid<>(0, 0);
14  }
15
16  public ListNode(Grid<Node> grid) {
17    this.grid = grid;
18  }
19
20  public static Builder builder() {
21    return new Builder();
22  }
23
24  public Grid<Node> getGrid() {
25    return grid;
26  }
27
28  public boolean isEmpty() {
29    return grid.isEmpty();
30  }
31
32  @Override
33  public String toString() {
34    return MoreObjects.toStringHelper(this)
35        .add("grid", grid)
36        .toString();
37  }
38
39  @Override
40  public Object[] significantAttributes() {
41    return new Object[]{
42        grid
43    };
44  }
45
46  public static class Builder {
47    private Map<ColumnRowKey, Node> values = new HashMap<>();
48
49    public Builder value(int columnIndex, int rowIndex, Node value) {
50      values.put(new ColumnRowKey(columnIndex, rowIndex), value);
51      return this;
52    }
53
54    public ListNode build() {
55      int columnSize = 1 + values.keySet().stream().map(ColumnRowKey::getColumnIndex).reduce(Integer::max).get();
56      int rowSize = 1 + values.keySet().stream().map(ColumnRowKey::getRowIndex).reduce(Integer::max).get();
57      Grid<Node> grid = new Grid<>(columnSize, rowSize);
58      for (Map.Entry<ColumnRowKey, Node> entry : values.entrySet()) {
59        Node value = entry.getValue();
60        if (Objects.nonNull(value)) {
61          grid.set(entry.getKey().getColumnIndex(), entry.getKey().getRowIndex(), entry.getValue());
62        } else {
63          grid.setEmpty(entry.getKey().getColumnIndex(), entry.getKey().getRowIndex());
64        }
65      }
66      return new ListNode(grid);
67    }
68  }
69}