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
This commit is contained in:
Nick Burch 2010-01-18 12:56:18 +00:00
parent 6605995ac6
commit 0111e33db5
5 changed files with 41 additions and 42 deletions

View File

@ -31,9 +31,9 @@ import org.apache.poi.util.IntMapper;
class SSTDeserializer class SSTDeserializer
{ {
private IntMapper strings; private IntMapper<UnicodeString> strings;
public SSTDeserializer( IntMapper strings ) public SSTDeserializer( IntMapper<UnicodeString> strings )
{ {
this.strings = strings; this.strings = strings;
} }
@ -46,14 +46,14 @@ class SSTDeserializer
public void manufactureStrings( int stringCount, RecordInputStream in ) public void manufactureStrings( int stringCount, RecordInputStream in )
{ {
for (int i=0;i<stringCount;i++) { for (int i=0;i<stringCount;i++) {
//Extract exactly the count of strings from the SST record. // Extract exactly the count of strings from the SST record.
UnicodeString str = new UnicodeString(in); UnicodeString str = new UnicodeString(in);
addToStringTable( strings, str ); addToStringTable( strings, str );
} }
} }
static public void addToStringTable( IntMapper strings, UnicodeString string ) static public void addToStringTable( IntMapper<UnicodeString> strings, UnicodeString string )
{ {
strings.add(string ); strings.add(string);
}
} }
}

View File

