poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r703100 - in /poi/trunk/src: java/org/apache/poi/hssf/record/ java/org/apache/poi/hssf/record/formula/ testcases/org/apache/poi/hssf/record/ testcases/org/apache/poi/hssf/record/formula/
Date Thu, 09 Oct 2008 08:33:54 GMT
Author: josh
Date: Thu Oct  9 01:33:54 2008
New Revision: 703100

URL: http://svn.apache.org/viewvc?rev=703100&view=rev
Log:
Removed last occurrences of storing Ptg arrays in Stacks.  Some related clean-up.

Removed:
    poi/trunk/src/java/org/apache/poi/hssf/record/CustomField.java
Modified:
    poi/trunk/src/java/org/apache/poi/hssf/record/ExternalNameRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/LinkedDataFormulaField.java
    poi/trunk/src/java/org/apache/poi/hssf/record/LinkedDataRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/formula/Ptg.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/TestLinkedDataRecord.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestReferencePtg.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/ExternalNameRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/ExternalNameRecord.java?rev=703100&r1=703099&r2=703100&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/ExternalNameRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/ExternalNameRecord.java Thu Oct  9 01:33:54
2008
@@ -17,8 +17,6 @@
 
 package org.apache.poi.hssf.record;
 
-import java.util.Stack;
-
 import org.apache.poi.hssf.record.formula.Ptg;
 import org.apache.poi.util.LittleEndian;
 import org.apache.poi.util.StringUtil;
@@ -124,11 +122,7 @@
 	}
 
 	private int getNameDefinitionSize() {
-		int result = 0;
-		for (int i = 0; i < field_5_name_definition.length; i++) {
-			result += field_5_name_definition[i].getSize();
-		}
-		return result;
+		return Ptg.getEncodedSize(field_5_name_definition);
 	}
 
 

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/LinkedDataFormulaField.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/LinkedDataFormulaField.java?rev=703100&r1=703099&r2=703100&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/LinkedDataFormulaField.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/LinkedDataFormulaField.java Thu Oct  9 01:33:54
2008
@@ -20,47 +20,38 @@
 import org.apache.poi.hssf.record.formula.Ptg;
 import org.apache.poi.util.LittleEndian;
 
-import java.util.Stack;
-import java.util.Iterator;
-
 /**
  * Not implemented yet. May commit it anyway just so people can see
  * where I'm heading.
  *
  * @author Glen Stampoultzis (glens at apache.org)
  */
