|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 The original author or authors |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | + * and Apache License v2.0 which accompanies this distribution. |
| 7 | + * |
| 8 | + * The Eclipse Public License is available at |
| 9 | + * http://www.eclipse.org/legal/epl-v10.html |
| 10 | + * |
| 11 | + * The Apache License v2.0 is available at |
| 12 | + * http://www.opensource.org/licenses/apache2.0.php |
| 13 | + * |
| 14 | + * You may elect to redistribute this code under either of these licenses. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.engagingspaces.vertx.dataloader; |
| 18 | + |
| 19 | +import io.engagingspaces.vertx.dataloader.impl.DefaultCacheMap; |
| 20 | + |
| 21 | +/** |
| 22 | + * Cache map interface for data loaders that use caching. |
| 23 | + * <p> |
| 24 | + * The default implementation used by the data loader is based on a {@link java.util.LinkedHashMap}. Note that the |
| 25 | + * implementation could also have used a regular {@link java.util.Map} instead of this {@link CacheMap}, but |
| 26 | + * this aligns better to the reference data loader implementation provided by Facebook |
| 27 | + * <p> |
| 28 | + * Also it doesn't require you to implement the full set of map overloads, just the required methods. |
| 29 | + * |
| 30 | + * @param <U> type parameter indicating the type of the cache keys |
| 31 | + * @param <V> type parameter indicating the type of the data that is cached |
| 32 | + * |
| 33 | + * @author <a href="https://github.com/aschrijver/">Arnold Schrijver</a> |
| 34 | + */ |
| 35 | +public interface CacheMap<U, V> { |
| 36 | + |
| 37 | + /** |
| 38 | + * Creates a new cache map, using the default implementation that is based on a {@link java.util.LinkedHashMap}. |
| 39 | + * |
| 40 | + * @param <U> type parameter indicating the type of the cache keys |
| 41 | + * @param <V> type parameter indicating the type of the data that is cached |
| 42 | + * @return the cache map |
| 43 | + */ |
| 44 | + static <U, V> CacheMap<U, V> simpleMap() { |
| 45 | + return new DefaultCacheMap<>(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Checks whether the specified key is contained in the cach map. |
| 50 | + * |
| 51 | + * @param key the key to check |
| 52 | + * @return {@code true} if the cache contains the key, {@code false} otherwise |
| 53 | + */ |
| 54 | + boolean containsKey(U key); |
| 55 | + |
| 56 | + /** |
| 57 | + * Gets the specified key from the cache map. |
| 58 | + * <p> |
| 59 | + * May throw an exception if the key does not exists, depending on the cache map implementation that is used, |
| 60 | + * so be sure to check {@link CacheMap#containsKey(Object)} first. |
| 61 | + * |
| 62 | + * @param key the key to retrieve |
| 63 | + * @return the cached value, or {@code null} if not found (depends on cache implementation) |
| 64 | + */ |
| 65 | + V get(U key); |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates a new cache map entry with the specified key and value, or updates the value if the key already exists. |
| 69 | + * |
| 70 | + * @param key the key to cache |
| 71 | + * @param value the value to cache |
| 72 | + * @return the cache map for fluent coding |
| 73 | + */ |
| 74 | + CacheMap set(U key, V value); |
| 75 | + |
| 76 | + /** |
| 77 | + * Deletes the entry with the specified key from the cache map, if it exists. |
| 78 | + * |
| 79 | + * @param key the key to delete |
| 80 | + * @return the cache map for fluent coding |
| 81 | + */ |
| 82 | + CacheMap delete(U key); |
| 83 | + |
| 84 | + /** |
| 85 | + * Clears all entries of the cache map |
| 86 | + * |
| 87 | + * @return the cache map for fluent coding |
| 88 | + */ |
| 89 | + CacheMap clear(); |
| 90 | +} |
0 commit comments