@ -60,7 +60,7 @@ public final class SSTRecord extends ContinuableRecord {
/** according to docs ONLY SST */ /** according to docs ONLY SST */
private int field_2_num_unique_strings; private int field_2_num_unique_strings;
private IntMapper field_3_strings; private IntMapper<UnicodeString> field_3_strings;
private SSTDeserializer deserializer; private SSTDeserializer deserializer;
@ -73,7 +73,7 @@ public final class SSTRecord extends ContinuableRecord {
{ {
field_1_num_strings = 0; field_1_num_strings = 0;
field_2_num_unique_strings = 0; field_2_num_unique_strings = 0;
field_3_strings = new IntMapper(); field_3_strings = new IntMapper<UnicodeString>();
deserializer = new SSTDeserializer(field_3_strings); deserializer = new SSTDeserializer(field_3_strings);
} }
@ -130,7 +130,7 @@ public final class SSTRecord extends ContinuableRecord {
*/ */
public UnicodeString getString(int id ) 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" ); .append( Integer.toHexString( getNumUniqueStrings() ) ).append( "\n" );
for ( int k = 0; k < field_3_strings.size(); k++ ) 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 + " = " ) buffer.append( " .string_" + k + " = " )
.append( s.getDebugInfo() ).append( "\n" ); .append( s.getDebugInfo() ).append( "\n" );
} }
@ -245,7 +245,7 @@ public final class SSTRecord extends ContinuableRecord {
// we initialize our fields // we initialize our fields
field_1_num_strings = in.readInt(); field_1_num_strings = in.readInt();
field_2_num_unique_strings = in.readInt(); field_2_num_unique_strings = in.readInt();
field_3_strings = new IntMapper(); field_3_strings = new IntMapper<UnicodeString>();
deserializer = new SSTDeserializer(field_3_strings); deserializer = new SSTDeserializer(field_3_strings);
deserializer.manufactureStrings( field_2_num_unique_strings, in ); 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 * @return an iterator of the strings we hold. All instances are
* UnicodeStrings * UnicodeStrings
*/ */
Iterator getStrings() Iterator<UnicodeString> getStrings()
{ {
return field_3_strings.iterator(); return field_3_strings.iterator();
} }

View File

@ -32,7 +32,7 @@ final class SSTSerializer {
private final int _numStrings; private final int _numStrings;
private final int _numUniqueStrings; private final int _numUniqueStrings;
private final IntMapper strings; private final IntMapper<UnicodeString> strings;
/** Offsets from the beginning of the SST record (even across continuations) */ /** Offsets from the beginning of the SST record (even across continuations) */
private final int[] bucketAbsoluteOffsets; private final int[] bucketAbsoluteOffsets;
@ -40,7 +40,7 @@ final class SSTSerializer {
private final int[] bucketRelativeOffsets; private final int[] bucketRelativeOffsets;
int startOfSST, startOfRecord; int startOfSST, startOfRecord;
public SSTSerializer( IntMapper strings, int numStrings, int numUniqueStrings ) public SSTSerializer( IntMapper<UnicodeString> strings, int numStrings, int numUniqueStrings )
{ {
this.strings = strings; this.strings = strings;
_numStrings = numStrings; _numStrings = numStrings;
@ -78,9 +78,9 @@ final class SSTSerializer {
return getUnicodeString(strings, index); return getUnicodeString(strings, index);
} }
private static UnicodeString getUnicodeString( IntMapper strings, int index ) private static UnicodeString getUnicodeString( IntMapper<UnicodeString> strings, int index )
{ {
return ( (UnicodeString) strings.get( index ) ); return ( strings.get( index ) );
} }
public int[] getBucketAbsoluteOffsets() public int[] getBucketAbsoluteOffsets()

View File

@ -34,10 +34,9 @@ import org.apache.poi.util.LittleEndianOutput;
* Title: Unicode String<p/> * Title: Unicode String<p/>
* Description: Unicode String - just standard fields that are in several records. * Description: Unicode String - just standard fields that are in several records.
* It is considered more desirable then repeating it in all of them.<p/> * It is considered more desirable then repeating it in all of them.<p/>
* This is often called a XLUnicodeRichExtendedString in MS documentation.<p/>
* REFERENCE: PG 264 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<p/> * REFERENCE: PG 264 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<p/>
* @author Andrew C. Oliver * REFERENCE: PG 951 Excel Binary File Format (.xls) Structure Specification v20091214
* @author Marc Johnson (mjohnson at apache dot org)
* @author Glen Stampoultzis (glens at apache.org)
*/ */
public final class UnicodeString implements Comparable<UnicodeString> { public final class UnicodeString implements Comparable<UnicodeString> {
private short field_1_charCount; private short field_1_charCount;
@ -46,6 +45,7 @@ public final class UnicodeString implements Comparable<UnicodeString> {
private List<FormatRun> field_4_format_runs; private List<FormatRun> field_4_format_runs;
private byte[] field_5_ext_rst; private byte[] field_5_ext_rst;
private static final BitField highByte = BitFieldFactory.getInstance(0x1); private static final BitField highByte = BitFieldFactory.getInstance(0x1);
// 0x2 is reserved
private static final BitField extBit = BitFieldFactory.getInstance(0x4); private static final BitField extBit = BitFieldFactory.getInstance(0x4);
private static final BitField richText = BitFieldFactory.getInstance(0x8); private static final BitField richText = BitFieldFactory.getInstance(0x8);

View File

@ -34,10 +34,10 @@ import java.util.*;
* @author Jason Height * @author Jason Height
*/ */
public class IntMapper public class IntMapper<T>
{ {
private List elements; private List<T> elements;
private Map valueKeyMap; private Map<T,Integer> valueKeyMap;
private static final int _default_size = 10; private static final int _default_size = 10;
@ -52,8 +52,8 @@ public class IntMapper
public IntMapper(final int initialCapacity) public IntMapper(final int initialCapacity)
{ {
elements = new ArrayList(initialCapacity); elements = new ArrayList<T>(initialCapacity);
valueKeyMap = new HashMap(initialCapacity); valueKeyMap = new HashMap<T,Integer>(initialCapacity);
} }
/** /**
@ -64,12 +64,11 @@ public class IntMapper
* @return true (as per the general contract of the Collection.add * @return true (as per the general contract of the Collection.add
* method). * method).
*/ */
public boolean add(final T value)
public boolean add(final Object value)
{ {
int index = elements.size(); int index = elements.size();
elements.add(value); elements.add(value);
valueKeyMap.put(value, Integer.valueOf(index)); valueKeyMap.put(value, index);
return true; return true;
} }
@ -77,18 +76,18 @@ public class IntMapper
return elements.size(); return elements.size();
} }
public Object get(int index) { public T get(int index) {
return elements.get(index); return elements.get(index);
} }
public int getIndex(Object o) { public int getIndex(T o) {
Integer i = ((Integer)valueKeyMap.get(o)); Integer i = valueKeyMap.get(o);
if (i == null) if (i == null)
return -1; return -1;
return i.intValue(); return i.intValue();
} }
public Iterator iterator() { public Iterator<T> iterator() {
return elements.iterator(); return elements.iterator();
} }
} // end public class IntMapper } // end public class IntMapper