refactoring
This commit is contained in:
parent
cf8d3729d4
commit
be3a8c650d
|
@ -2,15 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
public class Abs extends Function
|
||||
{
|
||||
private final Value source;
|
||||
|
||||
Abs (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@ABS(") : text;
|
||||
|
||||
source = new Expression (parent, cell, functionText).reduce ();
|
||||
source = cell.getExpressionValue (functionText);
|
||||
values.add (source);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,15 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
public class Acos extends Function
|
||||
{
|
||||
private final Value source;
|
||||
|
||||
Acos (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@ACOS(") : text;
|
||||
|
||||
source = new Expression (parent, cell, functionText).reduce ();
|
||||
source = cell.getExpressionValue (functionText);
|
||||
values.add (source);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,15 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
public class Asin extends Function
|
||||
{
|
||||
private final Value source;
|
||||
|
||||
Asin (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@ASIN(") : text;
|
||||
|
||||
source = new Expression (parent, cell, functionText).reduce ();
|
||||
source = cell.getExpressionValue (functionText);
|
||||
values.add (source);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,15 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
public class Atan extends Function
|
||||
{
|
||||
private final Value source;
|
||||
|
||||
Atan (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@ATAN(") : text;
|
||||
|
||||
source = new Expression (parent, cell, functionText).reduce ();
|
||||
source = cell.getExpressionValue (functionText);
|
||||
values.add (source);
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@ import com.bytezone.diskbrowser.visicalc.Cell.CellType;
|
|||
|
||||
public class Average extends Function
|
||||
{
|
||||
private final ValueList list;
|
||||
private final boolean isRange; // may affect how the count is done
|
||||
|
||||
public Average (Cell cell, String text)
|
||||
|
|
|
@ -33,6 +33,16 @@ class Cell extends AbstractValue implements Comparable<Cell>
|
|||
isVolatile = false;
|
||||
}
|
||||
|
||||
Function getFunction (String text)
|
||||
{
|
||||
return parent.getFunction (this, text);
|
||||
}
|
||||
|
||||
Value getExpressionValue (String text)
|
||||
{
|
||||
return new Expression (this, text).reduce ();
|
||||
}
|
||||
|
||||
boolean isCellType (CellType cellType)
|
||||
{
|
||||
return this.cellType == cellType;
|
||||
|
@ -106,7 +116,7 @@ class Cell extends AbstractValue implements Comparable<Cell>
|
|||
try
|
||||
{
|
||||
expressionText = command;
|
||||
value = new Expression (parent, this, expressionText).reduce ();
|
||||
value = new Expression (this, expressionText).reduce ();
|
||||
cellType = CellType.VALUE;
|
||||
isVolatile = true;
|
||||
}
|
||||
|
|
|
@ -2,23 +2,17 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
public class Choose extends Function
|
||||
{
|
||||
private final String sourceText;
|
||||
private final String rangeText;
|
||||
|
||||
private final Value source;
|
||||
private final Range range;
|
||||
|
||||
Choose (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@CHOOSE(") : text;
|
||||
|
||||
sourceText = Expression.getParameter (functionText);
|
||||
source = new Expression (parent, cell, sourceText).reduce ();
|
||||
String sourceText = Expression.getParameter (functionText);
|
||||
source = cell.getExpressionValue (sourceText);
|
||||
values.add (source);
|
||||
|
||||
rangeText = functionText.substring (sourceText.length () + 1);
|
||||
String rangeText = functionText.substring (sourceText.length () + 1);
|
||||
range = new Range (parent, cell, rangeText);
|
||||
}
|
||||
|
||||
|
|
|
@ -29,13 +29,13 @@ class Condition extends AbstractValue implements Iterable<Value>
|
|||
if (pos > 0)
|
||||
{
|
||||
conditionText = text.substring (0, pos);
|
||||
conditionExpression = new Expression (parent, cell, conditionText);
|
||||
conditionExpression = new Expression (cell, conditionText);
|
||||
values.add (conditionExpression);
|
||||
|
||||
comparator = comp;
|
||||
|
||||
valueText = text.substring (pos + comp.length ());
|
||||
valueExpression = new Expression (parent, cell, valueText);
|
||||
valueExpression = new Expression (cell, valueText);
|
||||
values.add (valueExpression);
|
||||
break;
|
||||
}
|
||||
|
@ -46,13 +46,13 @@ class Condition extends AbstractValue implements Iterable<Value>
|
|||
if (text.startsWith ("@"))
|
||||
{
|
||||
conditionText = text;
|
||||
conditionExpression = new Expression (parent, cell, text);
|
||||
conditionExpression = new Expression (cell, text);
|
||||
values.add (conditionExpression);
|
||||
|
||||
comparator = "=";
|
||||
|
||||
valueText = "1";
|
||||
valueExpression = new Expression (parent, cell, valueText);
|
||||
valueExpression = new Expression (cell, valueText);
|
||||
values.add (valueExpression);
|
||||
}
|
||||
else
|
||||
|
|
|
@ -2,15 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
public class Cos extends Function
|
||||
{
|
||||
private final Value source;
|
||||
|
||||
Cos (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@COS(") : text;
|
||||
|
||||
source = new Expression (parent, cell, functionText).reduce ();
|
||||
source = cell.getExpressionValue (functionText);
|
||||
values.add (source);
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,6 @@ import com.bytezone.diskbrowser.visicalc.Cell.CellType;
|
|||
|
||||
class Count extends Function
|
||||
{
|
||||
private final ValueList list;
|
||||
private final boolean isRange;
|
||||
|
||||
public Count (Cell cell, String text)
|
||||
|
|
|
@ -2,15 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
public class Exp extends Function
|
||||
{
|
||||
private final Value source;
|
||||
|
||||
Exp (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@EXP(") : text;
|
||||
|
||||
source = new Expression (parent, cell, functionText).reduce ();
|
||||
source = cell.getExpressionValue (functionText);
|
||||
values.add (source);
|
||||
}
|
||||
|
||||
|
|
|
@ -35,11 +35,12 @@ class Expression extends AbstractValue implements Iterable<Value>
|
|||
|
||||
private final String text;
|
||||
|
||||
public Expression (Sheet parent, Cell cell, String text)
|
||||
public Expression (Cell cell, String text)
|
||||
{
|
||||
super ("Exp");
|
||||
this.cell = cell;
|
||||
this.text = text;
|
||||
Sheet parent = cell.getParent ();
|
||||
|
||||
String line = balanceBrackets (text); // add trailing right brackets if necessary
|
||||
|
||||
|
@ -66,13 +67,13 @@ class Expression extends AbstractValue implements Iterable<Value>
|
|||
case '@': // function
|
||||
String functionText = getFunctionCall (line.substring (ptr));
|
||||
ptr += functionText.length ();
|
||||
values.add (parent.getFunction (cell, functionText));
|
||||
values.add (cell.getFunction (functionText));
|
||||
break;
|
||||
|
||||
case '(': // parentheses block
|
||||
String bracketText = getBalancedText (line.substring (ptr));
|
||||
ptr += bracketText.length ();
|
||||
values.add (new Expression (parent, cell,
|
||||
values.add (new Expression (cell,
|
||||
bracketText.substring (1, bracketText.length () - 1)));
|
||||
break;
|
||||
|
||||
|
|
|
@ -16,9 +16,14 @@ abstract class Function extends AbstractValue implements Iterable<Value>
|
|||
protected String functionText;
|
||||
protected String fullText;
|
||||
|
||||
protected Value source;
|
||||
protected ValueList list;
|
||||
protected Range range;
|
||||
|
||||
Function (Cell cell, String text)
|
||||
{
|
||||
super ("Function");
|
||||
|
||||
this.parent = cell.getParent ();
|
||||
this.cell = cell;
|
||||
fullText = text;
|
||||
|
|
|
@ -25,10 +25,10 @@ class If extends Function
|
|||
condition = new Condition (parent, cell, conditionText);
|
||||
values.add (condition);
|
||||
|
||||
expTrue = new Expression (parent, cell, textTrue);
|
||||
expTrue = new Expression (cell, textTrue);
|
||||
values.add (expTrue);
|
||||
|
||||
expFalse = new Expression (parent, cell, textFalse);
|
||||
expFalse = new Expression (cell, textFalse);
|
||||
values.add (expFalse);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,15 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
public class Int extends Function
|
||||
{
|
||||
private final Value source;
|
||||
|
||||
Int (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@INT(") : text;
|
||||
|
||||
source = new Expression (parent, cell, functionText).reduce ();
|
||||
source = cell.getExpressionValue (functionText);
|
||||
values.add (source);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,15 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
class IsError extends Function
|
||||
{
|
||||
private final Value source;
|
||||
|
||||
public IsError (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@ISERROR(") : text;
|
||||
|
||||
source = new Expression (parent, cell, functionText).reduce ();
|
||||
source = cell.getExpressionValue (functionText);
|
||||
values.add (source);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,15 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
public class IsNa extends Function
|
||||
{
|
||||
private final Value source;
|
||||
|
||||
IsNa (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@ISNA(") : text;
|
||||
|
||||
source = new Expression (parent, cell, functionText).reduce ();
|
||||
source = cell.getExpressionValue (functionText);
|
||||
values.add (source);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,15 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
public class Ln extends Function
|
||||
{
|
||||
private final Value source;
|
||||
|
||||
Ln (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@LN(") : text;
|
||||
|
||||
source = new Expression (parent, cell, functionText).reduce ();
|
||||
source = cell.getExpressionValue (functionText);
|
||||
values.add (source);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,15 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
public class Log10 extends Function
|
||||
{
|
||||
private final Value source;
|
||||
|
||||
Log10 (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@LOG10(") : text;
|
||||
|
||||
source = new Expression (parent, cell, functionText).reduce ();
|
||||
source = cell.getExpressionValue (functionText);
|
||||
values.add (source);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,23 +2,17 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
class Lookup extends Function
|
||||
{
|
||||
private final String sourceText;
|
||||
private final String rangeText;
|
||||
|
||||
private final Value source;
|
||||
private final Range range;
|
||||
|
||||
public Lookup (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@LOOKUP(") : text;
|
||||
|
||||
sourceText = Expression.getParameter (functionText);
|
||||
source = new Expression (parent, cell, sourceText).reduce ();
|
||||
String sourceText = Expression.getParameter (functionText);
|
||||
source = cell.getExpressionValue (sourceText);
|
||||
values.add (source);
|
||||
|
||||
rangeText = functionText.substring (sourceText.length () + 1);
|
||||
String rangeText = functionText.substring (sourceText.length () + 1);
|
||||
range = new Range (parent, cell, rangeText);
|
||||
}
|
||||
|
||||
|
@ -72,7 +66,4 @@ class Lookup extends Function
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// @LOOKUP(B8,F3...F16)
|
||||
// @LOOKUP(.2*K8+K7,F3...F16)
|
||||
}
|
|
@ -2,8 +2,6 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
class Max extends Function
|
||||
{
|
||||
private final ValueList list;
|
||||
|
||||
public Max (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
|
|
@ -2,8 +2,6 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
class Min extends Function
|
||||
{
|
||||
private final ValueList list;
|
||||
|
||||
public Min (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
|
|
@ -4,23 +4,17 @@ import com.bytezone.diskbrowser.visicalc.Cell.CellType;
|
|||
|
||||
public class Npv extends Function
|
||||
{
|
||||
private final String sourceText;
|
||||
private final String rangeText;
|
||||
|
||||
private final Value source;
|
||||
private final Range range;
|
||||
|
||||
Npv (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@NPV(") : text;
|
||||
|
||||
sourceText = Expression.getParameter (functionText);
|
||||
source = new Expression (parent, cell, sourceText).reduce ();
|
||||
String sourceText = Expression.getParameter (functionText);
|
||||
source = cell.getExpressionValue (sourceText);
|
||||
values.add (source);
|
||||
|
||||
rangeText = functionText.substring (sourceText.length () + 1);
|
||||
String rangeText = functionText.substring (sourceText.length () + 1);
|
||||
range = new Range (parent, cell, rangeText);
|
||||
}
|
||||
|
||||
|
|
|
@ -493,24 +493,16 @@ public class Sheet
|
|||
|
||||
Function getFunction (Cell cell, String text)
|
||||
{
|
||||
if (text.charAt (0) != '@')
|
||||
{
|
||||
System.out.printf ("Unknown function: [%s]%n", text);
|
||||
return new Error (cell, "@ERROR");
|
||||
}
|
||||
|
||||
String functionName = "";
|
||||
int functionId = -1;
|
||||
for (int i = 0; i < Function.functionList.length; i++)
|
||||
if (text.startsWith (Function.functionList[i]))
|
||||
{
|
||||
functionId = i;
|
||||
functionTotals[i]++;
|
||||
functionName = Function.functionList[i];
|
||||
break;
|
||||
}
|
||||
|
||||
if (functionName.isEmpty ())
|
||||
if (functionId < 0)
|
||||
{
|
||||
System.out.printf ("Unknown function: [%s]%n", text);
|
||||
return new Error (cell, "@ERROR");
|
||||
|
@ -609,7 +601,7 @@ public class Sheet
|
|||
return new True (cell, text);
|
||||
|
||||
default:
|
||||
System.out.printf ("Unknown function: [%s]%n", text);
|
||||
System.out.printf ("Unknown function ID: %d%n", functionId);
|
||||
return new Error (cell, "@ERROR");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,15 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
public class Sin extends Function
|
||||
{
|
||||
private final Value source;
|
||||
|
||||
Sin (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@SIN(") : text;
|
||||
|
||||
source = new Expression (parent, cell, functionText).reduce ();
|
||||
source = cell.getExpressionValue (functionText);
|
||||
values.add (source);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,15 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
public class Sqrt extends Function
|
||||
{
|
||||
private final Value source;
|
||||
|
||||
Sqrt (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@SQRT(") : text;
|
||||
|
||||
source = new Expression (parent, cell, functionText).reduce ();
|
||||
source = cell.getExpressionValue (functionText);
|
||||
values.add (source);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,8 +2,6 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
class Sum extends Function
|
||||
{
|
||||
private final ValueList list;
|
||||
|
||||
public Sum (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
|
|
@ -2,15 +2,13 @@ package com.bytezone.diskbrowser.visicalc;
|
|||
|
||||
public class Tan extends Function
|
||||
{
|
||||
private final Value source;
|
||||
|
||||
Tan (Cell cell, String text)
|
||||
{
|
||||
super (cell, text);
|
||||
|
||||
assert text.startsWith ("@TAN(") : text;
|
||||
|
||||
source = new Expression (parent, cell, functionText).reduce ();
|
||||
source = cell.getExpressionValue (functionText);
|
||||
values.add (source);
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ public class ValueList implements Iterable<Value>
|
|||
for (Address address : new Range (parent, cell, parameter))
|
||||
values.add (parent.getCell (address));
|
||||
else
|
||||
values.add (new Expression (parent, cell, parameter).reduce ());
|
||||
values.add (new Expression (cell, parameter).reduce ());
|
||||
|
||||
if (remainder.length () == parameter.length ())
|
||||
break;
|
||||
|
|
Loading…
Reference in New Issue