All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.zhengframework.cache.simple.SimpleCache Maven / Gradle / Ivy

There is a newer version: 1.8.0
Show newest version
package com.github.zhengframework.cache.simple;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.configuration.CacheEntryListenerConfiguration;
import javax.cache.configuration.Configuration;
import javax.cache.integration.CompletionListener;
import javax.cache.processor.EntryProcessor;
import javax.cache.processor.EntryProcessorException;
import javax.cache.processor.EntryProcessorResult;

public class SimpleCache implements Cache {

  private final ConcurrentMap caches = new ConcurrentHashMap<>(16);
  private final SimpleCacheManager simpleCacheManager;
  private final String name;

  private boolean isClosed = false;

  public SimpleCache(SimpleCacheManager simpleCacheManager, String name) {
    this.simpleCacheManager = simpleCacheManager;
    this.name = name;
  }

  @Override
  public V get(K k) {
    return caches.get(k);
  }

  @Override
  public Map getAll(Set set) {
    HashMap map = new HashMap<>();
    for (K k : set) {
      map.put(k, get(k));
    }
    return map;
  }

  @Override
  public boolean containsKey(K k) {
    return caches.containsKey(k);
  }

  @Override
  public void loadAll(Set set, boolean b, CompletionListener completionListener) {

  }

  @Override
  public void put(K k, V v) {
    caches.put(k, v);
  }

  @Override
  public V getAndPut(K k, V v) {
    V v1 = caches.get(k);
    caches.replace(k, v);
    return v1;
  }

  @Override
  public void putAll(Map map) {
    for (Map.Entry entry : map.entrySet()) {
      put(entry.getKey(), entry.getValue());
    }
  }

  @Override
  public boolean putIfAbsent(K k, V v) {
    caches.putIfAbsent(k, v);
    return true;
  }

  @Override
  public boolean remove(K k) {
    caches.remove(k);
    return true;
  }

  @Override
  public boolean remove(K k, V v) {
    caches.remove(k, v);
    return true;
  }

  @Override
  public V getAndRemove(K k) {
    V v = caches.get(k);
    caches.remove(k);
    return v;
  }

  @Override
  public boolean replace(K k, V v, V v1) {
    caches.replace(k, v1);
    return true;
  }

  @Override
  public boolean replace(K k, V v) {
    caches.replace(k, v);
    return true;
  }

  @Override
  public V getAndReplace(K k, V v) {
    V v1 = caches.get(k);
    caches.replace(k, v);
    return v1;
  }

  @Override
  public void removeAll(Set set) {
    for (K k : set) {
      caches.remove(k);
    }
  }

  @Override
  public void removeAll() {
    caches.clear();
  }

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

  @Override
  public > C getConfiguration(Class aClass) {
    return null;
  }

  @Override
  public  T invoke(K k, EntryProcessor entryProcessor, Object... objects)
      throws EntryProcessorException {
    return null;
  }

  @Override
  public  Map> invokeAll(Set set,
      EntryProcessor entryProcessor, Object... objects) {
    return null;
  }

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

  @Override
  public CacheManager getCacheManager() {
    return simpleCacheManager;
  }

  @Override
  public void close() {
    synchronized (caches) {
      isClosed = true;
      caches.clear();
    }
  }

  @Override
  public boolean isClosed() {
    return isClosed;
  }

  @Override
  public  T unwrap(Class aClass) {
    return null;
  }

  @Override
  public void registerCacheEntryListener(
      CacheEntryListenerConfiguration cacheEntryListenerConfiguration) {

  }

  @Override
  public void deregisterCacheEntryListener(
      CacheEntryListenerConfiguration cacheEntryListenerConfiguration) {

  }

  @Override
  public Iterator> iterator() {
    List> list = caches.entrySet()
        .stream().map(entry -> new SimpleCacheEntry<>(entry.getKey(), entry.getValue()))
        .collect(Collectors.toList());
    return list.iterator();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy