1 package org.simantics.spreadsheet.graph;
3 import java.util.Collections;
4 import java.util.HashMap;
7 import java.util.Optional;
9 import org.simantics.spreadsheet.Range;
10 import org.simantics.spreadsheet.graph.parser.ast.AstRange;
12 public class SpreadsheetEngine implements SpreadsheetElement, SheetNode {
14 private static final long serialVersionUID = -5246063647558595642L;
16 private static final String LINES = "Lines";
18 private final SpreadsheetBook book;
19 private final String name;
22 public SpreadsheetLines lines;
24 transient public Map<String,Object> rangeCache;
26 public Map<String,Object> getRangeCache() {
27 if(rangeCache == null) rangeCache = new HashMap<String,Object>();
31 public Object getCachedRange(AstRange range) {
32 if(range.sheetName != null) return null;
33 return getRangeCache().get(range.first + ":" + range.second);
36 public void cacheRange(AstRange range, Object value) {
37 if(range.sheetName != null) return;
38 getRangeCache().put(range.first + ":" + range.second, value);
41 public SpreadsheetEngine(SpreadsheetBook book, String name) {
44 this.id = book.getNewId(this);
45 this.lines = new SpreadsheetLines(this, LINES);
48 public SpreadsheetBook getBook() {
56 Object resolve(String[] parts, int index) {
58 String part = parts[index];
59 if(!part.equals(LINES)) return null;
61 if(index == parts.length-1) return lines;
63 return lines.resolve(parts, index+1);
68 public String getName() {
73 public Map<String, SheetNode> getChildren() {
74 return Collections.singletonMap(LINES, lines);
78 public Map<String, SheetNode> getProperties() {
79 return Collections.emptyMap();
82 public Object ensureSubprocess(String[] path, int index) {
84 String name = path[index];
85 if(!LINES.equals(name)) throw new IllegalStateException();
86 if(index == path.length - 1) return lines;
87 return lines.ensureSubprocess(path, index+1);
91 public SpreadsheetLine getLine(int row) {
92 assert(lines.nodes.size() == 1);
93 SpreadsheetLines root = lines.nodes.values().iterator().next();
94 return root.getLine(row);
98 public void accept(SpreadsheetVisitor v) {
102 public Range actualRange(Range r) {
104 SpreadsheetLines root = lines.nodes.values().iterator().next();
105 Range result = new Range(r);
107 result.endRow = root.getMaxRow();
115 public Optional<SpreadsheetElement> getParent() {
116 return Optional.of(book);
120 public List<SpreadsheetElement> getSpreadsheetChildren() {
121 return Collections.singletonList(lines);
125 public void remove(SpreadsheetElement child) {
130 public int hashCode() {
131 final int prime = 31;
133 result = prime * result + ((book == null) ? 0 : book.hashCode());
134 result = prime * result + ((name == null) ? 0 : name.hashCode());
139 public boolean equals(Object obj) {
144 if (getClass() != obj.getClass())
146 SpreadsheetEngine other = (SpreadsheetEngine) obj;
148 if (other.book != null)
150 } else if (!book.equals(other.book))
153 if (other.name != null)
155 } else if (!name.equals(other.name))