/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.jena.atlas.lib.cache;

import java.util.Iterator ;
import java.util.concurrent.Callable ;
import java.util.concurrent.ExecutionException ;
import java.util.function.BiConsumer;

import org.apache.jena.atlas.lib.Cache ;
import org.apache.jena.atlas.logging.Log ;
import org.apache.jena.ext.com.google.common.cache.CacheBuilder ;
import org.apache.jena.ext.com.google.common.cache.RemovalListener ;
import org.apache.jena.ext.com.google.common.cache.RemovalNotification ;

/** Wrapper around com.google.common.cache */
final
public class CacheGuava<K,V> implements Cache<K, V>
{
    private BiConsumer<K, V> dropHandler = null ;
    /*private*/ org.apache.jena.ext.com.google.common.cache.Cache<K,V> cache ;

    public CacheGuava(int size)
    {
        RemovalListener<K,V> drop = new RemovalListener<K, V>() {
            @Override
            public void onRemoval(RemovalNotification<K, V> notification) {
                if ( dropHandler != null )
                    dropHandler.accept(notification.getKey(),
                                      notification.getValue()) ;
            }
        } ;
        cache = CacheBuilder.newBuilder()
            .maximumSize(size)
            .removalListener(drop)
            .recordStats()
            .concurrencyLevel(8)
            .build() ;
    }

    // Change the interface to be ...
    @Override
    public V getOrFill(K key, Callable<V> filler) {
        try {
            return cache.get(key, filler) ;
        }
        catch (ExecutionException e) {
            Log.warn(CacheGuava.class, "Execution exception filling cache", e) ;
            return null ;
        }
    }

    @Override
    public V getIfPresent(K key) {
        return cache.getIfPresent(key) ;
    }

    @Override
    public void put(K key, V thing) {
        if ( thing == null ) {
            cache.invalidate(key); 
            return ;
        }
        cache.put(key, thing) ;
    }

    @Override
    public boolean containsKey(K key) {
        return cache.getIfPresent(key) != null ;
    }

    @Override
    public void remove(K key) {
        cache.invalidate(key) ;
    }

    @Override
    public Iterator<K> keys() {
        return cache.asMap().keySet().iterator() ;
    }

    @Override
    public boolean isEmpty() {
        return cache.size() == 0 ;
    }

    @Override
    public void clear() {
        cache.invalidateAll() ;
    }

    @Override
    public long size() {
        return cache.size() ;
    }

    @Override
    public void setDropHandler(BiConsumer<K, V> dropHandler) {
        this.dropHandler = dropHandler ;
    }
}


@@ -995,16 +995,17 @@
 Consumer
+ 
 ;%0A%0Aimpor
@@ -1221,81 +1221,8 @@
 er ;
-%0Aimport org.apache.jena.ext.com.google.common.cache.RemovalNotification ;
 %0A%0A/*
@@ -1238,16 +1238,25 @@
  around 
+a shaded 
 com.goog
@@ -1392,19 +1392,15 @@
     
-/*
 private
-*/
  org
@@ -1538,120 +1538,9 @@
 p = 
-new RemovalListener%3CK, V%3E() %7B%0A            @Override%0A            public void onRemoval(RemovalNotification%3CK, V%3E 
+(
 noti
@@ -1548,23 +1548,21 @@
 ication)
+-%3E
  %7B%0A
-    
         
@@ -1608,20 +1608,16 @@
         
- 
-   
 dropHand
@@ -1685,19 +1685,16 @@
         
-   
 notifica
@@ -1716,29 +1716,27 @@
 ) ;%0A        
-    %7D
+%7D ;
 %0A        %7D ;
@@ -1724,35 +1724,36 @@
     %7D ;%0A        
-%7D ;
+    
 %0A        cache =
@@ -1939,46 +1939,8 @@
  %7D%0A%0A
-    // Change the interface to be ...%0A
     

Clean code.