2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.dao;
10 import com.google.common.base.Optional;
11 import com.google.common.collect.Iterables;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.SimpleCachedDao;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
20 * Purpose: generic implementation of {@link SimpleCachedDao}
22 public class SimpleCachedDaoImpl<K, V extends DataObject> implements SimpleCachedDao<K, V> {
24 private final ConcurrentMap<K, V> cache;
26 public SimpleCachedDaoImpl() {
27 cache = new ConcurrentHashMap<>();
31 public V update(@Nonnull final K key, @Nullable final V value) {
32 final V previousValue;
34 previousValue = cache.put(key, value);
36 previousValue = cache.remove(key);
43 public Optional<V> find(@Nonnull final K key) {
44 return Optional.fromNullable(cache.get(key));
48 public void invalidateCache() {
53 public boolean isEmpty() {
54 return cache.isEmpty();
58 public Iterable<V> values() {
59 return Iterables.unmodifiableIterable(cache.values());