You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RollingList.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. /**
  26. * Implements a "rolling list". A rolling list has a maximum capacity, and
  27. * removes the oldest elements from the list to maintain this capacity.
  28. *
  29. * @param <T> The type if items that this list contains
  30. * @author chris
  31. */
  32. public class RollingList<T> {
  33. /** The items in this rolling list. */
  34. private final List<T> items = new ArrayList<T>();
  35. /** The maximum capacity of this list. */
  36. private final int capacity;
  37. /** Whether or not to add a fake empty item to the end of this list. */
  38. private final boolean addEmpty;
  39. /** This list's position pointer. */
  40. private int position = 0;
  41. /** The "empty" item to be added. */
  42. private T empty;
  43. /**
  44. * Creates a new RollingList of the specified capacity.
  45. *
  46. * @param capacity The capacity of this list.
  47. */
  48. public RollingList(final int capacity) {
  49. this.capacity = capacity;
  50. this.addEmpty = false;
  51. }
  52. /**
  53. * Creates a new RollingList of the specified capacity, with the specified
  54. * "empty" element appended to the end.
  55. *
  56. * @param capacity The capacity of this list.
  57. * @param empty The "empty" element to be added
  58. */
  59. public RollingList(final int capacity, final T empty) {
  60. this.capacity = capacity;
  61. this.addEmpty = true;
  62. this.empty = empty;
  63. }
  64. /**
  65. * Removes the specified element from this list.
  66. *
  67. * @param o The object to be removed from the list.
  68. * @return True if the list contained the specified element,
  69. * false otherwise.
  70. */
  71. public boolean remove(final T o) {
  72. return items.remove(o);
  73. }
  74. /**
  75. * Determines if this list is currently empty.
  76. *
  77. * @return True if the list is empty, false otherwise.
  78. */
  79. public boolean isEmpty() {
  80. return items.isEmpty();
  81. }
  82. /**
  83. * Retrieves the item at the specified index in this list.
  84. *
  85. * @param index The index to look up
  86. * @return The item at the specified index
  87. */
  88. public T get(final int index) {
  89. return items.get(index);
  90. }
  91. /**
  92. * Determines if this list contains the specified object.
  93. *
  94. * @param o The object to be checked
  95. * @return True if this list contains the item, false otherwise.
  96. */
  97. public boolean contains(final T o) {
  98. return items.contains(o);
  99. }
  100. /**
  101. * Clears all items from this list.
  102. */
  103. public void clear() {
  104. items.clear();
  105. }
  106. /**
  107. * Adds the specified item to this list. If the list has reached its
  108. * maximum capacity, this method will remove elements from the start of the
  109. * list until there is sufficient room for the new element.
  110. *
  111. * @param e The element to be added to the list.
  112. * @return True
  113. */
  114. public boolean add(final T e) {
  115. while (items.size() > capacity - 1) {
  116. items.remove(0);
  117. position--;
  118. }
  119. return items.add(e);
  120. }
  121. /**
  122. * Retrieves the current position within the list.
  123. *
  124. * @return This list's positional pointer
  125. */
  126. public int getPosition() {
  127. return position;
  128. }
  129. /**
  130. * Sets the positional pointer of this list.
  131. *
  132. * @param position The new position
  133. */
  134. public void setPosition(final int position) {
  135. this.position = position;
  136. }
  137. /**
  138. * Determines if there is an element after the positional pointer of
  139. * the list.
  140. *
  141. * @return True if there is an element, false otherwise.
  142. */
  143. public boolean hasNext() {
  144. return (items.size() > position + 1) || ((items.size() > position)
  145. && addEmpty);
  146. }
  147. /**
  148. * Retrieves the element after the positional pointer of the list.
  149. *
  150. * @return The next element in the list
  151. */
  152. public T getNext() {
  153. if (items.size() > position + 1 || !addEmpty) {
  154. return get(++position);
  155. } else {
  156. position++;
  157. return empty;
  158. }
  159. }
  160. /**
  161. * Determines if there is an element befpre the positional pointer of
  162. * the list.
  163. *
  164. * @return True if there is an element, false otherwise.
  165. */
  166. public boolean hasPrevious() {
  167. return 0 < position;
  168. }
  169. /**
  170. * Retrieves the element before the positional pointer of the list.
  171. *
  172. * @return The previous element in the list
  173. */
  174. public T getPrevious() {
  175. return get(--position);
  176. }
  177. /**
  178. * Sets the positional pointer of this list to the end.
  179. */
  180. public void seekToEnd() {
  181. position = items.size();
  182. }
  183. /**
  184. * Sets the positional pointer of this list to the start.
  185. */
  186. public void seekToStart() {
  187. position = 0;
  188. }
  189. /**
  190. * Retrieves a list of items that this rolling list contains.
  191. *
  192. * @return A list of items in this rolling list.
  193. */
  194. public List<T> getList() {
  195. return new ArrayList<T>(items);
  196. }
  197. }