⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractcache.java

📁 实现数据库的storage manager 功能
💻 JAVA
字号:
/*
 * Copyright (c) 2000-2004, Rickard C鰏ter, Martin Svensson
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer. 
 * Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution. 
 * Neither the name of SICS nor the names of its contributors 
 * may be used to endorse or promote products derived from this software 
 * without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 *
 *
 */

package com.mellowtech.disc;

import java.util.*;

/**
 * Generic model for caches. The user of a cache provides a
 * CacheCallback function that is called everytime something is
 * unloaded from cache. Also note that the callback function should
 * only be called if the value of the key has been changed. A key that
 * has been "put" into the cache more than one time 
 * (i.e. the key/value pair is update) is considerd to be changed.
 * 
 * @author Martin Svensson
 * @version 1.0
 * @see com.mellowtech.disc.CacheCallback
 */
public abstract class AbstractCache{
  
  CacheCallback callback;
  int maxSize;

  /**
   * Clears the cache without calling any callback functions.
   * Should be used with cear
   */
  public abstract void clearCache();

  
  /**
   * Tell the cache that when removing this key from cache the
   * callback's remove function should be called. Always call this
   * method if the content in the value has changed. If the key
   * has been unloaded the key/value is reinserted into the cache.
   * The value will only be reinserted into the cache if the key did
   * not exist. i.e. it is persumed that value object is the same
   * as the old one.
   * @param key the effect key
   * @param value the value that has been changed
   */
  public abstract void dirty(Object key, Object value);
  
  /**
   * Describe <code>notDirty</code> method here.
   *
   * @param key an <code>Object</code> value
   */
  public abstract void notDirty(Object key);

  /**
   * Describe <code>setCallback</code> method here.
   *
   * @param callback a <code>CacheCallback</code> value
   */
  public void setCallback(CacheCallback callback){
    this.callback = callback;
  }

  /**
   * Empty the cache. For each item in the cache the Callback function will
   * be called.
   */
  public void emptyCache(){
    Map.Entry entry;
    for(Iterator it = iterator(); it.hasNext();){
      entry = (Map.Entry) it.next();
      callback.remove(entry.getKey(), ((CacheValue)entry.getValue()).getValue());
    }
    clearCache();
  }
  
  /**
   * Get the item for a key. This can be null even if the key exist.
   *
   * @param key the key to search for
   * @return null if the key has no value or if the did not exist.
   */
  public abstract Object get(Object key);
  
  /**
   * Return an iterator over the cache. The iterator is sorted over
   * keys. The returned objects are Map.Entry objects. 
   * @return a value of type 'Iterator'
   * @see java.util.Map.Entry
   */
  public abstract Iterator iterator();


  /**
   * Put a key/value pair into the cache. If the cache is full one item
   * will first be unloaded from the cache. If the value was previously in
   * the cache its dirty bit should be set to true.
   *
   * @param key the key
   * @param value corresponding value, can be null
   */
  public abstract void put(Object key, Object value);

  /**
   * Removes a key/value pair from the cache.
   *
   * @param key the key to remove
   */
  public abstract void remove(Object key);

  /**
   * Set the new size of the cache. A negative value indicates a cache with no
   * limit. If the new size is less than the oldsize keys should be removed from
   * the cache.
   * @param size can be negative
   */
  public abstract void setSize(int size);
  
  /**
   * Return the maxCapacity of the cache. A negative value indicates
   * a cache with no limit.
   * @return an <code>int</code> value
   */
  public int maxSize(){
    return maxSize;
  }

  /**
   * Prints each key/value pair in the cache by iterating over it.
   *
   * @return a value of type 'String'
   */
  public String toString(){
    StringBuffer sb = new StringBuffer();
    Map.Entry entry;
    for(Iterator it = iterator(); it.hasNext();){
      entry = (Map.Entry) it.next();
      sb.append(entry.getKey()+" "+entry.getValue()+"\n");
    }
    return sb.toString();
  }

  class Callback implements CacheCallback{
    public void remove(Object key, Object value){
      ;
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -