View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.jcs3.jcache;
20  
21  import java.io.Closeable;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import javax.cache.configuration.CacheEntryListenerConfiguration;
26  import javax.cache.configuration.Factory;
27  import javax.cache.event.CacheEntryCreatedListener;
28  import javax.cache.event.CacheEntryEvent;
29  import javax.cache.event.CacheEntryEventFilter;
30  import javax.cache.event.CacheEntryExpiredListener;
31  import javax.cache.event.CacheEntryListener;
32  import javax.cache.event.CacheEntryListenerException;
33  import javax.cache.event.CacheEntryRemovedListener;
34  import javax.cache.event.CacheEntryUpdatedListener;
35  
36  public class JCSListener<K, V> implements Closeable
37  {
38  //    private final boolean oldValue;
39  //    private final boolean synchronous;
40      private final CacheEntryEventFilter<? super K, ? super V> filter;
41      private final CacheEntryListener<? super K, ? super V> delegate;
42      private final boolean remove;
43      private final boolean expire;
44      private final boolean update;
45      private final boolean create;
46  
47      public JCSListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration)
48      {
49  //        oldValue = cacheEntryListenerConfiguration.isOldValueRequired();
50  //        synchronous = cacheEntryListenerConfiguration.isSynchronous();
51  
52          final Factory<CacheEntryEventFilter<? super K, ? super V>> filterFactory = cacheEntryListenerConfiguration
53                  .getCacheEntryEventFilterFactory();
54          if (filterFactory == null)
55          {
56              filter = NoFilter.INSTANCE;
57          }
58          else
59          {
60              filter = filterFactory.create();
61          }
62  
63          delegate = cacheEntryListenerConfiguration.getCacheEntryListenerFactory().create();
64          remove = CacheEntryRemovedListener.class.isInstance(delegate);
65          expire = CacheEntryExpiredListener.class.isInstance(delegate);
66          update = CacheEntryUpdatedListener.class.isInstance(delegate);
67          create = CacheEntryCreatedListener.class.isInstance(delegate);
68      }
69  
70      public void onRemoved(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException
71      {
72          if (remove)
73          {
74              CacheEntryRemovedListener.class.cast(delegate).onRemoved(filter(events));
75          }
76      }
77  
78      public void onExpired(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException
79      {
80          if (expire)
81          {
82              CacheEntryExpiredListener.class.cast(delegate).onExpired(filter(events));
83          }
84      }
85  
86      public void onUpdated(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException
87      {
88          if (update)
89          {
90              CacheEntryUpdatedListener.class.cast(delegate).onUpdated(filter(events));
91          }
92      }
93  
94      public void onCreated(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException
95      {
96          if (create)
97          {
98              CacheEntryCreatedListener.class.cast(delegate).onCreated(filter(events));
99          }
100     }
101 
102     private Iterable<CacheEntryEvent<? extends K, ? extends V>> filter(final List<CacheEntryEvent<? extends K, ? extends V>> events)
103     {
104         if (filter == NoFilter.INSTANCE)
105         {
106             return events;
107         }
108 
109         final List<CacheEntryEvent<? extends K, ? extends V>> filtered = new ArrayList<>(
110                 events.size());
111         for (final CacheEntryEvent<? extends K, ? extends V> event : events)
112         {
113             if (filter.evaluate(event))
114             {
115                 filtered.add(event);
116             }
117         }
118         return filtered;
119     }
120 
121     @Override
122     public void close()
123     {
124         if (Closeable.class.isInstance(delegate)) {
125             Closeable.class.cast(delegate);
126         }
127     }
128 }