1   package com.atlassian.user.impl.cache;
2   
3   import com.atlassian.cache.CacheFactory;
4   import com.atlassian.user.Group;
5   
6   /**
7    * Caches the groups retrieved from the underlying group manager.
8    * <br/>
9    * Will also cache the fact that a group could not be found so that we don't incur
10   * the expense of another search when have previously determined that a group doesn't
11   * exist.
12   *
13   * @see CachingGroupManager
14   */
15  public class GroupCache
16  {
17      private final CacheFactory cacheFactory;
18      private final String cacheName;
19  
20      /**
21       * The NULL_GROUP is stored as the value in the groups cache when
22       * a group that does not exist is requested.
23       */
24      public static final Group NULL_GROUP = new Group()
25      {
26          private String NAME = "NULL GROUP";
27  
28          public String getName()
29          {
30              return NAME;
31          }
32  
33          public boolean equals(Object obj)
34          {
35              return obj.getClass().getName().equals(getClass().getName());
36          }
37  
38          public int hashCode()
39          {
40              return getClass().getName().hashCode();
41          }
42      };
43  
44      public GroupCache(CacheFactory cacheFactory, String cacheName)
45      {
46          this.cacheFactory = cacheFactory;
47          this.cacheName = cacheName;
48      }
49  
50      private com.atlassian.cache.Cache getCache()
51      {
52          return cacheFactory.getCache(cacheName);
53      }
54  
55      /**
56       * Stores the specified group in the cache against the group name.
57       * If group is null, stores {@link #NULL_GROUP} in the cache so the
58       * fact that the group does not exist is remembered by the
59       * cache.
60       */
61      public void put(String groupName, Group group)
62      {
63          getCache().put(groupName, group == null ? NULL_GROUP : group);
64      }
65  
66      /**
67       * @return the group with the cached name, {@link #NULL_GROUP} if the
68       * group could not be found on the previous search, or null if no
69       * group with that name is in the cache.
70       */
71      public Group get(String groupName)
72      {
73          return (Group) getCache().get(groupName);
74      }
75  
76      public void remove(String groupName)
77      {
78          getCache().remove(groupName);
79      }
80  }