1   /**
2    *  Copyright 2003-2007 Greg Luck
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  
17  package net.sf.ehcache.jcache;
18  
19  import net.sf.jsr107cache.CacheListener;
20  
21  import java.util.List;
22  import java.util.Collections;
23  import java.util.ArrayList;
24  import java.util.Date;
25  
26  /**
27   * @author Greg Luck
28   * @version $Id: CountingCacheListener.java 512 2007-07-10 09:18:45Z gregluck $
29   */
30  public class CountingCacheListener implements CacheListener {
31  
32      private final List cacheElementsLoaded = Collections.synchronizedList(new ArrayList());
33      private final List cacheElementsPut = Collections.synchronizedList(new ArrayList());
34      private final List cacheElementsRemoved = Collections.synchronizedList(new ArrayList());
35      private final List cacheElementsEvicted = Collections.synchronizedList(new ArrayList());
36      private final List cacheClears = Collections.synchronizedList(new ArrayList());
37  
38      /**
39       * Accessor
40       */
41      public List getCacheElementsRemoved() {
42          return cacheElementsRemoved;
43      }
44  
45      /**
46       * Accessor
47       */
48      public List getCacheElementsPut() {
49          return cacheElementsPut;
50      }
51  
52      /**
53       * Accessor
54       */
55      public List getCacheElementsEvicted() {
56          return cacheElementsEvicted;
57      }
58  
59      /**
60       * Accessor
61       */
62      public List getCacheRemoveAlls() {
63          return cacheClears;
64      }
65  
66  
67      /**
68       * Resets the counters to 0
69       */
70      public void resetCounters() {
71          synchronized (cacheElementsLoaded) {
72              cacheElementsLoaded.clear();
73          }
74          synchronized (cacheElementsRemoved) {
75              cacheElementsRemoved.clear();
76          }
77          synchronized (cacheElementsPut) {
78              cacheElementsPut.clear();
79          }
80          synchronized (cacheElementsEvicted) {
81              cacheElementsEvicted.clear();
82          }
83          synchronized (cacheClears) {
84              cacheClears.clear();
85          }
86      }
87  
88  
89      /**
90       * Triggered when a cache mapping is created due to the cache loader being consulted
91       */
92      public void onLoad(Object key) {
93          cacheElementsLoaded.add(key);
94      }
95  
96      /**
97       * Triggered when a cache mapping is created due to calling Cache.put()
98       */
99      public void onPut(Object key) {
100         cacheElementsPut.add(key);
101     }
102 
103     /**
104      * Triggered when a cache mapping is removed due to eviction
105      */
106     public void onEvict(Object key) {
107         cacheElementsEvicted.add(key);
108     }
109 
110     /**
111      * Triggered when a cache mapping is removed due to calling Cache.remove()
112      */
113     public void onRemove(Object key) {
114         cacheElementsRemoved.add(key);
115     }
116 
117     /**
118      * Triggered when an clear occurs
119      */
120     public void onClear() {
121         cacheClears.add(new Date());
122     }
123 }