Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. package spreadsheet;
  2.  
  3. import java.util.HashSet;
  4. import java.util.Set;
  5. import spreadsheet.api.CellLocation;
  6. import spreadsheet.api.ExpressionUtils;
  7. import spreadsheet.api.value.InvalidValue;
  8. import spreadsheet.api.value.Value;
  9. import spreadsheet.api.observer.Observer;
  10.  
  11. public class Cell implements Observer<Cell> {
  12.  
  13. private CellLocation location;
  14. private Spreadsheet sheet;
  15. private String exp;
  16. private Value val;
  17. private Set<Observer<Cell>> depCells = new HashSet<Observer<Cell>>();
  18.  
  19. public Cell (CellLocation location, Spreadsheet sheet) {
  20. this.location = location;
  21. this.sheet = sheet;
  22. this.exp = "";
  23. this.val = null;
  24. }
  25.  
  26. public String getExpression() {
  27. return exp;
  28. }
  29.  
  30. public void setExpression(String newExp) {
  31. removeObserver(this);
  32. this.exp = newExp;
  33. this.val = new InvalidValue(newExp);
  34. sheet.recomputeCells.add(this);
  35. Set<CellLocation> refCells = ExpressionUtils.getReferencedLocations(newExp);
  36.  
  37. for (CellLocation refCell : refCells) {
  38. Cell newCell = sheet.map.get(refCell);
  39. depCells.add(newCell);
  40. }
  41.  
  42. }
  43.  
  44. public Value getValue() {
  45. return val;
  46. }
  47.  
  48. public void setValue(Value newValue) {
  49. this.val = newValue;
  50. }
  51.  
  52. @Override
  53. public void update(Cell changed) {
  54. setExpression(changed.getExpression());
  55.  
  56. if (!sheet.recomputeCells.contains(this)) {
  57. this.val = new InvalidValue(this.exp);
  58. }
  59.  
  60. }
  61.  
  62. private void removeObserver(Observer<Cell> observer) {
  63. depCells.remove(observer);
  64. }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement