// Copyright (c) 2003-present, Jodd Team (http://jodd.org)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

package jodd.cache;

import jodd.core.JoddCore;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Type cache.
 */
public class TypeCache<K, T> {

	/**
	 * Creates default type cache implementation, as defined in {@link jodd.core.JoddCoreDefaults}.
	 */
	public static <A, B> TypeCache<A, B> create() {
		return new TypeCache<>(JoddCore.get().typeCacheImplementation());
	}

	public static <A, B> TypeCache<A, B> create(final Implementation implementation) {
		return new TypeCache<>(implementation);
	}

	public enum Implementation {
		/**
		 * Just a simple map.
		 */
		MAP(false, false),
		/**
		 * Synchronized map.
		 */
		SYNC_MAP(false, true),
		/**
		 * Weak map.
		 */
		WEAK(true, false),
		/**
		 * Synchronized and weak map.
		 */
		SYNC_WEAK(true, true);

		private final boolean sync;
		private final boolean weak;

		Implementation(final boolean weak, final boolean sync) {
			this.weak = weak;
			this.sync = sync;
		}

		/**
		 * Creates a map based on type.
		 */
		public <A, B> Map<Class<A>, B> createMap() {
			if (weak) {
				if (sync) {
					return Collections.synchronizedMap(new WeakHashMap<>());
				} else {
					return new WeakHashMap<>();
				}
			} else {
				if (sync) {
					return new ConcurrentHashMap<>();
				} else {
					return new HashMap<>();
				}
			}
		}
	}

	private final Map<Class<K>, T> map;

	public TypeCache(final Implementation typeCacheImplementation) {
		this.map = typeCacheImplementation.createMap();
	}

	/**
	 * Returns value from the map or {@code null} if value does not exist.
	 */
	public T get(final Class<K> key) {
		return map.get(key);
	}

	/**
	 * Returns existing value of default one if key is not registered.
	 */
	public T getOrDefault(final Class<K> key, final T defaultValue) {
		return map.getOrDefault(key, defaultValue);
	}

	/**
	 * Cache some value for given class.
	 */
	public T put(final Class<K> key, final T value) {
		return map.put(key, value);
	}

	/**
	 * Removes element from type cache.
	 */
	public T remove(final Class<K> type) {
		return map.remove(type);
	}

	/**
	 * Clears complete cache.
	 */
	public void clear() {
		map.clear();
	}

	/**
	 * Returns cache size.
	 */
	public int size() {
		return map.size();
	}

	/**
	 * Returns {@code true} if cache is empty.
	 */
	public boolean isEmpty() {
		return map.isEmpty();
	}
}

@@ -1631,16 +1631,13 @@
 che%3C
-K, 
 T%3E %7B%0A
+
 %0A%09/*
@@ -1749,35 +1749,32 @@
 public static %3CA
-, B
 %3E TypeCache%3CA, B
@@ -1770,27 +1770,24 @@
 eCache%3CA
-, B
 %3E create
 () %7B%0A%09%09r
@@ -1778,16 +1778,23 @@
 %3E create
+Default
 () %7B%0A%09%09r
@@ -1879,19 +1879,16 @@
 tatic %3CA
-, B
 %3E TypeCa
@@ -1892,19 +1892,16 @@
 eCache%3CA
-, B
 %3E create
@@ -2475,19 +2475,16 @@
 ublic %3CA
-, B
 %3E Map%3CCl
@@ -2491,13 +2491,13 @@
 ass%3C
-A%3E, B
+?%3E, A
 %3E cr
@@ -2806,17 +2806,17 @@
 p%3CClass%3C
-K
+?
 %3E, T%3E ma
@@ -3043,25 +3043,25 @@
 final Class%3C
-K
+?
 %3E key) %7B%0A%09%09r
@@ -3189,33 +3189,33 @@
 ult(final Class%3C
-K
+?
 %3E key, final T d
@@ -3353,17 +3353,17 @@
 l Class%3C
-K
+?
 %3E key, f
@@ -3488,17 +3488,17 @@
 l Class%3C
-K
+?
 %3E type) 

Use only single generic parameter as Java is weird.