From 2d27cd697c3a866b477804fbf17b79b31de098de Mon Sep 17 00:00:00 2001 From: Denis Molony Date: Sun, 19 Mar 2017 13:31:20 +1100 Subject: [PATCH] Refactoring Converted CHOOSE, LOOKUP and NPV to use an ExpressionList. --- .../bytezone/diskbrowser/visicalc/Choose.java | 47 +++++++++++-- .../bytezone/diskbrowser/visicalc/Lookup.java | 70 +++++++++++++++++-- .../bytezone/diskbrowser/visicalc/Npv.java | 51 ++++++++++++-- .../diskbrowser/visicalc/ValueList.java | 5 ++ 4 files changed, 158 insertions(+), 15 deletions(-) diff --git a/src/com/bytezone/diskbrowser/visicalc/Choose.java b/src/com/bytezone/diskbrowser/visicalc/Choose.java index 5d14ceb..55b685c 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Choose.java +++ b/src/com/bytezone/diskbrowser/visicalc/Choose.java @@ -1,5 +1,7 @@ package com.bytezone.diskbrowser.visicalc; +import com.bytezone.diskbrowser.visicalc.Cell.CellType; + public class Choose extends Function { Choose (Cell cell, String text) @@ -8,16 +10,51 @@ public class Choose extends Function assert text.startsWith ("@CHOOSE(") : text; - String sourceText = Expression.getParameter (functionText); - source = cell.getExpressionValue (sourceText); - values.add (source); + list = new ValueList (cell, functionText); - String rangeText = functionText.substring (sourceText.length () + 1); - range = new Range (parent, cell, rangeText); + for (Value v : list) + values.add (v); + + // String sourceText = Expression.getParameter (functionText); + // source = cell.getExpressionValue (sourceText); + // values.add (source); + // + // String rangeText = functionText.substring (sourceText.length () + 1); + // range = new Range (parent, cell, rangeText); } @Override public void calculate () + { + Value source = list.get (0); + + source.calculate (); + if (!source.isValueType (ValueType.VALUE)) + { + valueType = source.getValueType (); + return; + } + + int index = (int) source.getValue (); + if (index < 1 || index >= list.size ()) + { + valueType = ValueType.NA; + return; + } + + Cell cell = (Cell) list.get (index); + // Address address = range.get (index); + if (cell.isCellType (CellType.EMPTY)) + valueType = ValueType.NA; + else + { + // Cell cell = parent.getCell (address); + valueType = cell.getValueType (); + value = cell.getValue (); + } + } + + public void calculate2 () { source.calculate (); if (!source.isValueType (ValueType.VALUE)) diff --git a/src/com/bytezone/diskbrowser/visicalc/Lookup.java b/src/com/bytezone/diskbrowser/visicalc/Lookup.java index 94b1de1..75ba54e 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Lookup.java +++ b/src/com/bytezone/diskbrowser/visicalc/Lookup.java @@ -8,16 +8,76 @@ class Lookup extends Function assert text.startsWith ("@LOOKUP(") : text; - String sourceText = Expression.getParameter (functionText); - source = cell.getExpressionValue (sourceText); - values.add (source); + list = new ValueList (cell, functionText); - String rangeText = functionText.substring (sourceText.length () + 1); - range = new Range (parent, cell, rangeText); + for (Value v : list) + values.add (v); + + // String sourceText = Expression.getParameter (functionText); + // source = cell.getExpressionValue (sourceText); + // values.add (source); + // + // String rangeText = functionText.substring (sourceText.length () + 1); + // range = new Range (parent, cell, rangeText); } @Override public void calculate () + { + Value source = list.get (0); + + source.calculate (); + + if (!source.isValueType (ValueType.VALUE)) + { + valueType = source.getValueType (); + return; + } + + if (list.size () <= 1) + { + valueType = ValueType.NA; + return; + } + + double sourceValue = source.getValue (); + Address target = null; + Cell firstCell = (Cell) list.get (1); + Cell lastCell = (Cell) list.get (list.size () - 1); + boolean isVertical = firstCell.getAddress ().columnMatches (lastCell.getAddress ()); + + for (int i = 1; i < list.size (); i++) + { + Cell cell = (Cell) list.get (i); + // Cell cell = parent.getCell (address); + if (cell.getValue () > sourceValue) // past the value + break; + target = cell.getAddress (); + } + + if (target == null) + { + valueType = ValueType.NA; + value = 0; + } + else + { + Address adjacentAddress = isVertical ? target.nextColumn () : target.nextRow (); + + if (parent.cellExists (adjacentAddress)) + { + value = parent.getCell (adjacentAddress).getValue (); + valueType = ValueType.VALUE; + } + else + { + value = 0; + valueType = ValueType.VALUE; + } + } + } + + public void calculate2 () { source.calculate (); diff --git a/src/com/bytezone/diskbrowser/visicalc/Npv.java b/src/com/bytezone/diskbrowser/visicalc/Npv.java index c80ee3a..7c2ee30 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Npv.java +++ b/src/com/bytezone/diskbrowser/visicalc/Npv.java @@ -10,12 +10,16 @@ public class Npv extends Function assert text.startsWith ("@NPV(") : text; - String sourceText = Expression.getParameter (functionText); - source = cell.getExpressionValue (sourceText); - values.add (source); + list = new ValueList (cell, functionText); - String rangeText = functionText.substring (sourceText.length () + 1); - range = new Range (parent, cell, rangeText); + for (Value v : list) + values.add (v); + // String sourceText = Expression.getParameter (functionText); + // source = cell.getExpressionValue (sourceText); + // values.add (source); + // + // String rangeText = functionText.substring (sourceText.length () + 1); + // range = new Range (parent, cell, rangeText); } @Override @@ -24,6 +28,43 @@ public class Npv extends Function value = 0; valueType = ValueType.VALUE; + Value source = list.get (0); + source.calculate (); + if (!source.isValueType (ValueType.VALUE)) + { + valueType = source.getValueType (); + return; + } + + double rate = 1 + source.getValue (); + + int period = 0; + int pos = 0; + for (int i = 1; i < list.size (); i++) + { + Cell cell = (Cell) list.get (i); + + ++period; + + // Cell cell = parent.getCell (address); + if (cell.isCellType (CellType.EMPTY)) + continue; + + if (!cell.isValueType (ValueType.VALUE)) + { + valueType = cell.getValueType (); + return; + } + + value += cell.getValue () / Math.pow (rate, period); + } + } + + public void calculate2 () + { + value = 0; + valueType = ValueType.VALUE; + source.calculate (); if (!source.isValueType (ValueType.VALUE)) { diff --git a/src/com/bytezone/diskbrowser/visicalc/ValueList.java b/src/com/bytezone/diskbrowser/visicalc/ValueList.java index e35576e..203fc3c 100644 --- a/src/com/bytezone/diskbrowser/visicalc/ValueList.java +++ b/src/com/bytezone/diskbrowser/visicalc/ValueList.java @@ -30,6 +30,11 @@ public class ValueList implements Iterable } } + public Value get (int index) + { + return values.get (index); + } + public int size () { return values.size ();