lrucache.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 798 行 · 第 1/2 页

JAVA
798
字号
    synchronized (this) {      if (_size1 <= _size2)	tail = _tail2 != null ? _tail2 : _tail1;      else	tail = _tail1 != null ? _tail1 : _tail2;    }    if (tail == null)      return false;    remove(tail._key);        return true;  }  /**   * Removes an item from the cache   *   * @param key the key to remove   *   * @return the value removed   */  public V remove(K key)  {    Object okey = key;    if (okey == null)      okey = NULL;        int hash = okey.hashCode() & _mask;    V value = null;    synchronized (this) {      for (CacheItem<K,V> item = _entries[hash];	   item != null;	   item = item._nextHash) {	if (item._key == okey || item._key.equals(okey)) {	  CacheItem<K,V> prevHash = item._prevHash;	  CacheItem<K,V> nextHash = item._nextHash;	  if (prevHash != null)	    prevHash._nextHash = nextHash;	  else	    _entries[hash] = nextHash;	  	  if (nextHash != null)	    nextHash._prevHash = prevHash;	  	  CacheItem<K,V> prevLru = item._prevLru;	  CacheItem<K,V> nextLru = item._nextLru;	  if (item._hitCount == 1) {	    _size1--; 	    if (prevLru != null)	      prevLru._nextLru = nextLru;	    else	      _head1 = nextLru;	    if (nextLru != null)	      nextLru._prevLru = prevLru;	    else	      _tail1 = prevLru;	  }	  else {	    _size2--; 	    if (prevLru != null)	      prevLru._nextLru = nextLru;	    else	      _head2 = nextLru;	    if (nextLru != null)	      nextLru._prevLru = prevLru;	    else	      _tail2 = prevLru;	  }	  value = item._value;	  break;	}      }      if (_isEnableListeners && value instanceof SyncCacheListener)	((SyncCacheListener) value).syncRemoveEvent();    }    if (_isEnableListeners && value instanceof CacheListener)      ((CacheListener) value).removeEvent();    return value;  }  /**   * Returns the keys stored in the cache   */  public Iterator<K> keys()  {    KeyIterator iter = new KeyIterator<K,V>(this);    iter.init(this);    return iter;  }  /**   * Returns keys stored in the cache using an old iterator   */  public Iterator<K> keys(Iterator<K> oldIter)  {    KeyIterator iter = (KeyIterator) oldIter;    iter.init(this);    return oldIter;  }  /**   * Returns the values in the cache   */  public Iterator<V> values()  {    ValueIterator iter = new ValueIterator<K,V>(this);    iter.init(this);    return iter;  }  public Iterator<V> values(Iterator<V> oldIter)  {    ValueIterator iter = (ValueIterator) oldIter;    iter.init(this);    return oldIter;  }  /**   * Returns the entries   */  public Iterator<Entry<K,V>> iterator()  {    return new EntryIterator();  }  /**   * Returns the hit count.   */  public long getHitCount()  {    return _hitCount;  }  /**   * Returns the miss count.   */  public long getMissCount()  {    return _missCount;  }  /**   * A cache item   */  static class CacheItem<K,V> {    CacheItem<K,V> _prevHash;    CacheItem<K,V> _nextHash;        CacheItem<K,V> _prevLru;    CacheItem<K,V> _nextLru;        final K _key;    V _value;    int _index;    int _hitCount;    CacheItem(K key, V value)    {      if (key == null)	throw new NullPointerException();            _key = key;      _value = value;      _hitCount = 1;    }  }  /**   * Iterator of cache keys   */  static class KeyIterator<K,V> implements Iterator<K> {    private LruCache<K,V> _cache;    private CacheItem<K,V> _item;    private boolean _isHead1;    KeyIterator(LruCache<K,V> cache)    {      init(cache);    }    void init(LruCache<K,V> cache)    {      _cache = cache;      _item = _cache._head2;      _isHead1 = false;      if (_item == null) {        _item = _cache._head1;        _isHead1 = true;      }    }    /**     * Returns the next entry in the cache.     */    public boolean hasNext()    {      return _item != null;    }    /**     * Returns the next key.     */    public K next()    {      CacheItem<K,V> entry = _item;      if (_item != null)        _item = _item._nextLru;      if (_item == null && ! _isHead1) {        _isHead1 = true;        _item = _cache._head1;      }      if (entry != null)        return entry._key;      else        return null;    }    public void remove()    {      throw new UnsupportedOperationException();    }  }  /**   * Iterator of cache values   */  static class ValueIterator<K,V> implements Iterator<V> {    private LruCache<K,V> _cache;    private CacheItem<K,V> _item;    private boolean _isHead1;    ValueIterator(LruCache<K,V> cache)    {      init(cache);    }    void init(LruCache<K,V> cache)    {      _cache = cache;      _item = _cache._head2;      _isHead1 = false;      if (_item == null) {        _item = _cache._head1;        _isHead1 = true;      }    }    /**     * Returns the next entry in the cache.     */    public boolean hasNext()    {      return _item != null;    }    /**     * Returns the next value.     */    public V next()    {      CacheItem<K,V> entry = _item;      if (_item != null)        _item = _item._nextLru;      if (_item == null && ! _isHead1) {        _isHead1 = true;        _item = _cache._head1;      }      if (entry != null)        return entry._value;      else        return null;    }    public void remove()    {      throw new UnsupportedOperationException();    }  }  /**   * Interface for entry iterator;   */  public interface Entry<K,V> {    /**     * Returns the key.     */    public K getKey();        /**     * Returns the value.     */    public V getValue();  }  /**   * Iterator of cache values   */  class EntryIterator implements Iterator<Entry<K,V>>, Entry<K,V> {    private int _i = -1;    public boolean hasNext()    {      int i = _i + 1;      CacheItem<K,V> []entries = _entries;      int length = entries.length;      for (; i < length && entries[i] == null; i++) {      }      _i = i - 1;            return i < length;    }    public Entry<K,V> next()    {      int i = _i + 1;      CacheItem<K,V> []entries = _entries;      int length = entries.length;      for (; i < length && entries[i] == null; i++) {      }      _i = i;            if (_i < length) {	return this;      }      else	return null;    }    /**     * Returns the key.     */    public K getKey()    {      if (_i < _entries.length) {	CacheItem<K,V> entry = _entries[_i];	if (entry == null)	  return null;	else if (entry._key == NULL)	  return null;	else	  return entry._key;      }      return null;    }    /**     * Returns the value.     */    public V getValue()    {      if (_i < _entries.length) {	CacheItem<K,V> entry = _entries[_i];		return entry != null ? entry._value : null;      }      return null;    }    public void remove()    {      if (_i < _entries.length) {	CacheItem<K,V> entry = _entries[_i];	if (entry != null)	  LruCache.this.remove(entry._key);      }    }  }}

⌨️ 快捷键说明

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