Guest User

Untitled

a guest
Mar 31st, 2016
239
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /**
  3. *
  4. * @author Jure
  5. */
  6. public class FIFOArray
  7. {
  8. public int size;
  9. public int index; //not public on release
  10.  
  11. Object[] array;
  12. public FIFOArray()
  13. {
  14. this.size = 100;
  15. index = -1;
  16. array = new Object[100];
  17. }
  18.  
  19. public FIFOArray(int size)
  20. {
  21. this.size = size;
  22. index = -1;
  23. array = new Object[size];
  24. }
  25.  
  26. public Object getElement(int position, boolean sort)
  27. {
  28. if(sort)
  29. {
  30. int realPosition = position + index;
  31. if(realPosition < size)
  32. {
  33. return array[realPosition];
  34. }
  35. else
  36. {
  37. realPosition -= size;
  38. return array[realPosition];
  39. }
  40. }
  41. else return array[position];
  42. }
  43.  
  44.  
  45. public Object getLastElement(boolean delete)
  46. {
  47. if(isEmptey())
  48. {
  49. return null;
  50. }
  51. Object element = null;
  52. while(element == null)
  53. {
  54. element = array[index];
  55. if(delete)
  56. {
  57. array[index] = null;
  58. }
  59. decrementIndex();
  60. }
  61. return element;
  62. }
  63.  
  64. public Object getOldestElement(boolean delete)
  65. {
  66. if(isEmptey())
  67. {
  68. return null;
  69. }
  70. int position = (index + 1 < size) ? index + 1 : 0;
  71. Object oldest = array[position];
  72. while(oldest == null)
  73. {
  74. position = (position + 1 < size) ? position + 1 : 0;
  75. oldest = array[position];
  76. }
  77. if(delete)
  78. {
  79. array[position] = null;
  80. }
  81. return oldest;
  82. }
  83.  
  84. public void addElement(Object element)
  85. {
  86. incrementIndex();
  87. if((index + 1) < size)
  88. {
  89. array[index] = element;
  90. }
  91. else
  92. {
  93. array[index] = element;
  94. }
  95. }
  96.  
  97. public void incrementIndex()
  98. {
  99. if((index + 1) < size)
  100. {
  101. index++;
  102. }
  103. else
  104. {
  105. index = 0;
  106. }
  107. }
  108.  
  109. public void decrementIndex()
  110. {
  111. if((index - 1) < 0)
  112. {
  113. index = size - 1;
  114. }
  115. else
  116. {
  117. index -= 1;
  118. }
  119. }
  120.  
  121. public boolean isEmptey()
  122. {
  123. for(int i = 0; i < size; i++)
  124. {
  125. Object element = array[i];
  126. if(element != null)
  127. {
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133.  
  134. public boolean isFull()
  135. {
  136. for(int i = 0; i < size; i++)
  137. {
  138. Object element = array[i];
  139. if(element == null)
  140. {
  141. return false;
  142. }
  143. }
  144. return true;
  145. }
  146. }
RAW Paste Data