Refactoring

Converted CHOOSE, LOOKUP and NPV to use an ExpressionList.
This commit is contained in:
Denis Molony 2017-03-19 13:31:20 +11:00
parent 6a3ba98ba8
commit 2d27cd697c
4 changed files with 158 additions and 15 deletions

View File

@ -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))

View File

@ -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 ();

View File

@ -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))
{

View File

@ -30,6 +30,11 @@ public class ValueList implements Iterable<Value>
}
}
public Value get (int index)
{
return values.get (index);
}
public int size ()
{
return values.size ();