JDOM
0.0.5-BETA

org.jdom2.util
Class StringBin

java.lang.Object
  extended by org.jdom2.util.StringBin

public final class StringBin
extends java.lang.Object

This is a mechanism for storing and reusing unique instances of Strings. The idea is that in XML the tag names, attribute names, and other String content is often repeated a lot. Each repeat is typically done as a new String instance. This class makes it possible to substantially reduce memory usage by reusing String instances instead of keeping the new ones.

This class is not the same as String.intern() because String.intern() uses the PermGen memory space (very limited in size), whereas this uses the heap.

The primary goal of this class is to be as memory efficient as possible. This has the interesting side effect of reducing the amount of time spent in garbage-collection cycles. While this does increase the amount of time to process a String, it means that subsequent String values can be 'recycled' fast, and, ideally, never need to leave the 'eden space' in the memory model which in turn means that the duplicate strings do not even hit the GC overhead. It is easy to measure that this process takes longer than simply keeping the duplicate String values, but it is much harder to measure the decreased cost of GC. It would be somewhat fair to say that the memory benefit is substantial, and the cost of allocation is offset by the savings in garbage collection. This trade-off is dependent on the amount of duplicate data you have. In XML where there are lots of repeating patterns of element and attribute names this can add up pretty fast.

This class is not thread-safe.

Author:
Rolf Lear

Constructor Summary
StringBin()
          Create a default instance of the StringBin with the default capacity.
StringBin(int capacity)
          Create a StringBin instance with a specified initial capacity.
 
Method Summary
 java.lang.String reuse(java.lang.String value)
          Get a String instance that is equal to the input value.
 int size()
          Number of registered Strings
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringBin

public StringBin()
Create a default instance of the StringBin with the default capacity.


StringBin

public StringBin(int capacity)
Create a StringBin instance with a specified initial capacity.

Parameters:
capacity - the capacity to set.
Method Detail

reuse

public java.lang.String reuse(java.lang.String value)
Get a String instance that is equal to the input value. This may or may not be the same instance as the input value. Null input values will reuse() as null.

Parameters:
value - The value to check.
Returns:
a String that is equals() to the input value, or null if the input was null

size

public int size()
Number of registered Strings

Returns:
the number of registered String values.

JDOM
0.0.5-BETA

Copyright � 2012 Jason Hunter, Brett McLaughlin. All Rights Reserved.