/**
 * Copyright © 2012-2023 by Data Virtuality GmbH
 */
package org.teiid.cache;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Dmitry Pogorelov
 * @date 2023.06.14
 */
public class MapCache<K, V> implements Cache<K, V> {
	private Map<K, V> cache = new HashMap<>();
	private String cacheName;

	public MapCache(String cacheName) {
		this.cacheName = cacheName;
	}

	public MapCache(String cacheName, Map<K, V> cache) {
		this(cacheName);
		this.cache = cache;
	}

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

	@Override
	public Map<K, V> getAll(Set<K> keys) {
		return cache.entrySet().stream().filter(e -> keys.contains(e.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
	}

	@Override
	public V put(K key, V value, Long ttl) {
		return cache.put(key, value);
	}

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

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

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

	@Override
	public String getName() {
		return cacheName;
	}

	@Override
	public Set<K> keySet() {
		return cache.keySet();
	}

	@Override
	public boolean isTransactional() {
		return false;
	}

	@Override
	public boolean supportsLocks() {
		return false;
	}

}