-public final class LinkedDataFormulaField implements CustomField {
-    Stack formulaTokens = new Stack();
+public final class LinkedDataFormulaField {
+    private Ptg[] formulaTokens;
 
     public int getSize()
     {
-        int size = 0;
-        for ( Iterator iterator = formulaTokens.iterator(); iterator.hasNext(); )
-        {
-            Ptg token = (Ptg) iterator.next();
-            size += token.getSize();
-        }
-        return size + 2;
+        return 2 + Ptg.getEncodedSize(formulaTokens);
     }
 
     public int fillField( RecordInputStream in )
     {
-        short tokenSize = in.readShort();
-        formulaTokens = Ptg.createParsedExpressionTokens(tokenSize, in);
-
+        int tokenSize = in.readUShort();
+        formulaTokens = Ptg.readTokens(tokenSize, in);
         return tokenSize + 2;
     }
 
     public void toString( StringBuffer buffer )
     {
-        for ( int k = 0; k < formulaTokens.size(); k++ )
+        for ( int k = 0; k < formulaTokens.length; k++ )
         {
+        	Ptg ptg = formulaTokens[k];
             buffer.append( "Formula " )
                     .append( k )
                     .append( "=" )
-                    .append( formulaTokens.get( k ).toString() )
+                    .append(ptg.toString() )
                     .append( "\n" )
-                    .append( ( (Ptg) formulaTokens.get( k ) ).toDebugString() )
+                    .append(ptg.toDebugString() )
                     .append( "\n" );
         }
     }
@@ -75,34 +66,26 @@
     public int serializeField( int offset, byte[] data )
     {
         int size = getSize();
-        LittleEndian.putShort(data, offset, (short)(size - 2));
+        LittleEndian.putUShort(data, offset, size - 2);
         int pos = offset + 2;
-        pos += Ptg.serializePtgStack(formulaTokens, data, pos);
+        pos += Ptg.serializePtgs(formulaTokens, data, pos);
         return size;
     }
 
-    public Object clone()
-    {
-        try
-        {
-            // todo: clone tokens? or are they immutable?
-            return super.clone();
-        }
-        catch ( CloneNotSupportedException e )
-        {
-            // should not happen
-            return null;
-        }
-    }
-
-    public void setFormulaTokens( Stack formulaTokens )
+    public void setFormulaTokens(Ptg[] ptgs)
     {
-        this.formulaTokens = (Stack) formulaTokens.clone();
+        this.formulaTokens = (Ptg[])ptgs.clone();
     }
 
-    public Stack getFormulaTokens()
+    public Ptg[] getFormulaTokens()
     {
-        return (Stack)this.formulaTokens.clone();
+        return (Ptg[])this.formulaTokens.clone();
     }
 
+	public LinkedDataFormulaField copy() {
+		LinkedDataFormulaField result = new LinkedDataFormulaField();
+		
+		result.formulaTokens = getFormulaTokens();
+		return result;
+	}
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/LinkedDataRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/LinkedDataRecord.java?rev=703100&r1=703099&r2=703100&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/LinkedDataRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/LinkedDataRecord.java Thu Oct  9 01:33:54
2008
@@ -125,7 +125,7 @@
         rec.field_2_referenceType = field_2_referenceType;
         rec.field_3_options = field_3_options;
         rec.field_4_indexNumberFmtRecord = field_4_indexNumberFmtRecord;
-        rec.field_5_formulaOfLink = ((LinkedDataFormulaField)field_5_formulaOfLink.clone());
+        rec.field_5_formulaOfLink = field_5_formulaOfLink.copy();
         return rec;
     }
 

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/formula/Ptg.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/formula/Ptg.java?rev=703100&r1=703099&r2=703100&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/formula/Ptg.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/formula/Ptg.java Thu Oct  9 01:33:54 2008
@@ -19,10 +19,8 @@
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Stack;
 
 import org.apache.poi.hssf.record.RecordInputStream;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 
 /**
  * <tt>Ptg</tt> represents a syntactic token in a formula.  'PTG' is an acronym
for 
@@ -49,15 +47,7 @@
 	 * Extra data (beyond <tt>size</tt>) may be read if and <tt>ArrayPtg</tt>s
are present.
 	 */
 	public static Ptg[] readTokens(int size, RecordInputStream in) {
-		Stack temp = createParsedExpressionTokens((short)size, in);
-		return toPtgArray(temp);
-	}
-
-	/**
-	 * @deprecated - use readTokens()
-	 */
-	public static Stack createParsedExpressionTokens(short size, RecordInputStream in) {
-		Stack stack = new Stack();
+		List temp = new ArrayList(4 + size / 2);
 		int pos = 0;
 		List arrayPtgs = null;
 		while (pos < size) {
@@ -71,7 +61,7 @@
 			} else {
 				pos += ptg.getSize();
 			}
-			stack.push( ptg );
+			temp.add( ptg );
 		}
 		if(pos != size) {
 			throw new RuntimeException("Ptg array size mismatch");
@@ -82,7 +72,7 @@
 				p.readTokenValues(in);
 			}
 		}
-		return stack;
+		return toPtgArray(temp);
 	}
 
 	public static Ptg createPtg(RecordInputStream in) {
@@ -200,19 +190,11 @@
 		l.toArray(result);
 		return result;
 	}
-	private static Stack createStack(Ptg[] formulaTokens) {
-		Stack result = new Stack();
-		for (int i = 0; i < formulaTokens.length; i++) {
-			result.add(formulaTokens[i]);
-		} 
-		return result;
-	}
 	/**
 	 * This method will return the same result as {@link #getEncodedSizeWithoutArrayData(Ptg[])}

 	 * if there are no array tokens present.
 	 * @return the full size taken to encode the specified <tt>Ptg</tt>s 
 	 */
