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     *
017     * @param maxEntries maximum number of entries to store.
018     */
019    public InMemoryLRUAccessTokenCache(final int maxEntries) {
020        this.cache = new LinkedHashMap<String, String>(maxEntries, 0.75F, true) {
021            private static final long serialVersionUID = -187234623489L;
022
023            @Override
024            protected boolean removeEldestEntry(java.util.Map.Entry<String, String> eldest) {
025                return size() > maxEntries;
026            }
027        };
028    }
029
030    /**
031     * Add an entry to the cache.
032     *
033     * @param key   key to use.
034     * @param value access token information to store.
035     */
036    public void put(String key, String value) {
037        synchronized (this.cache) {
038            this.cache.put(key, value);
039        }
040    }
041
042    /**
043     * Get an entry from the cache.
044     *
045     * @param key key to look for.
046     * @return access token information.
047     */
048    public String get(String key) {
049        synchronized (this.cache) {
050            return this.cache.get(key);
051        }
052    }
053}