Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package spreadsheet;
- import java.util.HashSet;
- import java.util.Set;
- import spreadsheet.api.CellLocation;
- import spreadsheet.api.ExpressionUtils;
- import spreadsheet.api.value.InvalidValue;
- import spreadsheet.api.value.Value;
- import spreadsheet.api.observer.Observer;
- public class Cell implements Observer<Cell> {
- private CellLocation location;
- private Spreadsheet sheet;
- private String exp;
- private Value val;
- private Set<Observer<Cell>> depCells = new HashSet<Observer<Cell>>();
- public Cell (CellLocation location, Spreadsheet sheet) {
- this.location = location;
- this.sheet = sheet;
- this.exp = "";
- this.val = null;
- }
- public String getExpression() {
- return exp;
- }
- public void setExpression(String newExp) {
- removeObserver(this);
- this.exp = newExp;
- this.val = new InvalidValue(newExp);
- sheet.recomputeCells.add(this);
- Set<CellLocation> refCells = ExpressionUtils.getReferencedLocations(newExp);
- for (CellLocation refCell : refCells) {
- Cell newCell = sheet.map.get(refCell);
- depCells.add(newCell);
- }
- }
- public Value getValue() {
- return val;
- }
- public void setValue(Value newValue) {
- this.val = newValue;
- }
- @Override
- public void update(Cell changed) {
- setExpression(changed.getExpression());
- if (!sheet.recomputeCells.contains(this)) {
- this.val = new InvalidValue(this.exp);
- }
- }
- private void removeObserver(Observer<Cell> observer) {
- depCells.remove(observer);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement