Added RangeFunction

This commit is contained in:
Denis Molony 2016-08-01 09:52:47 +10:00
parent dfd60dec78
commit 732e883e3b
8 changed files with 73 additions and 83 deletions

View File

@ -1,13 +1,10 @@
package com.bytezone.diskbrowser.visicalc;
class Count extends Function
class Count extends RangeFunction
{
private final Range range;
public Count (Sheet parent, String text)
{
super (parent, text);
range = getRange (text);
}
@Override

View File

@ -1,8 +1,5 @@
package com.bytezone.diskbrowser.visicalc;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// http://www.bricklin.com/history/refcard1.htm
// Functions:
// @AVERAGE
@ -26,10 +23,6 @@ import java.util.regex.Pattern;
abstract class Function implements Value
{
private static final Pattern rangePattern = 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 ("\\(([^,]+(,[^,]+)*)\\)");
protected final Sheet parent;
protected String functionName;
protected String functionText;
@ -158,46 +151,6 @@ abstract class Function implements Value
return isNotAvailable () ? "NA" : isError () ? "Error" : isNotANumber () ? "NaN" : "";
}
protected Range getRange (String text)
{
Range range = null;
Matcher m = rangePattern.matcher (text);
if (m.find ())
{
Address fromAddress = new Address (m.group (1), m.group (2));
Address toAddress = new Address (m.group (3), m.group (4));
range = new Range (fromAddress, toAddress);
}
if (range != null)
return range;
m = addressList.matcher (text);
if (m.find ())
{
String[] cells = m.group (1).split (",");
range = new Range (cells);
}
if (range != null)
return range;
int pos = text.indexOf ("...");
if (pos > 0)
{
String from = text.substring (0, pos);
String to = text.substring (pos + 3);
Address fromAddress = new Address (from);
Address toAddress = new Address (to);
range = new Range (fromAddress, toAddress);
}
if (range == null)
System.out.printf ("null range [%s]%n", text);
return range;
}
@Override
public String toString ()
{

View File

@ -1,8 +1,7 @@
package com.bytezone.diskbrowser.visicalc;
class Lookup extends Function
class Lookup extends RangeFunction
{
Range range;
String sourceText;
String rangeText;
Expression source;
@ -20,13 +19,9 @@ class Lookup extends Function
public Value calculate ()
{
if (source == null)
{
source = new Expression (parent, sourceText);
range = getRange (rangeText);
}
source.calculate ();
// System.out.println ("calculated source");
if (source.isError () || source.isNotAvailable ())
{
valueType = source.getValueType ();
@ -53,9 +48,7 @@ class Lookup extends Function
valueType = ValueType.VALUE;
}
else
{
System.out.println ("Target is null!");
}
return this;
}

View File

@ -1,13 +1,10 @@
package com.bytezone.diskbrowser.visicalc;
class Max extends Function
class Max extends RangeFunction
{
private final Range range;
public Max (Sheet parent, String text)
{
super (parent, text);
range = getRange (text);
}
@Override

View File

@ -1,13 +1,10 @@
package com.bytezone.diskbrowser.visicalc;
class Min extends Function
class Min extends RangeFunction
{
private final Range range;
public Min (Sheet parent, String text)
{
super (parent, text);
range = getRange (text);
}
@Override

View File

@ -1,23 +1,21 @@
package com.bytezone.diskbrowser.visicalc;
public class Npv extends Function
public class Npv extends RangeFunction
{
private final String valueText;
private final String rangeText;
private final Expression valueExp;
private final Range range;
// private final String valueText;
// private final String rangeText;
//
// private final Expression valueExp;
Npv (Sheet parent, String text)
{
super (parent, text);
int pos = text.indexOf (',');
valueText = text.substring (8, pos);
rangeText = text.substring (pos + 1, text.length () - 1);
valueExp = new Expression (parent, valueText);
range = getRange (rangeText);
// int pos = text.indexOf (',');
// valueText = text.substring (8, pos);
// rangeText = text.substring (pos + 1, text.length () - 1);
//
// valueExp = new Expression (parent, valueText);
}
@Override

View File

@ -0,0 +1,58 @@
package com.bytezone.diskbrowser.visicalc;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class RangeFunction extends Function
{
private static final Pattern rangePattern = 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 ("\\(([^,]+(,[^,]+)*)\\)");
protected final Range range;
public RangeFunction (Sheet parent, String text)
{
super (parent, text);
range = getRange (text);
}
protected Range getRange (String text)
{
Range range = null;
Matcher m = rangePattern.matcher (text);
if (m.find ())
{
Address fromAddress = new Address (m.group (1), m.group (2));
Address toAddress = new Address (m.group (3), m.group (4));
range = new Range (fromAddress, toAddress);
}
if (range != null)
return range;
m = addressList.matcher (text);
if (m.find ())
{
String[] cells = m.group (1).split (",");
range = new Range (cells);
}
if (range != null)
return range;
int pos = text.indexOf ("...");
if (pos > 0)
{
String from = text.substring (0, pos);
String to = text.substring (pos + 3);
Address fromAddress = new Address (from);
Address toAddress = new Address (to);
range = new Range (fromAddress, toAddress);
}
if (range == null)
System.out.printf ("null range [%s]%n", text);
return range;
}
}

View File

@ -1,13 +1,10 @@
package com.bytezone.diskbrowser.visicalc;
class Sum extends Function
class Sum extends RangeFunction
{
private final Range range;
public Sum (Sheet parent, String text)
{
super (parent, text);
range = getRange (text);
}
@Override