Example usage for org.apache.commons.collections SequencedHashMap remove

List of usage examples for org.apache.commons.collections SequencedHashMap remove

Introduction

In this page you can find the example usage for org.apache.commons.collections SequencedHashMap remove.

Prototype

public Object remove(int index) 

Source Link

Document

Removes the element at the specified index.

Usage

From source file:org.openlaszlo.cache.Cache.java

/**
 * Find and optionally lock item in the cache that matches this key 
 * If the item doesn't exist, create it.  If the item 
 * does exist, move it to the top of the LRU list by removing
 * and re-adding.   /*  w  w  w  . j av a2  s. com*/
 * @return the locked item
 */
protected synchronized Item findItem(Serializable key, String enc, boolean doLockAndLeaveActive)
        throws IOException {

    //mLogger.debug("findItem");

    SequencedHashMap curMap = mMemMap;
    SequencedHashMap newMap = mMemMap;

    boolean hasMemCache = (mMaxMemSize != 0);

    if (!hasMemCache) {
        newMap = mDiskMap;
    }

    Item item = (Item) curMap.get(key);
    if (item == null) {
        curMap = mDiskMap;
        item = (Item) curMap.get(key);
        if (item == null && doLockAndLeaveActive) {
            curMap = mActiveMap;
            item = (Item) curMap.get(key);
            // TODO: [2003-08-27 bloch] add assert in java 1.4 item.active()
        }
    }

    // New items default to the mem cache if it exists.
    // Note that some items that are too big may
    // be cached on disk (and not in mem) but may 
    // temporarily be in the "mem cache" map.
    // This is confusing, but simple and safe to implement
    // w/out complicated locking.  This is because
    // we don't want to lock the maps while we're
    // fetching the items and we won't know the size
    // until we fetch the item.
    //
    // TODO: [2003-09-04 bloch] 
    // Note: this now only happens when doLockAndLeaveActive is
    // false.  At some point we could rearchitect to remove
    // this wart

    try {
        if (item == null) {
            item = new Item(key, enc, hasMemCache);
            if (doLockAndLeaveActive) {
                item.lock();
                item.setActive(true);
            }

            //                mLogger.debug(
            /* (non-Javadoc)
             * @i18n.test
             * @org-mes="Made new item for " + p[0]
             */
            //                        org.openlaszlo.i18n.LaszloMessages.getMessage(
            //                                Cache.class.getName(),"051018-270", new Object[] {key.toString()})
            //);

        } else {
            if (doLockAndLeaveActive) {
                item.lock();
                item.setActive(true);
            }
            if (item.needsReckoning()) {
                //                    mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="Reckoning an old item for " + p[0]
                 */
                //                        org.openlaszlo.i18n.LaszloMessages.getMessage(
                //                                Cache.class.getName(),"051018-285", new Object[] {key.toString()})
                //);
                item.reckon();
            }
            if (item.dirty()) {
                //                    mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="Found a dirty item for " + p[0]
                 */
                //                        org.openlaszlo.i18n.LaszloMessages.getMessage(
                //                                Cache.class.getName(),"051018-296", new Object[] {key.toString()})
                //);
                if (doLockAndLeaveActive) {
                    item.removeAndUnlock();
                } else {
                    item.remove();
                }
                curMap.remove(key);
                item = new Item(key, enc, hasMemCache);
                if (doLockAndLeaveActive) {
                    item.lock();
                    item.setActive(true);
                }
                //                    mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="Removed and made new item for " + p[0]
                 */
                //                        org.openlaszlo.i18n.LaszloMessages.getMessage(
                //                                Cache.class.getName(),"051018-315", new Object[] {key.toString()})
                //);
            } else {
                // Remove the item and re-add it below
                //                    mLogger.debug(
                /* (non-Javadoc)
                 * @i18n.test
                 * @org-mes="Found old item for " + p[0]
                 */

                //                          org.openlaszlo.i18n.LaszloMessages.getMessage(
                //                                Cache.class.getName(),"051018-325", new Object[] {key.toString()})
                //);
                curMap.remove(key);
                if (curMap == mDiskMap) {
                    if (newMap == mMemMap) {
                        // Update sizes when we're moving from disk to mem
                        long size = item.getSize();
                        if (size <= mMaxMemItemSize || mMaxMemItemSize <= 0) {
                            item.readIntoMemory();
                            // Update sizes after we read into memory in case
                            // the above read fails for some bizarro reason.
                            mDiskSize -= size;
                            mMemSize += size;
                        } else {
                            newMap = mDiskMap;
                        }
                    }
                }
            }
        }

        if (!doLockAndLeaveActive) {
            newMap.put(key, item);
        } else {
            mActiveMap.put(key, item);
        }

    } catch (IOException e) {
        // If we get any kind of exception, we better unlock the item
        // since no one will be able to unlock it.
        if (doLockAndLeaveActive && item != null) {
            item.unlock();
        }
        throw e;
    } catch (RuntimeException re) {
        // If we get any kind of exception, we better unlock the item
        // since no one will be able to unlock it.
        if (doLockAndLeaveActive && item != null) {
            item.unlock();
        }
        throw re;
    }

    return item;
}

From source file:org.openlaszlo.cache.Cache.java

/**
 * Clear entries from given map//  w  w w  .j ava  2  s . c  o  m
 * @param map to clear
 */
private boolean clearMap(SequencedHashMap map) {

    boolean ok = true;
    while (map.size() > 0) {
        Map.Entry<?, ?> first = map.getFirst();
        Object key = first.getKey();
        Item item = (Item) first.getValue();
        item.lock();
        if (!item.removeAndUnlock()) {
            ok = false;
        }
        map.remove(key);
    }

    return ok;
}