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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2006-2010 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. /** This list's position pointer. */
  38. private int position = 0;
  39. /** Whether or not to add a fake empty item to the end of this list. */
  40. private boolean addEmpty;
  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, false otherwise.
  69. */
  70. public boolean remove(final Object o) {
  71. return items.remove(o);
  72. }
  73. /**
  74. * Determines if this list is currently empty.
  75. *
  76. * @return True if the list is empty, false otherwise.
  77. */
  78. public boolean isEmpty() {
  79. return items.isEmpty();
  80. }
  81. /**
  82. * Retrieves the item at the specified index in this list.
  83. *
  84. * @param index The index to look up
  85. * @return The item at the specified index
  86. */
  87. public T get(final int index) {
  88. return items.get(index);
  89. }
  90. /**
  91. * Determines if this list contains the specified object.
  92. *
  93. * @param o The object to be checked
  94. * @return True if this list contains the item, false otherwise.
  95. */
  96. public boolean contains(final Object o) {
  97. return items.contains(o);
  98. }
  99. /**
  100. * Clears all items from this list.
  101. */
  102. public void clear() {
  103. items.clear();
  104. }
  105. /**
  106. * Adds the specified item to this list. If the list has reached its
  107. * maximum capacity, this method will remove elements from the start of the
  108. * list until there is sufficient room for the new element.
  109. *
  110. * @param e The element to be added to the list.
  111. * @return True
  112. */
  113. public boolean add(T e) {
  114. while (items.size() > capacity - 1) {
  115. items.remove(0);
  116. position--;
  117. }
  118. return items.add(e);
  119. }
  120. /**
  121. * Retrieves the current position within the list.
  122. *
  123. * @return This list's positional pointer
  124. */
  125. public int getPosition() {
  126. return position;
  127. }
  128. /**
  129. * Sets the positional pointer of this list.
  130. *
  131. * @param position The new position
  132. */
  133. public void setPosition(int position) {
  134. this.position = position;
  135. }
  136. /**
  137. * Determines if there is an element after the positional pointer of the list.
  138. *
  139. * @return True if there is an element, false otherwise.
  140. */
  141. public boolean hasNext() {
  142. return (items.size() > position + 1) || ((items.size() > position) && addEmpty);
  143. }
  144. /**
  145. * Retrieves the element after the positional pointer of the list.
  146. *
  147. * @return The next element in the list
  148. */
  149. public T getNext() {
  150. if (items.size() > position + 1 || !addEmpty) {
  151. return get(++position);
  152. } else {
  153. position++;
  154. return empty;
  155. }
  156. }
  157. /**
  158. * Determines if there is an element befpre the positional pointer of the list.
  159. *
  160. * @return True if there is an element, false otherwise.
  161. */
  162. public boolean hasPrevious() {
  163. return 0 < position;
  164. }
  165. /**
  166. * Retrieves the element before the positional pointer of the list.
  167. *
  168. * @return The previous element in the list
  169. */
  170. public T getPrevious() {
  171. return get(--position);
  172. }
  173. /**
  174. * Sets the positional pointer of this list to the end.
  175. */
  176. public void seekToEnd() {
  177. position = items.size();
  178. }
  179. /**
  180. * Sets the positional pointer of this list to the start.
  181. */
  182. public void seekToStart() {
  183. position = 0;
  184. }
  185. /**
  186. * Retrieves a list of items that this rolling list contains.
  187. *
  188. * @return A list of items in this rolling list.
  189. */
  190. public List<T> getList() {
  191. return new ArrayList<T>(items);
  192. }
  193. }