001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.repository.queue;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import org.netbeans.modules.cnd.repository.spi.Key;
047: import org.netbeans.modules.cnd.repository.spi.Persistent;
048: import org.netbeans.modules.cnd.repository.testbench.Stats;
049:
050: /**
051: * A queue for writing down cache
052: * @author Vladimir Kvashin
053: */
054: public class RepositoryQueue extends KeyValueQueue<Key, Persistent> {
055:
056: /** Overrides parent to allow timing by flag */
057: @Override
058: protected boolean needsTiming() {
059: return Stats.queueTiming;
060: }
061:
062: /** Overrides parent to allow timing by flag */
063: @Override
064: protected boolean needsTrace() {
065: return Stats.queueTrace;
066: }
067:
068: /** Returns this queue name; used for tracing/debugging purposes */
069: @Override
070: protected String getTraceName() {
071: return "RepositoryQueue" + '@' + hashCode(); // NOI18N
072: }
073:
074: @Override
075: protected void doReplaceAddLast(Key key, Persistent value,
076: Entry existent) {
077: super .doReplaceAddLast(key, value, existent);
078: queue.remove(existent);
079: queue.addLast(existent);
080: }
081:
082: public void onIdle() {
083: // do nothing
084: }
085:
086: /**
087: * Removes all queue entries that are accepted by the given filter.
088: * @param filter a filter that deceides whether the entry should be removed
089: * (if its accept returns true, entry should be removed)
090: * @return a set of objects that are removed from queue
091: */
092: public Collection<RepositoryQueue.Entry> clearQueue(Filter filter) {
093: synchronized (lock) {
094: Collection<RepositoryQueue.Entry> removed = new ArrayList<Entry>();
095: // collecting entried to remove
096: for (Entry entry : map.values()) {
097: if (filter.accept(entry.getKey(), entry.getValue())) {
098: removed.add(entry);
099: }
100: }
101: // remove entries
102: for (Entry entry : removed) {
103: remove(entry.getKey());
104: }
105: return removed;
106: }
107: }
108:
109: /**
110: * A filter for queue elements.
111: * An instances of this interface may be passed to a method
112: * that manipulates entries (e.g. removes them, as the clearQueue method does).
113: * In the case accept() returns true, the given action (i.e. removal) is performed,
114: * otherwise it isn't done
115: */
116: public interface Filter {
117: /**
118: * Is called for each entry in the queue.
119: *
120: * @param key the given entry key
121: * @param entry the given entry value
122: *
123: * @return true if the action (e.g. deletion) should be attempt,
124: * otherwise false
125: */
126: boolean accept(Key key, Persistent value);
127: }
128:
129: }
|