/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.xpn.xwiki.plugin.lucene;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.buffer.UnboundedFifoBuffer;

/**
 * This class represents a Queue (FirstInFirstOut) for XWikiDocument objects. It is used during
 * indexing of the wiki. The rebuilding of the index is done, until the processing queue is empty.
 * 
 * @version $Id: $
 */
public class XWikiDocumentQueue
{
    /**
     * maps names of documents to the document instances itself
     */
    private Map<String, IndexData> documentsByName = new HashMap<String, IndexData>();

    /**
     * maintains fifo order
     */
    private Buffer namesQueue = new UnboundedFifoBuffer();

    /**
     * @return remove an item from our queue and return it.
     */
    public synchronized IndexData remove()
    {
        return documentsByName.remove(namesQueue.remove());
    }

    /**
     * @param data IndexData object to add to our queue
     */
    public synchronized void add(IndexData data)
    {
        final String key = data.getId();
        if (!documentsByName.containsKey(key)) {
            // document with this name not yet in Queue, so add it
            namesQueue.add(key);
        }
        // in any case put new version of this document in the map, overwriting
        // possibly existing older version
        documentsByName.put(key, data);
    }

    /**
     * @return true if our queue is empty.
     */
    public synchronized boolean isEmpty()
    {
        return namesQueue.isEmpty();
    }

    /**
     * @return number of elements in our queue.
     */
    public long getSize()
    {
        return namesQueue.size();
    }
}

@@ -1020,16 +1020,80 @@
 Buffer;%0A
+import org.apache.commons.collections.BufferUnderflowException;%0A
 import o
@@ -1284,46 +1284,33 @@
 The 
-rebuilding of the index is done, until
+index is updated whenever
  the
@@ -1330,22 +1330,142 @@
 ueue is 
+not 
 empty.
+ This class%0A * is threadsafe, as usually several threads add elements and one thread removes them for%0A * processing.
 %0A * %0A * 
@@ -1533,17 +1533,17 @@
 %0A     * 
-m
+M
 aps name
@@ -1586,15 +1586,9 @@
 nces
- itself
+.
 %0A   
@@ -1695,17 +1695,17 @@
 %0A     * 
-m
+M
 aintains
@@ -1709,18 +1709,19 @@
 ins 
-fifo
+FIFO
  order
+.
 %0A   
@@ -1804,17 +1804,9 @@
   * 
-@return r
+R
 emov
@@ -1820,19 +1820,19 @@
 em from 
-our
+the
  queue a
@@ -1844,16 +1844,221 @@
 turn it.
+ Since this is a FIFO, the element returned will%0A     * be the oldes one in the queue.%0A     * %0A     * @return The oldest element in the queue.%0A     * @throws BufferUnderflowException If the queue is empty.
 %0A     */
@@ -2100,16 +2100,48 @@
 remove()
+ throws BufferUnderflowException
 %0A    %7B%0A 
@@ -2206,32 +2206,419 @@
 %0A    %7D%0A%0A    /**%0A
+     * Adds an item to the queue. Since this is a FIFO, it will be removed after all the other items%0A     * already in the queue have been processed. If the element was already in the queue, it will%0A     * not be added again (as a duplicate), its position will remain unchanged, but the associated%0A     * data is updated, so the new version will replace the old unprocessed one.%0A     * %0A
      * @param da
@@ -2647,25 +2647,26 @@
  add to 
-our
+the
  queue
+.
 %0A     */
@@ -2658,32 +2658,67 @@
  queue.%0A     */%0A
+    @SuppressWarnings(%22unchecked%22)%0A
     public synch
@@ -2861,17 +2861,17 @@
      // 
-d
+D
 ocument 
@@ -2896,16 +2896,20 @@
  yet in 
+the 
 Queue, s
@@ -2971,17 +2971,17 @@
      // 
-i
+I
 n any ca
@@ -3149,27 +3149,91 @@
   * 
-@return true
+Check if the queue is empty or not.%0A     * %0A     * @return %3Ccode%3Etrue%3C/code%3E
  if 
-our
+the
  que
@@ -3235,32 +3235,62 @@
 e queue is empty
+, %3Ccode%3Efalse%3C/code%3E otherwise
 .%0A     */%0A    pu
@@ -3390,23 +3390,27 @@
 %0A     * 
-@r
+R
 eturn
+s the
  number 
@@ -3424,19 +3424,75 @@
 ents in 
-our
+the queue.%0A     * %0A     * @return Number of elements in the
  queue.%0A
@@ -3510,20 +3510,32 @@
  public 
-long
+synchronized int
  getSize

Improve javadoc