1   /**
2    * Copyright 2003 - 2013 The eFaps Team
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * Revision:        $Rev$
17   * Last Changed:    $Date$
18   * Last Changed By: $Author$
19   */
20  
21  package org.efaps.util.cache;
22  
23  import java.util.UUID;
24  
25  /**
26   * Cache that is initialized automatically on the first access to it.
27   *
28   * @author The eFaps Team
29   * @version $Id$
30   * @param <T> CacheObjectInterface
31   */
32  public abstract class AbstractAutomaticCache<T extends CacheObjectInterface>
33      extends AbstractCache<T>
34  {
35      /**
36       * Returns for given key id the cached object from the cache4Id cache. If
37       * the cache is NOT initialized <code>null</code> is returned.
38       *
39       * @param _id   id of searched cached object
40       * @return cached object
41       */
42      @Override
43      public T get(final long _id)
44      {
45          if (!hasEntries()) {
46              initialize(AbstractAutomaticCache.class);
47          }
48          return getCache4Id().get(new Long(_id));
49      }
50  
51      /**
52       * Returns for given key id the cached object from the cache4Id cache. If
53       * the cache is NOT initialized, the cache will be initialized.
54       *
55       * @param _name     name of searched cached object
56       * @return cached object
57       */
58      @Override
59      public T get(final String _name)
60      {
61          if (!hasEntries()) {
62              initialize(AbstractAutomaticCache.class);
63          }
64          return getCache4Name().get(_name);
65      }
66  
67      /**
68       * Returns for given key id the cached object from the cache4Id cache. If
69       * the cache is NOT initialized, the cache will be initialized.
70       *
71       * @param _uuid     UUID of searched cached object
72       * @return cached object
73       */
74      @Override
75      public T get(final UUID _uuid)
76      {
77          if (!hasEntries()) {
78              initialize(AbstractAutomaticCache.class);
79          }
80          return getCache4UUID().get(_uuid);
81      }
82  }