Cache Map : TreeMap « Collections « Java Tutorial






/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation and Others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Hisashi MIYASHITA - initial API and implementation
 *    Kentarou FUKUDA - initial API and implementation
 *******************************************************************************/


import java.util.Iterator;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Utility class for cache map.
 */
public class CacheMap extends TreeMap<String, Object> {
  private static final long serialVersionUID = 6681131647931821052L;

  private final int maxSize;
  private final int evictSize;

  private final LinkedList<Object> accessList = new LinkedList<Object>();

  /**
   * Constructor of cache map. If the map exceed the maximum size, the
   * key/value sets will be removed from map based on specified evict size.
   * 
   * @param maxSize
   *            maximum size of the map
   * @param evictSize
   *            number of evict object
   */
  public CacheMap(int maxSize, int evictSize) {
    this.maxSize = maxSize;
    this.evictSize = evictSize;
  }

  private void evict() {
    Iterator<Object> it = accessList.iterator();
    for (int i = 0; i < evictSize; i++) {
      if (!it.hasNext())
        return;
      Object key = it.next();
      this.remove(key);
      it.remove();
    }
  }

  private int searchAccessList(Object key) {
    return accessList.indexOf(key);
  }

  private void accessEntry(Object key) {
    int idx = searchAccessList(key);
    if (idx >= 0) {
      accessList.remove(idx);
    }
    accessList.add(key);
  }

  public Object put(String key, Object val) {
    if (size() >= maxSize)
      evict();
    accessEntry(key);
    return super.put(key, val);
  }

  public Object get(Object key) {
    accessEntry(key);
    return super.get(key);
  }

  /**
   * Search a key that starts with the specified prefix from the map, and
   * return the value corresponding to the key.
   * 
   * @param prefix
   *            target prefix
   * @return the value whose key starts with prefix, or null if not available
   */
  public Object matchStartsWith(String prefix) {
    SortedMap<String, Object> smap = super.tailMap(prefix);
    Object okey;
    try {
      okey = smap.firstKey();
    } catch (NoSuchElementException e) {
      return null;
    }
    if (!(okey instanceof String))
      return null;
    String key = (String) okey;
    // System.err.println("MSW:" + key + " / " + prefix);
    if (!key.startsWith(prefix))
      return null;
    return super.get(key);
  }
}








9.29.TreeMap
9.29.1.TreeMap Class
9.29.2.Viewing Sub Maps
9.29.3.Working with End Points
9.29.4.Get Synchronized Map from TreeMap
9.29.5.Check if a particular key exists in TreeMap
9.29.6.Check if a particular value exists in TreeMap
9.29.7.Get Head Map from TreeMap
9.29.8.Get lowest and highest key stored in TreeMap
9.29.9.Get Set view of Keys from TreeMap
9.29.10.Get TreeMap Size
9.29.11.Get Sub Map from TreeMap
9.29.12.Get Tail Map from TreeMap
9.29.13.Iterate through the values of TreeMap
9.29.14.Remove all values from TreeMap
9.29.15.Remove value from TreeMap
9.29.16.TreeMap
9.29.17.Cache Map