From 796f95c0b4674c19edbbdb999fbe38e3af538053 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 18 Jan 2010 12:56:18 +0000 Subject: [PATCH] Tidy up some of the IntMapper/UnicodeString bits with generics git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@900376 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/SSTDeserializer.java | 22 +++++++++--------- .../org/apache/poi/hssf/record/SSTRecord.java | 12 +++++----- .../apache/poi/hssf/record/SSTSerializer.java | 20 ++++++++-------- .../poi/hssf/record/common/UnicodeString.java | 6 ++--- src/java/org/apache/poi/util/IntMapper.java | 23 +++++++++---------- 5 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/SSTDeserializer.java b/src/java/org/apache/poi/hssf/record/SSTDeserializer.java index c9f6569b4a..660ad0e95a 100644 --- a/src/java/org/apache/poi/hssf/record/SSTDeserializer.java +++ b/src/java/org/apache/poi/hssf/record/SSTDeserializer.java @@ -31,9 +31,9 @@ import org.apache.poi.util.IntMapper; class SSTDeserializer { - private IntMapper strings; + private IntMapper strings; - public SSTDeserializer( IntMapper strings ) + public SSTDeserializer( IntMapper strings ) { this.strings = strings; } @@ -46,14 +46,14 @@ class SSTDeserializer public void manufactureStrings( int stringCount, RecordInputStream in ) { for (int i=0;i strings, UnicodeString string ) + { + strings.add(string); + } +} diff --git a/src/java/org/apache/poi/hssf/record/SSTRecord.java b/src/java/org/apache/poi/hssf/record/SSTRecord.java index e95f155eb5..6b60d75dd7 100644 --- a/src/java/org/apache/poi/hssf/record/SSTRecord.java +++ b/src/java/org/apache/poi/hssf/record/SSTRecord.java @@ -60,7 +60,7 @@ public final class SSTRecord extends ContinuableRecord { /** according to docs ONLY SST */ private int field_2_num_unique_strings; - private IntMapper field_3_strings; + private IntMapper field_3_strings; private SSTDeserializer deserializer; @@ -73,7 +73,7 @@ public final class SSTRecord extends ContinuableRecord { { field_1_num_strings = 0; field_2_num_unique_strings = 0; - field_3_strings = new IntMapper(); + field_3_strings = new IntMapper(); deserializer = new SSTDeserializer(field_3_strings); } @@ -130,7 +130,7 @@ public final class SSTRecord extends ContinuableRecord { */ public UnicodeString getString(int id ) { - return (UnicodeString) field_3_strings.get( id ); + return field_3_strings.get( id ); } @@ -149,7 +149,7 @@ public final class SSTRecord extends ContinuableRecord { .append( Integer.toHexString( getNumUniqueStrings() ) ).append( "\n" ); for ( int k = 0; k < field_3_strings.size(); k++ ) { - UnicodeString s = (UnicodeString)field_3_strings.get( k ); + UnicodeString s = field_3_strings.get( k ); buffer.append( " .string_" + k + " = " ) .append( s.getDebugInfo() ).append( "\n" ); } @@ -245,7 +245,7 @@ public final class SSTRecord extends ContinuableRecord { // we initialize our fields field_1_num_strings = in.readInt(); field_2_num_unique_strings = in.readInt(); - field_3_strings = new IntMapper(); + field_3_strings = new IntMapper(); deserializer = new SSTDeserializer(field_3_strings); deserializer.manufactureStrings( field_2_num_unique_strings, in ); } @@ -255,7 +255,7 @@ public final class SSTRecord extends ContinuableRecord { * @return an iterator of the strings we hold. All instances are * UnicodeStrings */ - Iterator getStrings() + Iterator getStrings() { return field_3_strings.iterator(); } diff --git a/src/java/org/apache/poi/hssf/record/SSTSerializer.java b/src/java/org/apache/poi/hssf/record/SSTSerializer.java index f1022642e4..44980f54e5 100644 --- a/src/java/org/apache/poi/hssf/record/SSTSerializer.java +++ b/src/java/org/apache/poi/hssf/record/SSTSerializer.java @@ -32,7 +32,7 @@ final class SSTSerializer { private final int _numStrings; private final int _numUniqueStrings; - private final IntMapper strings; + private final IntMapper strings; /** Offsets from the beginning of the SST record (even across continuations) */ private final int[] bucketAbsoluteOffsets; @@ -40,7 +40,7 @@ final class SSTSerializer { private final int[] bucketRelativeOffsets; int startOfSST, startOfRecord; - public SSTSerializer( IntMapper strings, int numStrings, int numUniqueStrings ) + public SSTSerializer( IntMapper strings, int numStrings, int numUniqueStrings ) { this.strings = strings; _numStrings = numStrings; @@ -59,14 +59,14 @@ final class SSTSerializer { { if (k % ExtSSTRecord.DEFAULT_BUCKET_SIZE == 0) { - int rOff = out.getTotalSize(); + int rOff = out.getTotalSize(); int index = k/ExtSSTRecord.DEFAULT_BUCKET_SIZE; if (index < ExtSSTRecord.MAX_BUCKETS) { - //Excel only indexes the first 128 buckets. - bucketAbsoluteOffsets[index] = rOff; - bucketRelativeOffsets[index] = rOff; - } - } + //Excel only indexes the first 128 buckets. + bucketAbsoluteOffsets[index] = rOff; + bucketRelativeOffsets[index] = rOff; + } + } UnicodeString s = getUnicodeString(k); s.serialize(out); } @@ -78,9 +78,9 @@ final class SSTSerializer { return getUnicodeString(strings, index); } - private static UnicodeString getUnicodeString( IntMapper strings, int index ) + private static UnicodeString getUnicodeString( IntMapper strings, int index ) { - return ( (UnicodeString) strings.get( index ) ); + return ( strings.get( index ) ); } public int[] getBucketAbsoluteOffsets() diff --git a/src/java/org/apache/poi/hssf/record/common/UnicodeString.java b/src/java/org/apache/poi/hssf/record/common/UnicodeString.java index 842a7341c5..bd1758e819 100644 --- a/src/java/org/apache/poi/hssf/record/common/UnicodeString.java +++ b/src/java/org/apache/poi/hssf/record/common/UnicodeString.java @@ -34,10 +34,9 @@ import org.apache.poi.util.LittleEndianOutput; * Title: Unicode String

* Description: Unicode String - just standard fields that are in several records. * It is considered more desirable then repeating it in all of them.

+ * This is often called a XLUnicodeRichExtendedString in MS documentation.

* REFERENCE: PG 264 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)

- * @author Andrew C. Oliver - * @author Marc Johnson (mjohnson at apache dot org) - * @author Glen Stampoultzis (glens at apache.org) + * REFERENCE: PG 951 Excel Binary File Format (.xls) Structure Specification v20091214 */ public final class UnicodeString implements Comparable { private short field_1_charCount; @@ -46,6 +45,7 @@ public final class UnicodeString implements Comparable { private List field_4_format_runs; private byte[] field_5_ext_rst; private static final BitField highByte = BitFieldFactory.getInstance(0x1); + // 0x2 is reserved private static final BitField extBit = BitFieldFactory.getInstance(0x4); private static final BitField richText = BitFieldFactory.getInstance(0x8); diff --git a/src/java/org/apache/poi/util/IntMapper.java b/src/java/org/apache/poi/util/IntMapper.java index e4b7d34507..d5cdc7b67b 100644 --- a/src/java/org/apache/poi/util/IntMapper.java +++ b/src/java/org/apache/poi/util/IntMapper.java @@ -34,10 +34,10 @@ import java.util.*; * @author Jason Height */ -public class IntMapper +public class IntMapper { - private List elements; - private Map valueKeyMap; + private List elements; + private Map valueKeyMap; private static final int _default_size = 10; @@ -52,8 +52,8 @@ public class IntMapper public IntMapper(final int initialCapacity) { - elements = new ArrayList(initialCapacity); - valueKeyMap = new HashMap(initialCapacity); + elements = new ArrayList(initialCapacity); + valueKeyMap = new HashMap(initialCapacity); } /** @@ -64,12 +64,11 @@ public class IntMapper * @return true (as per the general contract of the Collection.add * method). */ - - public boolean add(final Object value) + public boolean add(final T value) { int index = elements.size(); elements.add(value); - valueKeyMap.put(value, Integer.valueOf(index)); + valueKeyMap.put(value, index); return true; } @@ -77,18 +76,18 @@ public class IntMapper return elements.size(); } - public Object get(int index) { + public T get(int index) { return elements.get(index); } - public int getIndex(Object o) { - Integer i = ((Integer)valueKeyMap.get(o)); + public int getIndex(T o) { + Integer i = valueKeyMap.get(o); if (i == null) return -1; return i.intValue(); } - public Iterator iterator() { + public Iterator iterator() { return elements.iterator(); } } // end public class IntMapper -- 2.39.5