From cb842b1165de73fd6bd8bd463180ab0b89f7a6fe Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Fri, 8 Aug 2008 19:49:02 +0000 Subject: [PATCH] Standardised toString methods on ScalarConstantPtg subclasses git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@684067 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/formula/BoolPtg.java | 29 +++++++------------ .../poi/hssf/record/formula/ErrPtg.java | 3 +- .../poi/hssf/record/formula/IntPtg.java | 16 ++-------- .../hssf/record/formula/MissingArgPtg.java | 16 ++++------ .../poi/hssf/record/formula/NumberPtg.java | 26 ++++------------- .../record/formula/ScalarConstantPtg.java | 23 +++++++++++++-- .../poi/hssf/record/formula/StringPtg.java | 15 ++-------- 7 files changed, 49 insertions(+), 79 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/formula/BoolPtg.java b/src/java/org/apache/poi/hssf/record/formula/BoolPtg.java index ce8a6ac409..ae2fd541cc 100644 --- a/src/java/org/apache/poi/hssf/record/formula/BoolPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/BoolPtg.java @@ -17,7 +17,6 @@ package org.apache.poi.hssf.record.formula; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.record.RecordInputStream; /** @@ -30,36 +29,30 @@ import org.apache.poi.hssf.record.RecordInputStream; public final class BoolPtg extends ScalarConstantPtg { public final static int SIZE = 2; public final static byte sid = 0x1d; - private final boolean field_1_value; + private final boolean _value; - public BoolPtg(RecordInputStream in) - { - field_1_value = (in.readByte() == 1); + public BoolPtg(RecordInputStream in) { + _value = (in.readByte() == 1); } - public BoolPtg(String formulaToken) { - field_1_value = (formulaToken.equals("TRUE")); + _value = (formulaToken.equalsIgnoreCase("TRUE")); } - public boolean getValue() - { - return field_1_value; + public boolean getValue() { + return _value; } - public void writeBytes(byte [] array, int offset) - { + public void writeBytes(byte [] array, int offset) { array[ offset + 0 ] = sid; - array[ offset + 1 ] = (byte) (field_1_value ? 1 : 0); + array[ offset + 1 ] = (byte) (_value ? 1 : 0); } - public int getSize() - { + public int getSize() { return SIZE; } - public String toFormulaString(HSSFWorkbook book) - { - return field_1_value ? "TRUE" : "FALSE"; + protected String toFormulaString() { + return _value ? "TRUE" : "FALSE"; } } diff --git a/src/java/org/apache/poi/hssf/record/formula/ErrPtg.java b/src/java/org/apache/poi/hssf/record/formula/ErrPtg.java index 7e67343bd3..aef580d5e4 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ErrPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ErrPtg.java @@ -17,7 +17,6 @@ package org.apache.poi.hssf.record.formula; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.hssf.usermodel.HSSFErrorConstants; @@ -68,7 +67,7 @@ public final class ErrPtg extends ScalarConstantPtg { array[offset + 1] = (byte)field_1_error_code; } - public String toFormulaString(HSSFWorkbook book) { + protected String toFormulaString() { return HSSFErrorConstants.getText(field_1_error_code); } diff --git a/src/java/org/apache/poi/hssf/record/formula/IntPtg.java b/src/java/org/apache/poi/hssf/record/formula/IntPtg.java index 89081ffe07..c4abb7b1bc 100644 --- a/src/java/org/apache/poi/hssf/record/formula/IntPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/IntPtg.java @@ -17,9 +17,8 @@ package org.apache.poi.hssf.record.formula; -import org.apache.poi.util.LittleEndian; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.record.RecordInputStream; +import org.apache.poi.util.LittleEndian; /** * Integer (unsigned short integer) @@ -60,8 +59,7 @@ public final class IntPtg extends ScalarConstantPtg { return field_1_value; } - public void writeBytes(byte [] array, int offset) - { + public void writeBytes(byte [] array, int offset) { array[ offset + 0 ] = sid; LittleEndian.putUShort(array, offset + 1, getValue()); } @@ -70,15 +68,7 @@ public final class IntPtg extends ScalarConstantPtg { return SIZE; } - public String toFormulaString(HSSFWorkbook book) { + protected String toFormulaString() { return String.valueOf(getValue()); } - - public String toString() { - StringBuffer sb = new StringBuffer(64); - sb.append(getClass().getName()).append(" ["); - sb.append(field_1_value); - sb.append("]"); - return sb.toString(); - } } diff --git a/src/java/org/apache/poi/hssf/record/formula/MissingArgPtg.java b/src/java/org/apache/poi/hssf/record/formula/MissingArgPtg.java index ed49520b50..ffcf423b4f 100644 --- a/src/java/org/apache/poi/hssf/record/formula/MissingArgPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/MissingArgPtg.java @@ -17,8 +17,6 @@ package org.apache.poi.hssf.record.formula; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; - /** * Missing Function Arguments * @@ -31,22 +29,20 @@ public final class MissingArgPtg extends ScalarConstantPtg { public final static byte sid = 0x16; public static final Ptg instance = new MissingArgPtg(); - private MissingArgPtg() - { + + private MissingArgPtg() { + // enforce singleton } - public void writeBytes(byte [] array, int offset) - { + public void writeBytes(byte [] array, int offset) { array[ offset + 0 ] = sid; } - public int getSize() - { + public int getSize() { return SIZE; } - public String toFormulaString(HSSFWorkbook book) - { + protected String toFormulaString() { return " "; } } diff --git a/src/java/org/apache/poi/hssf/record/formula/NumberPtg.java b/src/java/org/apache/poi/hssf/record/formula/NumberPtg.java index 84d4a1704d..225de3d535 100644 --- a/src/java/org/apache/poi/hssf/record/formula/NumberPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/NumberPtg.java @@ -17,9 +17,8 @@ package org.apache.poi.hssf.record.formula; -import org.apache.poi.util.LittleEndian; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.record.RecordInputStream; +import org.apache.poi.util.LittleEndian; /** * Number @@ -34,8 +33,7 @@ public final class NumberPtg extends ScalarConstantPtg { private final double field_1_value; /** Create a NumberPtg from a byte array read from disk */ - public NumberPtg(RecordInputStream in) - { + public NumberPtg(RecordInputStream in) { this(in.readDouble()); } @@ -52,33 +50,21 @@ public final class NumberPtg extends ScalarConstantPtg { field_1_value = value; } - public double getValue() - { + public double getValue() { return field_1_value; } - public void writeBytes(byte [] array, int offset) - { + public void writeBytes(byte [] array, int offset) { array[ offset + 0 ] = sid; LittleEndian.putDouble(array, offset + 1, getValue()); } - public int getSize() - { + public int getSize() { return SIZE; } - public String toFormulaString(HSSFWorkbook book) - { + protected String toFormulaString() { // TODO - java's rendering of double values is not quite same as excel's return String.valueOf(field_1_value); } - - public String toString() { - StringBuffer sb = new StringBuffer(64); - sb.append(getClass().getName()).append(" ["); - sb.append(field_1_value); - sb.append("]"); - return sb.toString(); - } } diff --git a/src/java/org/apache/poi/hssf/record/formula/ScalarConstantPtg.java b/src/java/org/apache/poi/hssf/record/formula/ScalarConstantPtg.java index 8b00d327fc..fc5653cdeb 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ScalarConstantPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ScalarConstantPtg.java @@ -17,6 +17,8 @@ package org.apache.poi.hssf.record.formula; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; + /** * @author Josh Micich */ @@ -24,7 +26,22 @@ abstract class ScalarConstantPtg extends Ptg { public boolean isBaseToken() { return true; } - public final byte getDefaultOperandClass() { - return Ptg.CLASS_VALUE; - } + + public final byte getDefaultOperandClass() { + return Ptg.CLASS_VALUE; + } + + public final String toFormulaString(HSSFWorkbook book) { + return toFormulaString(); + } + + protected abstract String toFormulaString(); + + public final String toString() { + StringBuffer sb = new StringBuffer(64); + sb.append(getClass().getName()).append(" ["); + sb.append(toFormulaString()); + sb.append("]"); + return sb.toString(); + } } diff --git a/src/java/org/apache/poi/hssf/record/formula/StringPtg.java b/src/java/org/apache/poi/hssf/record/formula/StringPtg.java index 9102b083d5..7007b48fdb 100644 --- a/src/java/org/apache/poi/hssf/record/formula/StringPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/StringPtg.java @@ -17,11 +17,10 @@ package org.apache.poi.hssf.record.formula; -import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.hssf.record.RecordInputStream; import org.apache.poi.util.BitField; import org.apache.poi.util.BitFieldFactory; import org.apache.poi.util.StringUtil; -import org.apache.poi.hssf.record.RecordInputStream; /** * String Stores a String value in a formula value stored in the format @@ -55,8 +54,6 @@ public final class StringPtg extends ScalarConstantPtg { } else { field_3_string = in.readCompressedUnicode(field_1_length); } - - // setValue(new String(data, offset+3, data[offset+1] + 256*data[offset+2])); } /** @@ -100,7 +97,7 @@ public final class StringPtg extends ScalarConstantPtg { } } - public String toFormulaString(HSSFWorkbook book) { + protected String toFormulaString() { String value = field_3_string; int len = value.length(); StringBuffer sb = new StringBuffer(len + 4); @@ -117,12 +114,4 @@ public final class StringPtg extends ScalarConstantPtg { sb.append(FORMULA_DELIMITER); return sb.toString(); } - - public String toString() { - StringBuffer sb = new StringBuffer(64); - sb.append(getClass().getName()).append(" ["); - sb.append(field_3_string); - sb.append("]"); - return sb.toString(); - } }