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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  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.collections;
  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. */
  31. public class RollingList<T> {
  32. /** The items in this rolling list. */
  33. private final List<T> items = new ArrayList<>();
  34. /** The maximum capacity of this list. */
  35. private final int capacity;
  36. /** Whether or not to add a fake empty item to the end of this list. */
  37. private final boolean addEmpty;
  38. /** This list's position pointer. */
  39. private int position;
  40. /** The "empty" item to be added. */
  41. private T empty;
  42. /**
  43. * Creates a new RollingList of the specified capacity.
  44. *
  45. * @param capacity The capacity of this list.
  46. */
  47. public RollingList(final int capacity) {
  48. this.capacity = capacity;
  49. this.addEmpty = false;
  50. }
  51. /**
  52. * Creates a new RollingList of the specified capacity, with the specified
  53. * "empty" element appended to the end.
  54. *
  55. * @param capacity The capacity of this list.
  56. * @param empty The "empty" element to be added
  57. */
  58. public RollingList(final int capacity, final T empty) {
  59. this.capacity = capacity;
  60. this.addEmpty = true;
  61. this.empty = empty;
  62. }
  63. /**
  64. * Removes the specified element from this list.
  65. *
  66. * @param o The object to be removed from the list.
  67. * @return True if the list contained the specified element,
  68. * false otherwise.
  69. */
  70. public boolean remove(final T 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 T 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(final 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(final int position) {
  134. this.position = position;
  135. }
  136. /**
  137. * Determines if there is an element after the positional pointer of
  138. * the list.
  139. *
  140. * @return True if there is an element, false otherwise.
  141. */
  142. public boolean hasNext() {
  143. return items.size() > position + 1 || items.size() > position && addEmpty;
  144. }
  145. /**
  146. * Retrieves the element after the positional pointer of the list.
  147. *
  148. * @return The next element in the list
  149. */
  150. public T getNext() {
  151. if (items.size() > position + 1 || !addEmpty) {
  152. return get(++position);
  153. } else {
  154. position++;
  155. return empty;
  156. }
  157. }
  158. /**
  159. * Determines if there is an element befpre the positional pointer of
  160. * the list.
  161. *
  162. * @return True if there is an element, false otherwise.
  163. */
  164. public boolean hasPrevious() {
  165. return 0 < position;
  166. }
  167. /**
  168. * Retrieves the element before the positional pointer of the list.
  169. *
  170. * @return The previous element in the list
  171. */
  172. public T getPrevious() {
  173. return get(--position);
  174. }
  175. /**
  176. * Sets the positional pointer of this list to the end.
  177. */
  178. public void seekToEnd() {
  179. position = items.size();
  180. }
  181. /**
  182. * Sets the positional pointer of this list to the start.
  183. */
  184. public void seekToStart() {
  185. position = 0;
  186. }
  187. /**
  188. * Retrieves a list of items that this rolling list contains.
  189. *
  190. * @return A list of items in this rolling list.
  191. */
  192. public List<T> getList() {
  193. return new ArrayList<>(items);
  194. }
  195. }