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 com.google.common.annotations.GwtCompatible;
     18 
     19 import java.util.concurrent.atomic.AtomicInteger;
     20 
     21 /**
     22  * Utility {@link RemovalListener} implementations intended for use in testing.
     23  *
     24  * @author mike nonemacher
     25  */
     26 @GwtCompatible(emulated = true)
     27 class TestingRemovalListeners {
     28 
     29   /**
     30    * Returns a new no-op {@code RemovalListener}.
     31    */
     32   static <K, V> NullRemovalListener<K, V> nullRemovalListener() {
     33     return new NullRemovalListener<K, V>();
     34   }
     35 
     36   /**
     37    * Type-inferring factory method for creating a {@link CountingRemovalListener}.
     38    */
     39   static <K, V> CountingRemovalListener<K, V> countingRemovalListener() {
     40     return new CountingRemovalListener<K,V>();
     41   }
     42 
     43   /**
     44    * {@link RemovalListener} that counts each {@link RemovalNotification} it receives, and provides
     45    * access to the most-recently received one.
     46    */
     47   static class CountingRemovalListener<K, V> implements RemovalListener<K, V> {
     48     private final AtomicInteger count = new AtomicInteger();
     49     private volatile RemovalNotification<K, V> lastNotification;
     50 
     51     @Override
     52     public void onRemoval(RemovalNotification<K, V> notification) {
     53       count.incrementAndGet();
     54       lastNotification = notification;
     55     }
     56 
     57     public int getCount() {
     58       return count.get();
     59     }
     60 
     61     public K getLastEvictedKey() {
     62       return lastNotification.getKey();
     63     }
     64 
     65     public V getLastEvictedValue() {
     66       return lastNotification.getValue();
     67     }
     68 
     69     public RemovalNotification<K, V> getLastNotification() {
     70       return lastNotification;
     71     }
     72   }
     73 
     74   /**
     75    * No-op {@link RemovalListener}.
     76    */
     77   static class NullRemovalListener<K, V> implements RemovalListener<K, V> {
     78     @Override
     79     public void onRemoval(RemovalNotification<K, V> notification) {}
     80   }
     81 }
     82 
     83