Hi,

Hehe - those dreaded fingers are always faster than my mind ....

Next try. Hope this now makes it. Sorry for the overhead.

Regards
Felix

Jukka Zitting schrieb:

Hi,

Sorry to bug you, almost there... :-)

Felix Meschberger wrote:

 * This software is the confidential and proprietary information of
 * Day Management AG, ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Day.


BR,

Jukka Zitting

/*
 * Copyright 2004-2005 The Apache Software Foundation or its licensors,
 *                     as applicable.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.rmi.observation;

import java.util.LinkedList;

/**
 * The <code>Queue</code> class is a very simple queue assuming that there is
 * at least one consumer and potentially multiple producers. This class poses
 * no restrictions on the size of the queue. 
 * 
 * @author Felix Meschberger
 */
public class Queue {

    /** The linked list implementing the queue of data */
    private final LinkedList queue;
    
    /**
     * Creates an instance of this queue.
     */
    public Queue() {
        queue = new LinkedList();
    }

    /**
     * Appends the given <code>object</code> to the end of the queue.
     * <p>
     * After appending the element, the queue is notified such that threads
     * waiting to retrieve an element from the queue are woken up.
     */
    public void put(Object object) {
        synchronized (queue) {
            queue.addLast(object);
            queue.notifyAll();
        }
    }

    /**
     * Returns the first element from the queue. If the queue is currently empty
     * the method waits at most the given number of milliseconds.
     * 
     * @param timeout The maximum number of milliseconds to wait for an entry in
     *      the queue if the queue is empty. If zero, the method waits forever
     *      for an element.
     * 
     * @return The first element of the queue or <code>null</code> if the method
     *      timed out waiting for an entry.
     * 
     * @throws InterruptedException Is thrown if the current thread is
     *      interrupted while waiting for the queue to get at least one entry.
     */
    public Object get(long timeout) throws InterruptedException {
        synchronized (queue) {
            // wait for data if the queue is empty
            if (queue.isEmpty()) {
                queue.wait(timeout);
            }
            
            // return null if queue is (still) empty
            if (queue.isEmpty()) {
                return null;
            }
            
            // return first if queue has content now
            return queue.removeFirst();
        }
    }
}

Reply via email to