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 @SuppressWarnings("rawtypes")
13 public class SpreadsheetEngine implements SpreadsheetElement, SheetNode {
15 private static final long serialVersionUID = -5246063647558595642L;
17 private static final String LINES = "Lines";
19 private final SpreadsheetBook book;
20 private final String name;
23 public SpreadsheetLines lines;
25 transient public Map<String,Object> rangeCache;
27 public Map<String,Object> getRangeCache() {
28 if(rangeCache == null) rangeCache = new HashMap<String,Object>();
32 public Object getCachedRange(AstRange range) {
33 if(range.sheetName != null) return null;
34 return getRangeCache().get(range.first + ":" + range.second);
37 public void cacheRange(AstRange range, Object value) {
38 if(range.sheetName != null) return;
39 getRangeCache().put(range.first + ":" + range.second, value);
42 public SpreadsheetEngine(SpreadsheetBook book, String name) {
45 this.id = book.getNewId(this);
46 this.lines = new SpreadsheetLines(this, LINES);
49 public SpreadsheetBook getBook() {
57 Object resolve(String[] parts, int index) {
59 String part = parts[index];
60 if(!part.equals(LINES)) return null;
62 if(index == parts.length-1) return lines;
64 return lines.resolve(parts, index+1);
69 public String getName() {
74 public Map<String, SheetNode> getChildren() {
75 return Collections.singletonMap(LINES, lines);
79 public Map<String, SheetNode> getProperties() {
80 return Collections.emptyMap();
83 public Object ensureSubprocess(String[] path, int index) {
85 String name = path[index];
86 if(!LINES.equals(name)) throw new IllegalStateException();
87 if(index == path.length - 1) return lines;
88 return lines.ensureSubprocess(path, index+1);
92 public SpreadsheetLine getLine(int row) {
93 assert(lines.nodes.size() == 1);
94 SpreadsheetLines root = lines.nodes.values().iterator().next();
95 return root.getLine(row);
99 public void accept(SpreadsheetVisitor v) {
103 public Range actualRange(Range r) {
105 SpreadsheetLines root = lines.nodes.values().iterator().next();
106 Range result = new Range(r);
108 result.endRow = root.getMaxRow();
116 public Optional<SpreadsheetElement> getParent() {
117 return Optional.of(book);
121 public List<SpreadsheetElement> getSpreadsheetChildren() {
122 return Collections.singletonList(lines);
126 public void remove(SpreadsheetElement child) {
131 public int hashCode() {
132 final int prime = 31;
134 result = prime * result + ((book == null) ? 0 : book.hashCode());
135 result = prime * result + ((name == null) ? 0 : name.hashCode());
140 public boolean equals(Object obj) {
145 if (getClass() != obj.getClass())
147 SpreadsheetEngine other = (SpreadsheetEngine) obj;
149 if (other.book != null)
151 } else if (!book.equals(other.book))
154 if (other.name != null)
156 } else if (!name.equals(other.name))