JCSListener.java

  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. import java.io.Closeable;
  21. import java.util.ArrayList;
  22. import java.util.List;

  23. import javax.cache.configuration.CacheEntryListenerConfiguration;
  24. import javax.cache.configuration.Factory;
  25. import javax.cache.event.CacheEntryCreatedListener;
  26. import javax.cache.event.CacheEntryEvent;
  27. import javax.cache.event.CacheEntryEventFilter;
  28. import javax.cache.event.CacheEntryExpiredListener;
  29. import javax.cache.event.CacheEntryListener;
  30. import javax.cache.event.CacheEntryListenerException;
  31. import javax.cache.event.CacheEntryRemovedListener;
  32. import javax.cache.event.CacheEntryUpdatedListener;

  33. public class JCSListener<K, V> implements Closeable
  34. {
  35. //    private final boolean oldValue;
  36. //    private final boolean synchronous;
  37.     private final CacheEntryEventFilter<? super K, ? super V> filter;
  38.     private final CacheEntryListener<? super K, ? super V> delegate;
  39.     private final boolean remove;
  40.     private final boolean expire;
  41.     private final boolean update;
  42.     private final boolean create;

  43.     public JCSListener(final CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration)
  44.     {
  45. //        oldValue = cacheEntryListenerConfiguration.isOldValueRequired();
  46. //        synchronous = cacheEntryListenerConfiguration.isSynchronous();

  47.         final Factory<CacheEntryEventFilter<? super K, ? super V>> filterFactory = cacheEntryListenerConfiguration
  48.                 .getCacheEntryEventFilterFactory();
  49.         if (filterFactory == null)
  50.         {
  51.             filter = NoFilter.INSTANCE;
  52.         }
  53.         else
  54.         {
  55.             filter = filterFactory.create();
  56.         }

  57.         delegate = cacheEntryListenerConfiguration.getCacheEntryListenerFactory().create();
  58.         remove = CacheEntryRemovedListener.class.isInstance(delegate);
  59.         expire = CacheEntryExpiredListener.class.isInstance(delegate);
  60.         update = CacheEntryUpdatedListener.class.isInstance(delegate);
  61.         create = CacheEntryCreatedListener.class.isInstance(delegate);
  62.     }

  63.     public void onRemoved(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException
  64.     {
  65.         if (remove)
  66.         {
  67.             CacheEntryRemovedListener.class.cast(delegate).onRemoved(filter(events));
  68.         }
  69.     }

  70.     public void onExpired(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException
  71.     {
  72.         if (expire)
  73.         {
  74.             CacheEntryExpiredListener.class.cast(delegate).onExpired(filter(events));
  75.         }
  76.     }

  77.     public void onUpdated(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException
  78.     {
  79.         if (update)
  80.         {
  81.             CacheEntryUpdatedListener.class.cast(delegate).onUpdated(filter(events));
  82.         }
  83.     }

  84.     public void onCreated(final List<CacheEntryEvent<? extends K, ? extends V>> events) throws CacheEntryListenerException
  85.     {
  86.         if (create)
  87.         {
  88.             CacheEntryCreatedListener.class.cast(delegate).onCreated(filter(events));
  89.         }
  90.     }

  91.     private Iterable<CacheEntryEvent<? extends K, ? extends V>> filter(final List<CacheEntryEvent<? extends K, ? extends V>> events)
  92.     {
  93.         if (filter == NoFilter.INSTANCE)
  94.         {
  95.             return events;
  96.         }

  97.         final List<CacheEntryEvent<? extends K, ? extends V>> filtered = new ArrayList<>(
  98.                 events.size());
  99.         for (final CacheEntryEvent<? extends K, ? extends V> event : events)
  100.         {
  101.             if (filter.evaluate(event))
  102.             {
  103.                 filtered.add(event);
  104.             }
  105.         }
  106.         return filtered;
  107.     }

  108.     @Override
  109.     public void close()
  110.     {
  111.         if (Closeable.class.isInstance(delegate)) {
  112.             Closeable.class.cast(delegate);
  113.         }
  114.     }
  115. }