001package com.box.sdk;
002
003import java.util.LinkedHashMap;
004import java.util.Map;
005
006/**
007 * Use this class to create an in-memory LRU (least recently used) access token cache to be
008 * passed to BoxDeveloperEditionAPIConnection.
009 */
010public class InMemoryLRUAccessTokenCache implements IAccessTokenCache {
011
012    private final Map<String, String> cache;
013
014    /**
015     * Creates an in-memory LRU access token cache.
016     * @param maxEntries    maximum number of entries to store.
017     */
018    public InMemoryLRUAccessTokenCache(final int maxEntries) {
019        this.cache = new LinkedHashMap<String, String>(maxEntries, 0.75F, true) {
020            private static final long serialVersionUID = -187234623489L;
021            @Override
022            protected boolean removeEldestEntry(java.util.Map.Entry<String, String> eldest) {
023                return size() > maxEntries;
024            }
025        };
026    }
027
028    /**
029     * Add an entry to the cache.
030     * @param key       key to use.
031     * @param value     access token information to store.
032     */
033    public void put(String key, String value) {
034        synchronized (this.cache) {
035            this.cache.put(key, value);
036        }
037    }
038
039    /**
040     * Get an entry from the cache.
041     * @param key       key to look for.
042     * @return          access token information.
043     */
044    public String get(String key) {
045        synchronized (this.cache) {
046            return this.cache.get(key);
047        }
048    }
049}