View Javadoc

1   /*
2    * This file is part of Domingo
3    * an Open Source Java-API to Lotus Notes/Domino
4    * hosted at http://domingo.sourceforge.net
5    *
6    * Copyright (c) 2003-2007 Beck et al. projects GmbH Munich, Germany (http://www.bea.de)
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21   */
22  
23  package de.bea.domingo.cache;
24  
25  import java.io.Serializable;
26  import java.util.Map;
27  
28  /***
29   * Simple cache implementation using a HashMap.
30   *
31   * <b>Note that this implementation is not synchronized.</b> If multiple
32   * threads access this cache concurrently, and at least one of the threads
33   * modifies the cache structurally, it <i>must</i> be synchronized externally.
34   * (A structural modification is any operation that adds or deletes one or
35   * more mappings; merely changing the value associated with a key that an
36   * instance already contains is not a structural modification.)  This is
37   * typically accomplished by synchronizing on some object that naturally
38   * encapsulates the map.  If no such object exists, the map should be
39   * "wrapped" using the <tt>Collections.synchronizedMap</tt> method.  This is
40   * best done at creation time, to prevent accidental unsynchronized access to
41   * the map: <pre> Map map = Collections.synchronizedMap(new HashMap(...));
42   * </pre><p>
43   *
44   *
45   * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
46   */
47  public abstract class AbstractBaseCache implements Cache, Serializable {
48  
49      /***
50       * the cache.
51       *
52       * <p>key -> object</p>
53       */
54      private final Map map;
55  
56      /***
57       * Constructor.
58       */
59      public AbstractBaseCache() {
60          super();
61          map = createMap();
62      }
63  
64      /***
65       * Protected getter for map attribute to allow concrete classes to access
66       * the map.
67       *
68       * @return the internal map
69       */
70      protected final Map getMap() {
71          return map;
72      }
73  
74      /***
75       * Creates the map to be used with the cache.
76       *
77       * <p>Concrete classes must implement this method and create
78       * a concrete map for the cache.</p>
79       *
80       * @return a map for the cache
81       */
82      protected abstract Map createMap();
83  
84      /***
85       * {@inheritDoc}
86       * @see de.bea.domingo.cache.Cache#size()
87       */
88      public final synchronized int size() {
89          return map.size();
90      }
91  }