/*
 * Copyright 2015-2016 the original author or authors.
 *
 * Licensed 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 ac.simons.spring.boot.wro4j;

import ro.isdc.wro.cache.CacheStrategy;

import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.CacheManager;

/**
 * A simple strategy based on Spring caches. Delegates the heavy lifting to the
 * cache configured by or through Spring.
 *
 * @param <K> Type of the keys
 * @param <V> Type of the values
 * @author Michael J. Simons, 2016-01-18
 */
class SpringCacheStrategy<K, V> implements CacheStrategy<K, V> {

	private final CacheManager cacheManager;

	private final String cacheName;

	SpringCacheStrategy(CacheManager cacheManager, String cacheName) {
		this.cacheManager = cacheManager;
		this.cacheName = cacheName;
	}

	@Override
	public void put(K key, V value) {
		this.cacheManager.getCache(this.cacheName).put(key, value);
	}

	@Override
	public V get(K key) {
		final ValueWrapper valueWrapper = this.cacheManager.getCache(this.cacheName).get(key);
		return (V) (valueWrapper == null ? null : valueWrapper.get());
	}

	@Override
	public void clear() {
		this.cacheManager.getCache(this.cacheName).clear();
	}

	@Override
	public void destroy() {
		this.clear();
	}

}

@@ -17,17 +17,17 @@
 2015-201
-6
+8
  the ori
@@ -653,16 +653,44 @@
 wro4j;%0A%0A
+import java.util.Optional;%0A%0A
 import r
@@ -1225,16 +1225,22 @@
 trategy(
+final 
 CacheMan
@@ -1257,16 +1257,22 @@
 Manager,
+ final
  String 
@@ -1386,14 +1386,26 @@
 put(
-K key,
+final K key, final
  V v
@@ -1503,16 +1503,22 @@
 c V get(
+final 
 K key) %7B
@@ -1524,42 +1524,31 @@
 %7B%0A%09%09
-final ValueWrapper valueWrapper = 
+return (V) Optional.of(
 this
@@ -1589,81 +1589,80 @@
 ame)
-.get(key);%0A%09%09return (V) (valueWrapper == null ? null : valueWrapper.get()
+)%0A%09%09%09%09.map(c -%3E c.get(key))%0A%09%09%09%09.map(ValueWrapper::get)%0A%09%09%09%09.orElse(null
 );%0A%09

Avoid possible NPE