org.paxle.core.queue.AQueue.java Source code

Java tutorial

Introduction

Here is the source code for org.paxle.core.queue.AQueue.java

Source

/**
 * This file is part of the Paxle project.
 * Visit http://www.paxle.net for more information.
 * Copyright 2007-2010 the original author or authors.
 *
 * Licensed under the terms of the Common Public License 1.0 ("CPL 1.0").
 * Any use, reproduction or distribution of this program constitutes the recipient's acceptance of this agreement.
 * The full license text is available under http://www.opensource.org/licenses/cpl1.0.txt
 * or in the file LICENSE.txt in the root directory of the Paxle distribution.
 *
 * Unless required by applicable law or agreed to in writing, this software is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

package org.paxle.core.queue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.event.EventAdmin;

public abstract class AQueue<Data> {
    private static final long serialVersionUID = 1L;

    /**
     * for logging
     */
    protected Log logger = LogFactory.getLog(this.getClass());

    /**
     * Component to send {@link org.osgi.service.event.Event events}
     */
    protected EventAdmin eventService = null;

    /**
     * An internal {@link BlockingQueue}
     */
    protected final BlockingQueue<Data> queue;

    public AQueue(final int length) {
        this(length, false);
    }

    public AQueue(int length, final boolean limited) {
        this.queue = (limited) ? new ArrayBlockingQueue<Data>(length) : new LinkedBlockingQueue<Data>(length);
    }

    public void setEventService(EventAdmin eventService) {
        this.eventService = eventService;
    }

    /**
     * @return the number of elements in the queue
     */
    public int size() {
        return this.queue.size();
    }

    /**
     * @return the number of elements that this queue can ideally 
     * accept without blocking.
     */
    public int remainingCapacity() {
        return this.queue.remainingCapacity();
    }

    /**
     * @return an array containing all of the elements in this queue
     */
    public Object[] toArray() {
        return this.queue.toArray();
    }
}