From bc44c4e58e71be883ed5f9bbd19c29e8c93aed87 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 25 Nov 2008 00:57:22 +0000 Subject: [PATCH] A cache adapter for testing. --- .../depot/util/TestCacheAdapter.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/java/com/samskivert/depot/util/TestCacheAdapter.java diff --git a/src/java/com/samskivert/depot/util/TestCacheAdapter.java b/src/java/com/samskivert/depot/util/TestCacheAdapter.java new file mode 100644 index 0000000..47466e8 --- /dev/null +++ b/src/java/com/samskivert/depot/util/TestCacheAdapter.java @@ -0,0 +1,88 @@ +// +// $Id$ +// +// Depot library - a Java relational persistence library +// Copyright (C) 2006-2008 Michael Bayne and Pär Winzell +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.depot.util; + +import java.io.Serializable; +import java.util.Collections; +import java.util.Map; + +import com.google.common.collect.Maps; + +import com.samskivert.depot.CacheAdapter; + +/** + * A simple cache adapter that stores all cached values in an in-memory map and never flushes. + * Don't use this for anything other than testing or you'll regret it. + */ +public class TestCacheAdapter implements CacheAdapter +{ + // from interface CacheAdapter + public synchronized CacheAdapter.CacheBin getCache (String id) + { + @SuppressWarnings("unchecked") CacheBin bin = (CacheBin)_bins.get(id); + if (bin == null) { + bin = new TestCacheBin(); + _bins.put(id, bin); + } + return bin; + } + + // from interface CacheAdapter + public void shutdown () + { + // nothing doing! + } + + protected static class TestCachedValue implements CacheAdapter.CachedValue + { + public TestCachedValue (T value) { + _value = value; + } + public T getValue () { + return _value; + } + protected final T _value; + } + + protected static class TestCacheBin implements CacheAdapter.CacheBin + { + public CacheAdapter.CachedValue lookup (Serializable key) { + // System.err.println("GET " + key + ": " + _cache.containsKey(key)); + return _cache.get(key); + } + public void store (Serializable key, T value) { + // System.err.println("STORE " + key); + _cache.put(key, new TestCachedValue(value)); + } + public void remove (Serializable key) { + // System.err.println("REMOVE " + key); + _cache.remove(key); + } + public Iterable enumerateKeys () { + return _cache.keySet(); + } + protected Map> _cache = + Collections.synchronizedMap( + Maps.>newHashMap()); + } + + protected Map> _bins = Maps.newHashMap(); +}