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

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