001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.replication.server;
028    
029    import java.util.SortedMap;
030    import java.util.TreeMap;
031    
032    import org.opends.server.replication.common.ChangeNumber;
033    import org.opends.server.replication.protocol.UpdateMessage;
034    
035    /**
036     * This class is used to build ordered lists of UpdateMessage.
037     * The order is defined by the order of the ChangeNumber of the UpdateMessage.
038     */
039    
040    public class MsgQueue
041    {
042      private SortedMap<ChangeNumber, UpdateMessage>  map =
043        new TreeMap<ChangeNumber, UpdateMessage>();
044    
045      /**
046       * Return the first UpdateMessage in the MsgQueue.
047       *
048       * @return The first UpdateMessage in the MsgQueue.
049       */
050      public UpdateMessage first()
051      {
052        return map.get(map.firstKey());
053      }
054    
055      /**
056       * Return the last UpdateMessage in the MsgQueue.
057       *
058       * @return The last UpdateMessage in the MsgQueue.
059       */
060      public UpdateMessage last()
061      {
062        return map.get(map.lastKey());
063      }
064    
065      /**
066       * Returns the number of elements in this MsgQueue.
067       *
068       * @return The number of elements in this MsgQueue.
069       */
070      public int size()
071      {
072        return map.size();
073      }
074    
075      /**
076       * Returns <tt>true</tt> if this MsgQueue contains no UpdateMessage.
077       *
078       * @return <tt>true</tt> if this MsgQueue contains no UpdateMessage.
079       */
080      public boolean isEmpty()
081      {
082        return map.isEmpty();
083      }
084    
085    
086      /**
087       * Add an UpdateMessage to this MessageQueue.
088       *
089       * @param update The UpdateMessage to add to this MessageQueue.
090       */
091      public void add(UpdateMessage update)
092      {
093        map.put(update.getChangeNumber(), update);
094      }
095    
096      /**
097       * Get and remove the first UpdateMessage in this MessageQueue.
098       *
099       * @return The first UpdateMessage in this MessageQueue.
100       */
101      public UpdateMessage removeFirst()
102      {
103        UpdateMessage msg = map.get(map.firstKey());
104        map.remove(msg.getChangeNumber());
105        return msg;
106      }
107    
108      /**
109       * Returns <tt>true</tt> if this map contains an UpdateMessage
110       * with the same ChangeNumber as the given UpdateMessage.
111       *
112       * @param msg UpdateMessage whose presence in this queue is to be tested.
113       *
114       * @return <tt>true</tt> if this map contains an UpdateMessage
115       *         with the same ChangeNumber as the given UpdateMessage.
116       *
117       */
118      public boolean contains(UpdateMessage msg)
119      {
120        return map.containsKey(msg.getChangeNumber());
121      }
122    
123      /**
124       * Removes all UpdateMessage form this queue.
125       */
126      public void clear()
127      {
128        map.clear();
129      }
130    }