Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @author Jure
- */
- public class FIFOArray
- {
- public int size;
- public int index; //not public on release
- Object[] array;
- public FIFOArray()
- {
- this.size = 100;
- index = -1;
- array = new Object[100];
- }
- public FIFOArray(int size)
- {
- this.size = size;
- index = -1;
- array = new Object[size];
- }
- public Object getElement(int position, boolean sort)
- {
- if(sort)
- {
- int realPosition = position + index;
- if(realPosition < size)
- {
- return array[realPosition];
- }
- else
- {
- realPosition -= size;
- return array[realPosition];
- }
- }
- else return array[position];
- }
- public Object getLastElement(boolean delete)
- {
- if(isEmptey())
- {
- return null;
- }
- Object element = null;
- while(element == null)
- {
- element = array[index];
- if(delete)
- {
- array[index] = null;
- }
- decrementIndex();
- }
- return element;
- }
- public Object getOldestElement(boolean delete)
- {
- if(isEmptey())
- {
- return null;
- }
- int position = (index + 1 < size) ? index + 1 : 0;
- Object oldest = array[position];
- while(oldest == null)
- {
- position = (position + 1 < size) ? position + 1 : 0;
- oldest = array[position];
- }
- if(delete)
- {
- array[position] = null;
- }
- return oldest;
- }
- public void addElement(Object element)
- {
- incrementIndex();
- if((index + 1) < size)
- {
- array[index] = element;
- }
- else
- {
- array[index] = element;
- }
- }
- public void incrementIndex()
- {
- if((index + 1) < size)
- {
- index++;
- }
- else
- {
- index = 0;
- }
- }
- public void decrementIndex()
- {
- if((index - 1) < 0)
- {
- index = size - 1;
- }
- else
- {
- index -= 1;
- }
- }
- public boolean isEmptey()
- {
- for(int i = 0; i < size; i++)
- {
- Object element = array[i];
- if(element != null)
- {
- return false;
- }
- }
- return true;
- }
- public boolean isFull()
- {
- for(int i = 0; i < size; i++)
- {
- Object element = array[i];
- if(element == null)
- {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement