craterdog.collections.Queue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-collection-framework Show documentation
Show all versions of java-collection-framework Show documentation
A set of interfaces and classes that define a Java collection framework.
/************************************************************************
* Copyright (c) Crater Dog Technologies(TM). All Rights Reserved. *
************************************************************************
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. *
* *
* This code is free software; you can redistribute it and/or modify it *
* under the terms of The MIT License (MIT), as published by the Open *
* Source Initiative. (See http://opensource.org/licenses/MIT) *
************************************************************************/
package craterdog.collections;
import com.fasterxml.jackson.annotation.JsonValue;
import craterdog.collections.abstractions.ClosedCollection;
import craterdog.collections.interfaces.FIFO;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
/**
* This collection class implements a queue (FIFO) data structure. Normally, multiple
* threads will be accessing a queue at the same time. This class enforces synchronized
* access to all methods of this class. The implementation dynamically scales up and down
* the size of the underlying data structures as the number elements changes over time.
*
* @author Derk Norton
* @param The type of element managed by this collection.
*/
public class Queue extends ClosedCollection implements FIFO {
static private final XLogger logger = XLoggerFactory.getXLogger(Queue.class);
private final int capacity;
/**
* This constructor creates a new queue of unlimited capacity.
*/
public Queue() {
logger.entry();
this.capacity = Integer.MAX_VALUE;
logger.exit();
}
/**
* This constructor creates a new queue with the specified capacity.
*
* @param capacity The maximum number of elements that can be in the queue
* at one time.
*/
public Queue(int capacity) {
logger.entry(capacity);
this.capacity = capacity;
logger.exit();
}
@JsonValue
@Override
public E[] toArray() {
return super.toArray();
}
@Override
@SuppressWarnings("unchecked")
public Queue copy() {
return (Queue) super.copy();
}
@Override
public final synchronized void addElement(E element) throws InterruptedException {
logger.entry();
while (true) { // do this in a loop in case there are spurious wakeups (see Object.wait() javadoc)
int size = list.getSize();
if (size < capacity) {
logger.debug("Adding the element: " + element);
list.addElement(element);
notify(); // waiting removeElement() calls
break;
} else {
logger.debug("Waiting for the queue to drop from maximum capacity...");
wait(); // for a removeElement() call
}
}
logger.exit();
}
@Override
public final synchronized E removeElement() throws InterruptedException {
logger.entry();
E element = null;
while (true) { // do this in a loop in case there are spurious wakeups (see Object.wait() javadoc)
int size = list.getSize();
if (size > 0) {
element = list.removeElement(1);
logger.debug("Removed the element: " + element);
notify(); // waiting addElement() calls
break;
} else {
logger.debug("Waiting for an element to be added to the empty queue...");
wait(); // for an addElement() call
}
}
logger.exit(element);
return element;
}
@Override
public final synchronized E getHead() {
logger.entry();
E element = null;
int size = list.getSize();
if (size > 0) {
element = list.getElement(1);
}
logger.exit();
return element;
}
}