Home | History | Annotate | Download | only in cache
      1 /*
      2  * Copyright (C) 2011 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
      5  * in compliance with the License. You may obtain a copy of the License at
      6  *
      7  * http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the License
     10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
     11  * or implied. See the License for the specific language governing permissions and limitations under
     12  * the License.
     13  */
     14 
     15 package com.google.common.cache;
     16 
     17 import java.util.concurrent.ConcurrentLinkedQueue;
     18 import java.util.concurrent.atomic.AtomicInteger;
     19 
     20 /**
     21  * Utility {@link RemovalListener} implementations intended for use in testing.
     22  *
     23  * @author mike nonemacher
     24  */
     25 class TestingRemovalListeners {
     26 
     27   /**
     28    * Returns a new no-op {@code RemovalListener}.
     29    */
     30   static <K, V> NullRemovalListener<K, V> nullRemovalListener() {
     31     return new NullRemovalListener<K, V>();
     32   }
     33 
     34   /**
     35    * Type-inferring factory method for creating a {@link QueuingRemovalListener}.
     36    */
     37   static <K, V> QueuingRemovalListener<K, V> queuingRemovalListener() {
     38     return new QueuingRemovalListener<K,V>();
     39   }
     40 
     41   /**
     42    * Type-inferring factory method for creating a {@link CountingRemovalListener}.
     43    */
     44   static <K, V> CountingRemovalListener<K, V> countingRemovalListener() {
     45     return new CountingRemovalListener<K,V>();
     46   }
     47 
     48   /**
     49    * {@link RemovalListener} that adds all {@link RemovalNotification} objects to a queue.
     50    */
     51   static class QueuingRemovalListener<K, V>
     52       extends ConcurrentLinkedQueue<RemovalNotification<K, V>> implements RemovalListener<K, V> {
     53 
     54     @Override
     55     public void onRemoval(RemovalNotification<K, V> notification) {
     56       add(notification);
     57     }
     58   }
     59 
     60   /**
     61    * {@link RemovalListener} that counts each {@link RemovalNotification} it receives, and provides
     62    * access to the most-recently received one.
     63    */
     64   static class CountingRemovalListener<K, V> implements RemovalListener<K, V> {
     65     private final AtomicInteger count = new AtomicInteger();
     66     private volatile RemovalNotification<K, V> lastNotification;
     67 
     68     @Override
     69     public void onRemoval(RemovalNotification<K, V> notification) {
     70       count.incrementAndGet();
     71       lastNotification = notification;
     72     }
     73 
     74     public int getCount() {
     75       return count.get();
     76     }
     77 
     78     public K getLastEvictedKey() {
     79       return lastNotification.getKey();
     80     }
     81 
     82     public V getLastEvictedValue() {
     83       return lastNotification.getValue();
     84     }
     85 
     86     public RemovalNotification<K, V> getLastNotification() {
     87       return lastNotification;
     88     }
     89   }
     90 
     91   /**
     92    * No-op {@link RemovalListener}.
     93    */
     94   static class NullRemovalListener<K, V> implements RemovalListener<K, V> {
     95     @Override
     96     public void onRemoval(RemovalNotification<K, V> notification) {}
     97   }
     98 }
     99