org.opcfoundation.ua.utils
Class IncubationQueue<T>

java.lang.Object
  extended by org.opcfoundation.ua.utils.IncubationQueue<T>

public class IncubationQueue<T>
extends Object

IncubationQueue is ordered queue where objects are added in two phases; (a) as incubating, they are given queuing number, and as (b) hatched they become available to the consumer. Objects become consumable when they are hatch()ed. The order of incubation is maintained. Objects become consumable in the order they were incubate()ed. This class is synchronized and is multi-thread-safe.

Example: IncubationQueue q = new IncubationQueue(); q.incubate("a"); q.incubate("b"); q.incubate("c"); q.hatch("b"); q.removeNextHatchedIfAvailable(); // returns null q.hatch("a"); q.removeNextHatched(); // returns "a" q.removeNextHatched(); // returns "b" q.removeNextHatchedIfAvailable(); // returns null q.hatch("c"); q.removeNextHatched(); // returns "c"

Author:
Toni Kalajainen (toni.kalajainen@iki.fi)

Constructor Summary
IncubationQueue()
          Create new Incubation queue that compares with equals()/hashCode
IncubationQueue(boolean identityComparison)
          Create new Incubation queue
 
Method Summary
 void clear()
          clear.
 boolean contains(T o)
          contains.
 T getNext()
          getNext.
 T getNextHatched()
          Get next hatched object, blocks if empty or unhatched
 T getNextHatchedIfAvailable()
          Get next hatched object
 boolean hatch(T o)
          Hatch incubating object o.
 void incubate(T o)
          Add object to the queue
 boolean isEmpty()
          isEmpty.
 boolean isHatched(T o)
          isHatched.
 boolean isIncubating(T o)
          isIncubating.
 Iterator<T> iterator()
          Non-thread safe iterator.
 boolean nextIsHatched()
          Is next object hatched
 T removeNextHatched()
          Remove next hatched object.
 T removeNextHatchedIfAvailable()
          Remove next hatched object if available
 T removeNextHatchedUninterruptibly()
          Remove next hatched object.
 int size()
          size.
 void waitUntilIncubated(T o)
          waitUntilIncubated.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IncubationQueue

public IncubationQueue()
Create new Incubation queue that compares with equals()/hashCode


IncubationQueue

public IncubationQueue(boolean identityComparison)
Create new Incubation queue

Parameters:
identityComparison - if true objects are compared with ==, false compare with equals()/hashCode()
Method Detail

incubate

public void incubate(T o)
Add object to the queue

Parameters:
o - object not null and not in queue

hatch

public boolean hatch(T o)
              throws IllegalArgumentException
Hatch incubating object o. If all objects in the queue before o are hatched, all of them are returned in the order they were added to the queue. If there is an object still incubating before o, an empty list is returned.

Parameters:
o - object not null
Returns:
true if o was incubating
Throws:
IllegalArgumentException - if o was not incubating

removeNextHatchedIfAvailable

public T removeNextHatchedIfAvailable()
Remove next hatched object if available

Returns:
next hatched object or null

removeNextHatched

public T removeNextHatched()
                    throws InterruptedException
Remove next hatched object. Blocks until next is available.

Returns:
next hatched object
Throws:
InterruptedException - if any.

removeNextHatchedUninterruptibly

public T removeNextHatchedUninterruptibly()
Remove next hatched object. Blocks until next is available.

Returns:
next hatched object

nextIsHatched

public boolean nextIsHatched()
Is next object hatched

Returns:
true if there is an object in queue and it is hatched

getNextHatchedIfAvailable

public T getNextHatchedIfAvailable()
Get next hatched object

Returns:
next hatched object or null

getNextHatched

public T getNextHatched()
                 throws InterruptedException
Get next hatched object, blocks if empty or unhatched

Returns:
next hatched object
Throws:
InterruptedException - if any.

getNext

public T getNext()
          throws InterruptedException

getNext.

Returns:
a T object.
Throws:
InterruptedException - if any.

isEmpty

public boolean isEmpty()

isEmpty.

Returns:
a boolean.

clear

public void clear()

clear.


size

public int size()

size.

Returns:
a int.

iterator

public Iterator<T> iterator()
Non-thread safe iterator. Removing is not allowed.

Returns:
iterator

contains

public boolean contains(T o)

contains.

Parameters:
o - a T object.
Returns:
a boolean.

isHatched

public boolean isHatched(T o)

isHatched.

Parameters:
o - a T object.
Returns:
a boolean.

isIncubating

public boolean isIncubating(T o)

isIncubating.

Parameters:
o - a T object.
Returns:
a boolean.

waitUntilIncubated

public void waitUntilIncubated(T o)
                        throws InterruptedException

waitUntilIncubated.

Parameters:
o - a T object.
Throws:
InterruptedException - if any.


Copyright © 2018. All rights reserved.