This commit is contained in:
Denis Molony 2017-03-04 10:41:08 +11:00
parent 2b9db288d4
commit 2bbe143296
23 changed files with 165 additions and 130 deletions

View File

@ -2,22 +2,20 @@ package com.bytezone.diskbrowser.visicalc;
public class Abs extends Function
{
Expression source;
private final Expression source;
Abs (Sheet parent, String text)
{
super (parent, text);
source = new Expression (parent, functionText);
values.add (source);
}
@Override
public void calculate ()
{
if (source == null)
{
source = new Expression (parent, functionText);
values.add (source);
source.calculate ();
}
source.calculate ();
value = Math.abs (source.getValue ());
valueType = source.getValueType ();

View File

@ -89,7 +89,7 @@ public abstract class AbstractValue implements Value
else if (this instanceof Condition)
{
text.append (
String.format ("| Condition : %-69s |%n", ((Condition) this).fullText));
String.format ("| Condition : %-69s |%n", ((Condition) this).getFullText ()));
for (Value v : (Condition) this)
text.append (((AbstractValue) v).getValueText (depth + 1));
}

View File

@ -7,10 +7,10 @@ class Address implements Comparable<Address>
private static final int MAX_ROWS = 255;
private static final int MAX_COLUMNS = 64;
int row, column;
int rowKey;
int columnKey;
String text;
private int row, column;
private int rowKey;
private int columnKey;
private String text;
public Address (String column, String row)
{
@ -65,6 +65,26 @@ class Address implements Comparable<Address>
}
}
boolean rowMatches (Address other)
{
return row == other.row;
}
boolean columnMatches (Address other)
{
return column == other.column;
}
int getRow ()
{
return row;
}
int getColumn ()
{
return column;
}
Address nextRow ()
{
Address next = new Address (column, row + 1);
@ -90,6 +110,16 @@ class Address implements Comparable<Address>
return text;
}
int getRowKey ()
{
return rowKey;
}
int getColumnKey ()
{
return columnKey;
}
public String getDetails ()
{
return String.format ("Row:%3d Col:%3d rKey:%5d cKey:%5d", row, column, rowKey,

View File

@ -5,11 +5,12 @@ import java.util.List;
class And extends Function
{
List<Condition> conditions = new ArrayList<Condition> ();
private final List<Condition> conditions = new ArrayList<Condition> ();
public And (Sheet parent, String text)
{
super (parent, text);
String list[] = text.split (",");
for (String s : list)
conditions.add (new Condition (parent, s));

View File

@ -7,6 +7,7 @@ public class Average extends Function
public Average (Sheet parent, String text)
{
super (parent, text);
range = new Range (parent, text);
}
@ -19,7 +20,7 @@ public class Average extends Function
for (Address address : range)
{
Cell cell = parent.getCell (address);
if (cell == null || cell.isValueType (ValueType.NA))
if (cell.isValueType (ValueType.NA))
continue;
if (!cell.isValueType (ValueType.VALUE))

View File

@ -24,7 +24,7 @@ class Cell extends AbstractValue implements Comparable<Cell>
public Cell (Sheet parent, Address address)
{
super ("Cell " + address.text);
super ("Cell " + address.getText ());
this.parent = parent;
this.address = address;
@ -88,13 +88,13 @@ class Cell extends AbstractValue implements Comparable<Cell>
if (false)
{
System.out.println ("****** Hardcoded values ******");
if (address.rowKey == 67)
if (address.getRowKey () == 67)
expressionText = "1000";
else if (address.rowKey == 131)
else if (address.getRowKey () == 131)
expressionText = "10.5";
else if (address.rowKey == 195)
else if (address.getRowKey () == 195)
expressionText = "12";
else if (address.rowKey == 259)
else if (address.getRowKey () == 259)
expressionText = "8";
}
@ -102,15 +102,15 @@ class Cell extends AbstractValue implements Comparable<Cell>
if (false)
{
System.out.println ("****** Hardcoded values ******");
if (address.rowKey == 66)
if (address.getRowKey () == 66)
expressionText = "10";
else if (address.rowKey == 130)
else if (address.getRowKey () == 130)
expressionText = "30";
else if (address.rowKey == 194)
else if (address.getRowKey () == 194)
expressionText = "65";
else if (address.rowKey == 258)
else if (address.getRowKey () == 258)
expressionText = "1000";
else if (address.rowKey == 386)
else if (address.getRowKey () == 386)
expressionText = "15";
}
@ -118,13 +118,13 @@ class Cell extends AbstractValue implements Comparable<Cell>
if (false)
{
System.out.println ("****** Hardcoded values ******");
if (address.rowKey == 67)
if (address.getRowKey () == 67)
expressionText = "9375";
else if (address.rowKey == 131)
else if (address.getRowKey () == 131)
expressionText = "4500";
else if (address.rowKey == 195)
else if (address.getRowKey () == 195)
expressionText = "24";
else if (address.rowKey == 259)
else if (address.getRowKey () == 259)
expressionText = "11.9";
}
}
@ -173,10 +173,10 @@ class Cell extends AbstractValue implements Comparable<Cell>
@Override
public ValueType getValueType ()
{
// if (value == null)
// calculate ();
if (cellType == CellType.EMPTY)
return ValueType.NA;
if (cellType == CellType.LABEL || cellType == CellType.REPEATING_CHARACTER)
return ValueType.VALUE;
return value.getValueType ();
}
@ -184,8 +184,6 @@ class Cell extends AbstractValue implements Comparable<Cell>
@Override
public String getText ()
{
// if (value == null)
// calculate ();
if (cellType == CellType.EMPTY)
return "";
@ -195,16 +193,15 @@ class Cell extends AbstractValue implements Comparable<Cell>
@Override
public boolean isValueType (ValueType type)
{
// if (value == null || value.getValueType () != ValueType.VALUE)
// calculate ();
if (cellType == CellType.LABEL || cellType == CellType.REPEATING_CHARACTER)
return type == ValueType.VALUE;
if (cellType == CellType.EMPTY)
return type == ValueType.NA;
assert value != null : "bollocks " + address;
return value.isValueType (type);
// if (cellType == CellType.LABEL || cellType == CellType.REPEATING_CHARACTER)
// return type == ValueType.VALUE;
//
// if (cellType == CellType.EMPTY)
// return type == ValueType.NA;
//
// assert value != null : "bollocks " + address;
// return value.isValueType (type);
return type == getValueType ();
}
public boolean isCellType (CellType type)

View File

@ -2,10 +2,10 @@ package com.bytezone.diskbrowser.visicalc;
public class Choose extends Function
{
protected final Range range;
String sourceText;
String rangeText;
Number source;
private final Range range;
private final String sourceText;
private final String rangeText;
private final Number source;
Choose (Sheet parent, String text)
{
@ -13,8 +13,10 @@ public class Choose extends Function
int pos = text.indexOf (',');
sourceText = text.substring (8, pos);
source = new Number (sourceText);
rangeText = text.substring (pos + 1, text.length () - 1);
range = new Range (parent, rangeText);
source = new Number (sourceText);
values.add (source);
}
}

View File

@ -12,7 +12,7 @@ class Condition extends AbstractValue implements Iterable<Value>
private String comparator;
private String conditionText;
private String valueText;
protected String fullText;
private final String fullText;
private Expression conditionExpression;
private Expression valueExpression;
@ -30,6 +30,10 @@ class Condition extends AbstractValue implements Iterable<Value>
{
conditionText = text.substring (0, pos);
valueText = text.substring (pos + comp.length ());
conditionExpression = new Expression (parent, conditionText);
valueExpression = new Expression (parent, valueText);
values.add (conditionExpression);
values.add (valueExpression);
comparator = comp;
break;
}
@ -53,17 +57,8 @@ class Condition extends AbstractValue implements Iterable<Value>
{
value = 0;
if (conditionExpression == null)
{
conditionExpression = new Expression (parent, conditionText);
valueExpression = new Expression (parent, valueText);
conditionExpression.calculate ();
valueExpression.calculate ();
values.add (conditionExpression);
values.add (valueExpression);
}
conditionExpression.calculate ();
valueExpression.calculate ();
if (conditionExpression.isValueType (ValueType.ERROR)
|| valueExpression.isValueType (ValueType.ERROR))
@ -88,6 +83,11 @@ class Condition extends AbstractValue implements Iterable<Value>
System.out.printf ("Unexpected comparator result [%s]%n", comparator);
}
String getFullText ()
{
return fullText;
}
@Override
public String toString ()
{

View File

@ -7,6 +7,7 @@ class Count extends Function
public Count (Sheet parent, String text)
{
super (parent, text);
range = new Range (parent, text);
}
@ -19,7 +20,8 @@ class Count extends Function
for (Address address : range)
{
Cell cell = parent.getCell (address);
if (cell == null || cell.isValueType (ValueType.NA))
if (cell.isValueType (ValueType.NA))
continue;
if (!cell.isValueType (ValueType.VALUE))

View File

@ -32,7 +32,7 @@ class Expression extends AbstractValue implements Iterable<Value>
private final List<String> operators = new ArrayList<String> ();
private final List<String> signs = new ArrayList<String> ();
private String text;
private final String text;
public Expression (Sheet parent, String text)
{
@ -93,9 +93,8 @@ class Expression extends AbstractValue implements Iterable<Value>
Cell cell = parent.getCell (addressText);
if (cell == null)
{
// should this (or parent) create a new empty cell?
// cell = parent.addCell (addressText);
values.add (new Number ("0"));
assert false : "Impossible";
// values.add (new Number ("0"));
}
else
values.add (parent.getCell (addressText));
@ -155,17 +154,16 @@ class Expression extends AbstractValue implements Iterable<Value>
try
{
Value thisValue = values.get (0);
if (thisValue != null) // || thisValue.getValueType () != ValueType.VALUE)
thisValue.calculate ();
thisValue.calculate ();
value = 0;
if (!thisValue.isValueType (ValueType.VALUE))
if (thisValue.isValueType (ValueType.ERROR))
{
valueType = thisValue.getValueType ();
return;
}
value = thisValue.getValue ();
value = thisValue.getValue (); // NA returns zero
String sign = signs.get (0);
if (sign.equals ("(-)"))
@ -174,17 +172,15 @@ class Expression extends AbstractValue implements Iterable<Value>
for (int i = 1; i < values.size (); i++)
{
thisValue = values.get (i);
if (thisValue != null) // || thisValue.getValueType () != ValueType.VALUE)
thisValue.calculate ();
thisValue.calculate ();
if (!thisValue.isValueType (ValueType.VALUE))
// if (thisValue.isValueType (ValueType.ERROR))
if (thisValue.isValueType (ValueType.ERROR))
{
valueType = thisValue.getValueType ();
return;
}
double nextValue = thisValue.getValue ();
double nextValue = thisValue.getValue (); // NA returns zero
sign = signs.get (i);
if (sign.equals ("(-)"))

View File

@ -6,6 +6,11 @@ public class Format
{
private static final DecimalFormat nf = new DecimalFormat ("#####0.00");
private Format ()
{
}
static String format (Value value, char formatChar, int colWidth)
{
double actualValue = value.getValue ();

View File

@ -6,8 +6,8 @@ class If extends Function
private final String textTrue;
private final String textFalse;
private Expression expTrue;
private Expression expFalse;
private final Expression expTrue;
private final Expression expFalse;
public If (Sheet parent, String text)
{
@ -21,6 +21,11 @@ class If extends Function
textTrue = functionText.substring (pos1 + 1, pos2);
textFalse = functionText.substring (pos2 + 1);
expTrue = new Expression (parent, textTrue);
values.add (expTrue);
expFalse = new Expression (parent, textFalse);
values.add (expFalse);
}
@Override
@ -31,12 +36,6 @@ class If extends Function
if (condition.getValue () == 1)
{
if (expTrue == null)
{
expTrue = new Expression (parent, textTrue);
values.add (expTrue);
}
expTrue.calculate ();
if (!expTrue.isValueType (ValueType.VALUE))
@ -46,12 +45,6 @@ class If extends Function
}
else
{
if (expFalse == null)
{
expFalse = new Expression (parent, textFalse);
values.add (expFalse);
}
expFalse.calculate ();
if (!expFalse.isValueType (ValueType.VALUE))

View File

@ -7,13 +7,15 @@ class IsError extends Function
public IsError (Sheet parent, String text)
{
super (parent, text);
cell = parent.getCell (functionText);
}
@Override
public void calculate ()
{
if (cell == null)
cell = parent.getCell (functionText);
// if (cell == null)
// cell = parent.getCell (functionText);
value = cell == null ? 1 : cell.isValueType (ValueType.ERROR) ? 1 : 0;
valueType = ValueType.VALUE;

View File

@ -2,18 +2,20 @@ package com.bytezone.diskbrowser.visicalc;
public class IsNa extends Function
{
Expression expression;
Value expression;
IsNa (Sheet parent, String text)
{
super (parent, text);
expression = new Expression (parent, functionText).reduce ();
}
@Override
public void calculate ()
{
if (expression == null)
expression = new Expression (parent, functionText);
// if (expression == null)
// expression = new Expression (parent, functionText);
expression.calculate ();
value = expression.getValue ();

View File

@ -2,10 +2,10 @@ package com.bytezone.diskbrowser.visicalc;
class Lookup extends Function
{
protected final Range range;
String sourceText;
String rangeText;
Expression source;
private final String sourceText;
private final String rangeText;
private final Expression source;
private final Range range;
public Lookup (Sheet parent, String text)
{
@ -14,9 +14,10 @@ class Lookup extends Function
int pos = text.indexOf (',');
sourceText = text.substring (8, pos);
source = new Expression (parent, sourceText);
values.add (source);
rangeText = text.substring (pos + 1, text.length () - 1);
range = new Range (parent, rangeText);
values.add (source);
}
@Override
@ -47,7 +48,6 @@ class Lookup extends Function
target = address;
}
// System.out.printf ("*****-----**** %s%n", target);
if (target == null)
valueType = ValueType.NA;
else
@ -57,9 +57,7 @@ class Lookup extends Function
if (parent.cellExists (adjacentAddress))
{
Cell adjacentCell = parent.getCell (adjacentAddress);
if (adjacentCell != null)
value = adjacentCell.getValue ();
value = parent.getCell (adjacentAddress).getValue ();
valueType = ValueType.VALUE;
}
else

View File

@ -7,6 +7,7 @@ class Max extends Function
public Max (Sheet parent, String text)
{
super (parent, text);
range = new Range (parent, text);
}
@ -19,7 +20,7 @@ class Max extends Function
for (Address address : range)
{
Cell cell = parent.getCell (address);
if (cell == null || cell.isValueType (ValueType.NA))
if (cell.isValueType (ValueType.NA))
continue;
if (!cell.isValueType (ValueType.VALUE))

View File

@ -7,6 +7,7 @@ class Min extends Function
public Min (Sheet parent, String text)
{
super (parent, text);
range = new Range (parent, text);
}
@ -19,7 +20,7 @@ class Min extends Function
for (Address address : range)
{
Cell cell = parent.getCell (address);
if (cell == null || cell.isValueType (ValueType.NA))
if (cell.isValueType (ValueType.NA))
continue;
if (!cell.isValueType (ValueType.VALUE))

View File

@ -11,6 +11,7 @@ public class Npv extends Function
Npv (Sheet parent, String text)
{
super (parent, text);
range = new Range (parent, text);
// int pos = text.indexOf (',');
@ -29,7 +30,7 @@ public class Npv extends Function
for (Address address : range)
{
Cell cell = parent.getCell (address);
if (cell == null || cell.isValueType (ValueType.NA))
if (cell.isValueType (ValueType.NA))
continue;
if (!cell.isValueType (ValueType.VALUE))

View File

@ -5,11 +5,12 @@ import java.util.List;
class Or extends Function
{
List<Condition> conditions = new ArrayList<Condition> ();
private final List<Condition> conditions = new ArrayList<Condition> ();
public Or (Sheet parent, String text)
{
super (parent, text);
String list[] = text.split (",");
for (String s : list)
conditions.add (new Condition (parent, s));

View File

@ -5,6 +5,8 @@ class Pi extends Function
Pi (Sheet parent, String text)
{
super (parent, text);
value = Math.PI;
valueType = ValueType.VALUE;
}
}

View File

@ -13,9 +13,9 @@ class Range implements Iterable<Address>
Pattern.compile ("([A-B]?[A-Z])([0-9]{1,3})\\.\\.\\.([A-B]?[A-Z])([0-9]{1,3})");
private static final Pattern addressList = Pattern.compile ("\\(([^,]+(,[^,]+)*)\\)");
Address from, to;
List<Address> range = new ArrayList<Address> ();
Sheet parent;
private Address from, to;
private final List<Address> range = new ArrayList<Address> ();
private final Sheet parent;
public Range (Sheet parent, String rangeText)
{
@ -57,13 +57,13 @@ class Range implements Iterable<Address>
range.add (from);
Address tempFrom = from;
if (from.row == to.row)
if (from.rowMatches (to))
while (from.compareTo (to) < 0)
{
from = from.nextColumn ();
range.add (from);
}
else if (from.column == to.column)
else if (from.columnMatches (to))
while (from.compareTo (to) < 0)
{
from = from.nextRow ();
@ -79,14 +79,14 @@ class Range implements Iterable<Address>
{
Address first = range.get (0);
Address last = range.get (range.size () - 1);
return first.row == last.row;
return first.rowMatches (last);
}
boolean isVertical ()
{
Address first = range.get (0);
Address last = range.get (range.size () - 1);
return first.column == last.column;
return first.columnMatches (last);
}
@Override
@ -148,11 +148,11 @@ class Range implements Iterable<Address>
{
StringBuilder text = new StringBuilder ();
for (Address address : range)
text.append (address.text + ",");
text.append (address.getText () + ",");
if (text.length () > 0)
text.deleteCharAt (text.length () - 1);
return text.toString ();
}
return String.format (" %s -> %s", from.text, to.text);
return String.format (" %s -> %s", from.getText (), to.getText ());
}
}

View File

@ -199,7 +199,7 @@ public class Sheet
if (m.find ())
{
Address address = new Address (m.group (1), m.group (2));
currentCell = rowOrderCells.get (address.rowKey);
currentCell = rowOrderCells.get (address.getRowKey ());
int pos = line.indexOf (':'); // end of cell address
line = line.substring (pos + 1); // remove address from line
@ -224,7 +224,7 @@ public class Sheet
if (line.charAt (2) == 'C' && line.charAt (3) == 'C')
{
int width = Integer.parseInt (line.substring (4));
columnWidths.put (currentCell.getAddress ().column, width);
columnWidths.put (currentCell.getAddress ().getColumn (), width);
}
else
System.out.printf ("Unknown Global:[%s]%n", line);
@ -262,11 +262,11 @@ public class Sheet
private void addCell (Cell cell)
{
// System.out.printf ("Adding: %s%n", cell);
rowOrderCells.put (cell.getAddress ().rowKey, cell);
columnOrderCells.put (cell.getAddress ().columnKey, cell);
rowOrderCells.put (cell.getAddress ().getRowKey (), cell);
columnOrderCells.put (cell.getAddress ().getColumnKey (), cell);
highestRow = Math.max (highestRow, cell.getAddress ().row);
highestColumn = Math.max (highestColumn, cell.getAddress ().column);
highestRow = Math.max (highestRow, cell.getAddress ().getRow ());
highestColumn = Math.max (highestColumn, cell.getAddress ().getColumn ());
}
Cell getCell (String addressText)
@ -276,7 +276,7 @@ public class Sheet
Cell getCell (Address address)
{
Cell cell = rowOrderCells.get (address.rowKey);
Cell cell = rowOrderCells.get (address.getRowKey ());
if (cell == null)
{
// System.out.printf ("cell not found, creating: %s%n", address);
@ -288,7 +288,7 @@ public class Sheet
boolean cellExists (Address address)
{
return rowOrderCells.get (address.rowKey) != null;
return rowOrderCells.get (address.getRowKey ()) != null;
}
public int size ()
@ -397,7 +397,7 @@ public class Sheet
Address cellAddress = cell.getAddress ();
// insert newlines for empty rows
while (lastRow < cellAddress.row)
while (lastRow < cellAddress.getRow ())
{
++lastRow;
lastColumn = 0;
@ -408,7 +408,7 @@ public class Sheet
}
// pad out empty columns
while (lastColumn < cellAddress.column)
while (lastColumn < cellAddress.getColumn ())
{
int width = columnWidth;
if (columnWidths.containsKey (lastColumn))
@ -420,8 +420,8 @@ public class Sheet
++lastColumn;
int colWidth = columnWidth;
if (columnWidths.containsKey (cellAddress.column))
colWidth = columnWidths.get (cellAddress.column);
if (columnWidths.containsKey (cellAddress.getColumn ()))
colWidth = columnWidths.get (cellAddress.getColumn ());
text.append (cell.getText (colWidth, globalFormat));
}
@ -432,13 +432,13 @@ public class Sheet
int last = -1;
for (Cell cell : columnOrderCells.values ())
{
if (last < cell.getAddress ().column)
if (last < cell.getAddress ().getColumn ())
{
String columnName = Address.getCellName (1, cell.getAddress ().column);
String columnName = Address.getCellName (1, cell.getAddress ().getColumn ());
columnName = columnName.substring (0, columnName.length () - 1);
text.append ("\n *** Column " + columnName
+ " ***\n\n");
last = cell.getAddress ().column;
last = cell.getAddress ().getColumn ();
}
text.append (cell.getDebugText ());
text.append ("\n");

View File

@ -7,6 +7,7 @@ class Sum extends Function
public Sum (Sheet parent, String text)
{
super (parent, text);
range = new Range (parent, text);
}
@ -19,7 +20,8 @@ class Sum extends Function
for (Address address : range)
{
Cell cell = parent.getCell (address);
if (cell == null || cell.isValueType (ValueType.NA))
if (cell.isValueType (ValueType.NA))
continue;
if (!cell.isValueType (ValueType.VALUE))