In Java, create a generic parsed string cache (multiple resulting types)
<p>I'm trying to create a generic parsed string cache to prevent rebuilding the same object over and over. Essentially, what I'm building is something like the following:</p>

<pre><code>public class ParsedStringCache {

    // static Map&lt;Pair&lt;String, Parser&lt;T&gt;&gt;, T&gt; _cache
    //      = new HashMap&lt;Pair&lt;String, Parser&lt;T&gt;&gt;, T&gt;();

    public interface StringParser&lt;T&gt; {
        public T parseString(String stringToParse);
    }

    public static &lt;T&gt; T getParsedObject(String stringToParse, Parser&lt;T&gt; parser) {
        return parser.parseString(stringToParse);
    }
}
</code></pre>

<p>This works fine until I try to actually cache the results, such as by using something similar to the commented out hashmap in the code above, which would essentially memoize the result of getParsedObject. Is there a reasonably simple way to do this that avoids casting?</p>
