Top Description Inners Fields Constructors Methods
org.apache.commons.compress.harmony.unpack200

public Class SegmentConstantPoolArrayCache

extends Object
Class Inheritance
Imports
java.util.ArrayList, .Collections, .HashMap, .IdentityHashMap, .List

The SegmentConstantPool spends a lot of time searching through large arrays of Strings looking for matches. This can be sped up by caching the arrays in HashMaps so the String keys are looked up and resolve to positions in the array rather than iterating through the arrays each time. Because the arrays only grow (never shrink or change) we can use the last known size as a way to determine if the array has changed. Note that this cache must be synchronized externally if it is shared.

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
SegmentConstantPoolArrayCache.CachedArray

CachedArray keeps track of the last known size of an array as well as a HashMap that knows the mapping from element values to the indices of the array which contain that value.

Field Summary

Modifier and TypeField and Description
protected IdentityHashMap<String[], SegmentConstantPoolArrayCache.CachedArray>
protected String[]
protected List<Integer>
protected String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected boolean

Returns:

boolean true if up-to-date cache, otherwise false.
arrayIsCached
(final String[]
of String
array
)

Given a String array, answer true if the array is correctly cached.

protected void
cacheArray(final String[]
String[] to cache
array
)

Cache the array passed in as the argument

public List<Integer>

Returns:

List collection of index positions in the array
indexesForArrayKey
(final String[]
String[] array to search for the value
array
,
final String
String value for which to search
key
)

Answer the indices for the given key in the given array.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

knownArraysback to summary
protected IdentityHashMap<String[], SegmentConstantPoolArrayCache.CachedArray> knownArrays
lastArrayback to summary
protected String[] lastArray
lastIndexesback to summary
protected List<Integer> lastIndexes
lastKeyback to summary
protected String lastKey

Constructor Detail

SegmentConstantPoolArrayCacheback to summary
public SegmentConstantPoolArrayCache()

Method Detail

arrayIsCachedback to summary
protected boolean arrayIsCached(final String[] array)

Given a String array, answer true if the array is correctly cached. Answer false if the array is not cached, or if the array cache is outdated.

Parameters
array:String[]

of String

Returns:boolean

boolean true if up-to-date cache, otherwise false.

cacheArrayback to summary
protected void cacheArray(final String[] array)

Cache the array passed in as the argument

Parameters
array:String[]

String[] to cache

indexesForArrayKeyback to summary
public List<Integer> indexesForArrayKey(final String[] array, final String key)

Answer the indices for the given key in the given array. If no such key exists in the cached array, answer -1.

Parameters
array:String[]

String[] array to search for the value

key:String

String value for which to search

Returns:List<Integer>

List collection of index positions in the array

org.apache.commons.compress.harmony.unpack200 back to summary

protected Class SegmentConstantPoolArrayCache.CachedArray

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.commons.compress.harmony.unpack200.SegmentConstantPoolArrayCache.CachedArray

CachedArray keeps track of the last known size of an array as well as a HashMap that knows the mapping from element values to the indices of the array which contain that value.

Field Summary

Modifier and TypeField and Description
pack-priv int
pack-priv String[]
pack-priv HashMap<String, List<Integer>>

Constructor Summary

AccessConstructor and Description
public
CachedArray(final String[] array)

Method Summary

Modifier and TypeMethod and Description
protected void
cacheIndexes()

Given a primaryArray, cache its values in a HashMap to provide a backwards mapping from element values to element indexes.

public List<Integer>

Returns:

List of indexes containing that key in the array.
indexesForKey
(final String
String element of the array
key
)

Given a particular key, answer a List of index locations in the array which contain that key.

public int

Returns:

int last known size of the cached array
lastKnownSize
()

Answer the last known size of the array cached.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

lastKnownSizeback to summary
pack-priv int lastKnownSize
primaryArrayback to summary
pack-priv String[] primaryArray
primaryTableback to summary
pack-priv HashMap<String, List<Integer>> primaryTable

Constructor Detail

CachedArrayback to summary
public CachedArray(final String[] array)

Method Detail

cacheIndexesback to summary
protected void cacheIndexes()

Given a primaryArray, cache its values in a HashMap to provide a backwards mapping from element values to element indexes. For instance, a primaryArray of: {"Zero", "Foo", "Two", "Foo"} would yield a HashMap of: "Zero" -> 0 "Foo" -> 1, 3 "Two" -> 2 which is then cached.

indexesForKeyback to summary
public List<Integer> indexesForKey(final String key)

Given a particular key, answer a List of index locations in the array which contain that key. If no elements are found, answer an empty list.

Parameters
key:String

String element of the array

Returns:List<Integer>

List of indexes containing that key in the array.

lastKnownSizeback to summary
public int lastKnownSize()

Answer the last known size of the array cached. If the last known size is not the same as the current size, the array must have changed.

Returns:int

int last known size of the cached array