Browse Source

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
tags/REL_3_7_BETA1
Nick Burch 14 years ago
parent
commit
796f95c0b4

+ 11
- 11
src/java/org/apache/poi/hssf/record/SSTDeserializer.java View File

@@ -31,9 +31,9 @@ import org.apache.poi.util.IntMapper;
class SSTDeserializer
{

private IntMapper strings;
private IntMapper<UnicodeString> strings;

public SSTDeserializer( IntMapper strings )
public SSTDeserializer( IntMapper<UnicodeString> strings )
{
this.strings = strings;
}
@@ -46,14 +46,14 @@ class SSTDeserializer
public void manufactureStrings( int stringCount, RecordInputStream in )
{
for (int i=0;i<stringCount;i++) {
//Extract exactly the count of strings from the SST record.
UnicodeString str = new UnicodeString(in);
addToStringTable( strings, str );
}
// Extract exactly the count of strings from the SST record.
UnicodeString str = new UnicodeString(in);
addToStringTable( strings, str );
}
}

static public void addToStringTable( IntMapper strings, UnicodeString string )
{
strings.add(string );
}
}
static public void addToStringTable( IntMapper<UnicodeString> strings, UnicodeString string )
{
strings.add(string);
}
}

+ 6
- 6
src/java/org/apache/poi/hssf/record/SSTRecord.java View File

@@ -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<UnicodeString> 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<UnicodeString>();
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<UnicodeString>();
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<UnicodeString> getStrings()
{
return field_3_strings.iterator();
}

+ 10
- 10
src/java/org/apache/poi/hssf/record/SSTSerializer.java View File

@@ -32,7 +32,7 @@ final class SSTSerializer {
private final int _numStrings;
private final int _numUniqueStrings;

private final IntMapper strings;
private final IntMapper<UnicodeString> 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<UnicodeString> 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<UnicodeString> strings, int index )
{
return ( (UnicodeString) strings.get( index ) );
return ( strings.get( index ) );
}

public int[] getBucketAbsoluteOffsets()

+ 3
- 3
src/java/org/apache/poi/hssf/record/common/UnicodeString.java View File

@@ -34,10 +34,9 @@ import org.apache.poi.util.LittleEndianOutput;
* Title: Unicode String<p/>
* Description: Unicode String - just standard fields that are in several records.
* 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/>
* @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<UnicodeString> {
private short field_1_charCount;
@@ -46,6 +45,7 @@ public final class UnicodeString implements Comparable<UnicodeString> {
private List<FormatRun> 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);


+ 11
- 12
src/java/org/apache/poi/util/IntMapper.java View File

@@ -34,10 +34,10 @@ import java.util.*;
* @author Jason Height
*/

public class IntMapper
public class IntMapper<T>
{
private List elements;
private Map valueKeyMap;
private List<T> elements;
private Map<T,Integer> 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<T>(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
* 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<T> iterator() {
return elements.iterator();
}
} // end public class IntMapper

Loading…
Cancel
Save