com.h3c.util

## Class StringPool

- java.lang.Object
- - com.h3c.util.StringPool

-     public final class StringPool
      extends Object

  This class canonicalizes strings. Using a string pool can be helpful in reducing the amount of memory used when there are many references to the same few string values.

  A string pool instance is initialized with a "limit" parameter at construction. For a string pool `strPool` with a limit of `N`, and two strings `S1` and `S2`; `strPool.get(S1)` and `strPool.get(S2)` are guaranteed to return a reference to the same string instance iff `S1.equals(S2) &amp;&amp; S1.length() &lt;= N`.

  Using `StringPool` is preferrable to using `String.intern()`, which stores strings in its internal cache permanently. `StringPool` implements a mechanism to "age-out" entries over time.

  (NOTE: the age-out mechanism hasn't been implemented yet, but it will be soon!)

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `StringPool()` Constructs a string pool with the default string length limit of 64.
    `StringPool(int limit)` Constructs a string pool with the specified string length limit.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `String` | `get(String s)` Returns the canonicalized string from the pool, as long as the length of the string is less than the configured limit for this string pool.
    `String` | `toString()`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### StringPool

          public StringPool()

      Constructs a string pool with the default string length limit of 64.

    - #### StringPool

          public StringPool(int limit)

      Constructs a string pool with the specified string length limit. Only strings of length `limit` or less will be canonicalized.
      Parameters:
        `limit` - the string length limit

  - ### Method Detail

    - #### get

          public String get(String s)

      Returns the canonicalized string from the pool, as long as the length of the string is less than the configured limit for this string pool. The string is simply returned as is, if it is longer than the limit.
      Parameters:
        `s` - the string
      Returns:
        the canonicalized string

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

Copyright © 2014. All Rights Reserved.