Rakesh API

org.rakeshv.utils
Class SimpleFIFO<T>

java.lang.Object
  extended by org.rakeshv.utils.SimpleFIFO<T>
All Implemented Interfaces:
DataStructure<T>

public class SimpleFIFO<T>
extends java.lang.Object
implements DataStructure<T>

A First In First Out implementation using an array.

Version:
$Id: SimpleFIFO.java,v 1.4 2006/03/11 16:31:24 rakesh Exp $
Author:
Rakesh Vidyadharan 2002 March 1
See Also:
SharedObject,

Copyright 2002-2006 Rakesh Vidyadharan


Field Summary
private  T[] fifo
          An array that is used to implement a FIFO.
private  boolean filled
          A flag to indicate that the fifo has been filled.
private  int index
          A variable that holds the index into the fifo at which a new instance is to be added.
 
Fields inherited from interface org.rakeshv.utils.DataStructure
DYNAMIC, FIFO, FIFO_IMPLEMENTATION, LRU, LRU_IMPLEMENTATION
 
Constructor Summary
SimpleFIFO(int size)
          Construct a new FIFO of the specified size.
 
Method Summary
 void add(T object)
          Add the specified object to fifo.
 void clear()
          Removes all the objects from the fifo.
 T get()
          Return the current Object in the fifo.
 T remove()
          Removes the current Object designated for removal (located at index) from the fifo.
 boolean remove(T object)
          Removes the specified object from the fifo.
 int size()
          Returns the size of the fifo.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fifo

private T[] fifo
An array that is used to implement a FIFO.


index

private int index
A variable that holds the index into the fifo at which a new instance is to be added. The index rolls around from the end of the fifo to the beginning when the fifo is full.


filled

private boolean filled
A flag to indicate that the fifo has been filled. This will be set only once after the fifo has been filled initially.

Constructor Detail

SimpleFIFO

public SimpleFIFO(int size)
Construct a new FIFO of the specified size.

Method Detail

get

public T get()
Return the current Object in the fifo. This is the object that will be replaced when the next call to add( Object ) is made.

Specified by:
get in interface DataStructure<T>
Returns:
The Object that is at the index within the fifo. This will be null if the fifo has not been filled yet.

add

public void add(T object)
Add the specified object to fifo. Updates the index after adding the specified object to the next location in the fifo. Sets filled to true when the index rolls around to the beginning of the fifo for the first time.

Note: This method does not perform any synchronisation, and hence is not thread-safe. Client's must perform their own synchronisation if they so desire.

Specified by:
add in interface DataStructure<T>
Parameters:
object - The object to be added to the fifo.
Throws:
java.lang.IllegalArgumentException - If a null value was specified for the object.

remove

public T remove()
Removes the current Object designated for removal (located at index) from the fifo.

Specified by:
remove in interface DataStructure<T>
Returns:
The Object that was removed.

remove

public boolean remove(T object)
Removes the specified object from the fifo. The time to remove the specified object will be directly proportional to the size of the fifo.

This operation can be very heavy, since it involves iterating through the fifo. The following conditions apply:

  1. Iterate through the fifo, and find the first matching object.
  2. Remove the matching object from the fifo.
  3. Move all succeeding objects in the fifo up by one position.
  4. Reset the index to point to the end of the fifo or to the previous position depending upon whether the fifo was filled or not.
  5. Matching is performed using an exact matching rule (the == operator), and not the equivalent matching rule (the Object.equals( Object )) operator.

If there are multiple instances of the same object in the fifo, only the first matching object is removed.

Note: This method does not perform any synchronisation, and hence is not thread-safe. Client's must perform their own synchronisation if they so desire.

Specified by:
remove in interface DataStructure<T>
Parameters:
object - The object that is to be removed from the fifo.
Returns:
Returns true if the specified object was found and removed from the fifo.

clear

public void clear()
Removes all the objects from the fifo. The most efficient way to do it is to create a new instance of the fifo array, and consign the old instance to the garbage collector.

Note: This method does not perform any synchronisation, and hence is not thread-safe. Client's must perform their own synchronisation if they so desire.

Specified by:
clear in interface DataStructure<T>

size

public int size()
Returns the size of the fifo. Returns the total size of the fifo, if the fifo has been filled, else returns the index value.

Specified by:
size in interface DataStructure<T>
Returns:
The size of the fifo.

Rakesh API

Copyright © 2002-2005 - Rakesh Vidyadharan