diff --git a/src/com/bytezone/diskbrowser/visicalc/Count.java b/src/com/bytezone/diskbrowser/visicalc/Count.java index 312989d..4364272 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Count.java +++ b/src/com/bytezone/diskbrowser/visicalc/Count.java @@ -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 diff --git a/src/com/bytezone/diskbrowser/visicalc/Function.java b/src/com/bytezone/diskbrowser/visicalc/Function.java index c06a6fb..b5d8afe 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Function.java +++ b/src/com/bytezone/diskbrowser/visicalc/Function.java @@ -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 () { diff --git a/src/com/bytezone/diskbrowser/visicalc/Lookup.java b/src/com/bytezone/diskbrowser/visicalc/Lookup.java index 0eb389b..fc211e3 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Lookup.java +++ b/src/com/bytezone/diskbrowser/visicalc/Lookup.java @@ -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; } diff --git a/src/com/bytezone/diskbrowser/visicalc/Max.java b/src/com/bytezone/diskbrowser/visicalc/Max.java index f8b990f..88d77fa 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Max.java +++ b/src/com/bytezone/diskbrowser/visicalc/Max.java @@ -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 diff --git a/src/com/bytezone/diskbrowser/visicalc/Min.java b/src/com/bytezone/diskbrowser/visicalc/Min.java index 8a7d82c..daa350d 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Min.java +++ b/src/com/bytezone/diskbrowser/visicalc/Min.java @@ -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 diff --git a/src/com/bytezone/diskbrowser/visicalc/Npv.java b/src/com/bytezone/diskbrowser/visicalc/Npv.java index f2be91c..a589b31 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Npv.java +++ b/src/com/bytezone/diskbrowser/visicalc/Npv.java @@ -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 diff --git a/src/com/bytezone/diskbrowser/visicalc/RangeFunction.java b/src/com/bytezone/diskbrowser/visicalc/RangeFunction.java new file mode 100644 index 0000000..9782c95 --- /dev/null +++ b/src/com/bytezone/diskbrowser/visicalc/RangeFunction.java @@ -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; + } +} \ No newline at end of file diff --git a/src/com/bytezone/diskbrowser/visicalc/Sum.java b/src/com/bytezone/diskbrowser/visicalc/Sum.java index d615e92..d97fdd9 100644 --- a/src/com/bytezone/diskbrowser/visicalc/Sum.java +++ b/src/com/bytezone/diskbrowser/visicalc/Sum.java @@ -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