001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.messaging.memory;
023:
024: import java.util.Comparator;
025: import java.util.Iterator;
026: import java.util.Set;
027: import java.util.TreeSet;
028:
029: import org.jboss.messaging.interfaces.Consumer;
030: import org.jboss.messaging.interfaces.MessageReference;
031: import org.jboss.messaging.interfaces.MessageSet;
032:
033: import EDU.oswego.cs.dl.util.concurrent.ReentrantLock;
034:
035: /**
036: * An in memory message set
037: *
038: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
039: * @version $Revision: 57195 $
040: */
041: public class MemoryMessageSet implements MessageSet {
042: // Constants -----------------------------------------------------
043:
044: // Attributes ----------------------------------------------------
045:
046: /** The messages */
047: private Set messages;
048:
049: /** The lock */
050: private ReentrantLock mutex = new ReentrantLock();
051:
052: // Static --------------------------------------------------------
053:
054: // Constructors --------------------------------------------------
055:
056: /**
057: * Create a new MemoryMessageSet.
058: *
059: * @param comparator the comparator for the messages
060: */
061: public MemoryMessageSet(Comparator comparator) {
062: messages = new TreeSet(comparator);
063: }
064:
065: // Public --------------------------------------------------------
066:
067: // MessageSet implementation -------------------------------------
068:
069: public void add(MessageReference reference) {
070: messages.add(reference);
071: }
072:
073: public MessageReference remove(Consumer consumer) {
074: // Do we have a message?
075: if (messages.size() > 0) {
076: for (Iterator iterator = messages.iterator(); iterator
077: .hasNext();) {
078: MessageReference message = (MessageReference) iterator
079: .next();
080: if (consumer.accepts(message, true)) {
081: iterator.remove();
082: return message;
083: }
084: }
085: }
086: return null;
087: }
088:
089: public void lock() {
090: boolean interrupted = false;
091: try {
092: mutex.acquire();
093: } catch (InterruptedException e) {
094: interrupted = true;
095: }
096: if (interrupted)
097: Thread.currentThread().interrupt();
098: }
099:
100: public void unlock() {
101: mutex.release();
102: }
103:
104: public void setConsumer(Consumer consumer) {
105: // There are no out of band notifications
106: }
107:
108: // Protected -----------------------------------------------------
109:
110: // Package Private -----------------------------------------------
111:
112: // Private -------------------------------------------------------
113:
114: // Inner Classes -------------------------------------------------
115: }
|