Package org.chaidb.db.helper

Source Code of org.chaidb.db.helper.Queue

/*
* Copyright (C) 2006  http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
*/

package org.chaidb.db.helper;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
* This class implements a simple queue
*
* @version $date$
*/
public class Queue {

    /**
     * The list containing the queue elements - Not using an
     * ArrayList since deletions on array-list are very expensive
     */
    ////////////////Modified by Ben/////////////////////////
    //protected LinkedList q = null;
    protected List q = null;
    ///////////////////////////////////////////

    /**
     * Constructor
     */
    public Queue() {
        ////////////////////////Modified by Ben/////////////////////////
        /* In order to implement synchronized access */
        //q = new LinkedList();
        q = Collections.synchronizedList(new LinkedList());
        ///////////////////////////////////////////////////////////////
    }

    /**
     * inserts an object into the queue
     *
     * @param obj The Object to be inserted into the queue
     */
    public void insert(Object obj) {
        /////////////////////////////Modified by Ben //////////////////////////
        synchronized (q) {
            //q.add(obj);
            q.add(obj);
        }
        ///////////////////////////////////////////////////////////////////////
    }

    /**
     * removes an element from the queue
     *
     * @return The Object removed from the queue
     */
    public Object remove() {
        //////////////////////////////////Modified by Ben//////////////////////////
        //if(q.size() == 0)
        //  return null;
        //return q.removeFirst();
        synchronized (q) {
            if (q.size() == 0) return null;
            return q.remove(0);

        }
        /////////////////////////////////////////////////////////////////////
    }

    /**
     * returns true if the Queue is empty
     *
     * @return A boolean value, true if the queue is empty, false otherwise
     */
    public boolean isEmpty() {
        //////////////////////////////////Modified by Ben//////////////////////////
        //return q.isEmpty();
        synchronized (q) {
            return q.isEmpty();
        }
        ////////////////////////////////////////////////////////////////////
    }

    ////////////////// Appended by ben////////////////////////////////

    /**
     * Returns the number of elements in this list
     */
    public int size() {
        return q.size();
    }

    /**
     * Returns an iterator over the elements in this list in proper sequence.
     *
     * @return An iterator over the elements in this list in proper sequence.
     */
    public Iterator iterator() {
        return q.iterator();
    }

    /**
     * Returns true if this list contains the specified element. More formally,
     * returns true if and only if this list contains at least one element e
     * such that (o==null ? e==null : o.equals(e)).
     *
     * @param o Element whose presence in this list is to be tested.
     * @return True if this list contains the specified element.
     */
    public boolean contains(Object o) {
        return q.contains(o);

    }
    //////////////////////////////////////////////////////////////////////////////
}
TOP

Related Classes of org.chaidb.db.helper.Queue

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.