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