/*
   Copyright 2010 Aaron J. Radke

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package cc.drx

/**a plain constructor for cache types*/
object Cache{
  def apply[K,V](makeValue:K => V):Cache[K,V] = new Cache[K,V]{
    def valueOf(src:K):V = makeValue(src)
  }
}
/**this is essentially a mutable Map with a default generated value but hides many other methods that could get messy*/
abstract class Cache[K,V]{
  private val cache = scala.collection.concurrent.TrieMap.empty[K,V]

  //--required
  def valueOf(src:K):V

  //--implemented
  final def apply(src:K):V = cache.getOrElseUpdate(src, valueOf(src))
  final def empty = cache.empty
  final def values = cache.values
}