-	// TODO - several duplicates of this code should be refactored here
 	public static int getEncodedSize(Ptg[] ptgs) {
 		int result = 0;
 		for (int i = 0; i < ptgs.length; i++) {
@@ -243,23 +225,14 @@
 	 * The 2 byte encode length field is <b>not</b> written by this method.
 	 * @return number of bytes written
 	 */
-	public static int serializePtgs(Ptg[] ptgs, byte[] data, int offset) {
-		return serializePtgStack(createStack(ptgs), data, offset);
-	}
-
-	/**
-	 * @deprecated use serializePtgs()
-	 */
-	public static int serializePtgStack(Stack expression, byte[] array, int offset) {
+	public static int serializePtgs(Ptg[] ptgs, byte[] array, int offset) {
 		int pos = 0;
-		int size = 0;
-		if (expression != null)
-			size = expression.size();
+		int size = ptgs.length;
 
 		List arrayPtgs = null;
 
 		for (int k = 0; k < size; k++) {
-			Ptg ptg = ( Ptg ) expression.get(k);
+			Ptg ptg = ptgs[k];
 
 			ptg.writeBytes(array, pos + offset);
 			if (ptg instanceof ArrayPtg) {

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/TestLinkedDataRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/TestLinkedDataRecord.java?rev=703100&r1=703099&r2=703100&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/TestLinkedDataRecord.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/TestLinkedDataRecord.java Thu Oct 
9 01:33:54 2008
@@ -19,9 +19,9 @@
 
 
 import junit.framework.TestCase;
-import org.apache.poi.hssf.record.formula.Area3DPtg;
 
-import java.util.Stack;
+import org.apache.poi.hssf.record.formula.Area3DPtg;
+import org.apache.poi.hssf.record.formula.Ptg;
 
 /**
  * Tests the serialization and deserialization of the LinkedDataRecord
@@ -167,7 +167,7 @@
         Area3DPtg ptgExpected = new Area3DPtg(0, 7936, 0, 0,
                 false, false, false, false, 0);
         
-        Object ptgActual = record.getFormulaOfLink().getFormulaTokens().get(0);
+        Object ptgActual = record.getFormulaOfLink().getFormulaTokens()[0];
         assertEquals(ptgExpected.toString(),  ptgActual.toString());
 
         assertEquals( data.length + 4, record.getRecordSize() );
@@ -182,10 +182,8 @@
         record.setIndexNumberFmtRecord( (short)0 );
         Area3DPtg ptg = new Area3DPtg(0, 7936, 0, 0,
         		false, false, false, false, 0);
-        Stack s = new Stack();
-        s.push(ptg);
         LinkedDataFormulaField formulaOfLink = new LinkedDataFormulaField();
-        formulaOfLink.setFormulaTokens(s);
+        formulaOfLink.setFormulaTokens(new Ptg[] { ptg, });
         record.setFormulaOfLink(formulaOfLink );
 
         byte [] recordBytes = record.serialize();

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestReferencePtg.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestReferencePtg.java?rev=703100&r1=703099&r2=703100&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestReferencePtg.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/formula/TestReferencePtg.java Thu Oct
 9 01:33:54 2008
@@ -18,7 +18,6 @@
 package org.apache.poi.hssf.record.formula;
 
 import java.util.Arrays;
-import java.util.Stack;
 
 import junit.framework.AssertionFailedError;
 import junit.framework.TestCase;
@@ -95,9 +94,9 @@
     };
     public void testReadWrite_tRefN_bug45091() {
         TestcaseRecordInputStream in = new TestcaseRecordInputStream(-1, tRefN_data);
-        Stack ptgs = Ptg.createParsedExpressionTokens((short)tRefN_data.length, in);
+        Ptg[] ptgs = Ptg.readTokens(tRefN_data.length, in);
         byte[] outData = new byte[5];
-        Ptg.serializePtgStack(ptgs, outData, 0);
+        Ptg.serializePtgs(ptgs, outData, 0);
         if (outData[0] == 0x24) {
             throw new AssertionFailedError("Identified bug 45091");
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message