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; package com.bytezone.diskbrowser.visicalc;
class Count extends Function class Count extends RangeFunction
{ {
private final Range range;
public Count (Sheet parent, String text) public Count (Sheet parent, String text)
{ {
super (parent, text); super (parent, text);
range = getRange (text);
} }
@Override @Override

View File

@ -1,8 +1,5 @@
package com.bytezone.diskbrowser.visicalc; package com.bytezone.diskbrowser.visicalc;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
// http://www.bricklin.com/history/refcard1.htm // http://www.bricklin.com/history/refcard1.htm
// Functions: // Functions:
// @AVERAGE // @AVERAGE
@ -26,10 +23,6 @@ import java.util.regex.Pattern;
abstract class Function implements Value 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 final Sheet parent;
protected String functionName; protected String functionName;
protected String functionText; protected String functionText;
@ -158,46 +151,6 @@ abstract class Function implements Value
return isNotAvailable () ? "NA" : isError () ? "Error" : isNotANumber () ? "NaN" : ""; 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 @Override
public String toString () public String toString ()
{ {

View File

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

View File

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

View File

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

View File

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