Guest User

Untitled

a guest
Nov 17th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. /**
  2. * A class which represents a list of nodes.
  3. * @author Graham Edgecombe
  4. * @author Emperor
  5. * @param <E> The type of Node.
  6. */
  7. public class NodeList<E extends Node> implements Collection<E>, Iterable<E> {
  8.  
  9. /**
  10. * Internal nodes array.
  11. */
  12. private Node[] nodes;
  13.  
  14. /**
  15. * Current size.
  16. */
  17. private int size = 0;
  18.  
  19. /**
  20. * Creates a Node list with the specified capacity.
  21. * @param capacity The capacity.
  22. */
  23. public NodeList(int capacity) {
  24. nodes = new Node[capacity + 1]; // do not use idx 0
  25. }
  26.  
  27. /**
  28. * Gets an Node.
  29. * @param index The index.
  30. * @return The Node.
  31. * @throws IndexOutOufBoundException if the index is out of bounds.
  32. */
  33. @SuppressWarnings("unchecked")
  34. public E get(int index) {
  35. synchronized (this) {
  36. if (index <= 0 || index >= nodes.length) {
  37. throw new IndexOutOfBoundsException();
  38. }
  39. return (E) nodes[index];
  40. }
  41. }
  42.  
  43. /**
  44. * Gets the index of an Node.
  45. * @param Node The Node.
  46. * @return The index in the list.
  47. */
  48. public int indexOf(Node node) {
  49. return node.getIndex();
  50. }
  51.  
  52. /**
  53. * Gets the next free id.
  54. * @return The next free id.
  55. */
  56. private int getNextId() {
  57. for (int i = 1; i < nodes.length; i++) {
  58. if (nodes[i] == null) {
  59. return i;
  60. }
  61. }
  62. return -1;
  63. }
  64.  
  65. @Override
  66. public boolean add(E arg0) {
  67. synchronized (this) {
  68. int id = getNextId();
  69. if (id == -1) {
  70. return false;
  71. }
  72. nodes[id] = arg0;
  73. arg0.setIndex(id);
  74. size++;
  75. return true;
  76. }
  77. }
  78.  
  79. @Override
  80. public boolean addAll(Collection<? extends E> arg0) {
  81. synchronized (this) {
  82. boolean changed = false;
  83. for (E node : arg0) {
  84. if (add(node)) {
  85. changed = true;
  86. }
  87. }
  88. return changed;
  89. }
  90. }
  91.  
  92. @Override
  93. public void clear() {
  94. synchronized (this) {
  95. for (int i = 1; i < nodes.length; i++) {
  96. nodes[i] = null;
  97. }
  98. size = 0;
  99. }
  100. }
  101.  
  102. @Override
  103. public boolean contains(Object arg0) {
  104. synchronized (this) {
  105. for (int i = 1; i < nodes.length; i++) {
  106. if (nodes[i] == arg0) {
  107. return true;
  108. }
  109. }
  110. }
  111. return false;
  112. }
  113.  
  114. @Override
  115. public boolean containsAll(Collection<?> arg0) {
  116. boolean failed = false;
  117. for (Object o : arg0) {
  118. if (!contains(o)) {
  119. failed = true;
  120. }
  121. }
  122. return !failed;
  123. }
  124.  
  125. @Override
  126. public boolean isEmpty() {
  127. return size() == 0;
  128. }
  129.  
  130. @Override
  131. public Iterator<E> iterator() {
  132. return new NodeListIterator<E>(this);
  133. }
  134.  
  135. /**
  136. * Removes the node on this index.
  137. * @param index The index.
  138. * @return {@code True} if a node got removed.
  139. */
  140. public boolean remove(int index) {
  141. synchronized (this) {
  142. Node n = nodes[index];
  143. if (n != null) {
  144. size--;
  145. nodes[index] = null;
  146. }
  147. return n != null;
  148. }
  149. }
  150.  
  151. @Override
  152. public boolean remove(Object arg0) {
  153. synchronized (this) {
  154. for (int i = 1; i < nodes.length; i++) {
  155. if (nodes[i] == arg0) {
  156. nodes[i] = null;
  157. size--;
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. }
  164.  
  165. @Override
  166. public boolean removeAll(Collection<?> arg0) {
  167. synchronized (this) {
  168. boolean changed = false;
  169. for (Object o : arg0) {
  170. if (remove(o)) {
  171. changed = true;
  172. }
  173. }
  174. return changed;
  175. }
  176. }
  177.  
  178. @Override
  179. public boolean retainAll(Collection<?> arg0) {
  180. synchronized (this) {
  181. boolean changed = false;
  182. for (int i = 1; i < nodes.length; i++) {
  183. if (nodes[i] != null) {
  184. if (!arg0.contains(nodes[i])) {
  185. nodes[i] = null;
  186. size--;
  187. changed = true;
  188. }
  189. }
  190. }
  191. return changed;
  192. }
  193. }
  194.  
  195. @Override
  196. public int size() {
  197. return size;
  198. }
  199.  
  200. @Override
  201. public Node[] toArray() {
  202. synchronized (this) {
  203. int size = size();
  204. Node[] array = new Node[size];
  205. int ptr = 0;
  206. for (int i = 1; i < nodes.length; i++) {
  207. if (nodes[i] != null) {
  208. array[ptr++] = nodes[i];
  209. }
  210. }
  211. return array;
  212. }
  213. }
  214.  
  215. @SuppressWarnings("unchecked")
  216. @Override
  217. public <T> T[] toArray(T[] arg0) {
  218. Node[] arr = toArray();
  219. return (T[]) Arrays.copyOf(arr, arr.length, arg0.getClass());
  220. }
  221.  
  222. }
Add Comment
Please, Sign In to add comment