001// Copyright 2011 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.plastic;
016
017import java.util.HashMap;
018import java.util.Map;
019
020/**
021 * Quick and dirty key/value cache that is subclassed to provide the logic that generates the value for
022 * a missing key.
023 * 
024 * @param <S> key type
025 * @param <T> value type
026 */
027public abstract class Cache<S, T>
028{
029    private Map<S, T> innerCache = new HashMap<S, T>();
030
031    public T get(S input)
032    {
033        T result = innerCache.get(input);
034
035        if (result == null)
036        {
037            result = convert(input);
038            innerCache.put(input, result);
039        }
040
041        return result;
042    }
043
044    protected abstract T convert(S input);
045}