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.

QueuedLinkedHashSet.java 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.Iterator;
  19. import java.util.LinkedHashSet;
  20. import java.util.NoSuchElementException;
  21. import java.util.Queue;
  22. /**
  23. * A LinkedHashSet with a Queue implementation added, also supports readding
  24. * unique items to the head of the queue.
  25. *
  26. * @param <E> the type of elements held in this collection
  27. */
  28. public class QueuedLinkedHashSet<E> extends LinkedHashSet<E>
  29. implements Queue<E> {
  30. /**
  31. * A version number for this class. It should be changed whenever the class
  32. * structure is changed (or anything else that would prevent serialized
  33. * objects being unserialized with the new class).
  34. */
  35. private static final long serialVersionUID = 1;
  36. @Override
  37. public boolean offer(final E e) {
  38. return add(e);
  39. }
  40. @Override
  41. public E remove() {
  42. if (isEmpty()) {
  43. throw new NoSuchElementException("Queue is empty.");
  44. }
  45. return poll();
  46. }
  47. @Override
  48. public E poll() {
  49. final E object = peek();
  50. if (object != null) {
  51. remove(object);
  52. }
  53. return object;
  54. }
  55. @Override
  56. public E element() {
  57. if (isEmpty()) {
  58. throw new NoSuchElementException("Queue is empty.");
  59. }
  60. return peek();
  61. }
  62. @Override
  63. public E peek() {
  64. if (isEmpty()) {
  65. return null;
  66. }
  67. final Iterator<E> iterator = iterator();
  68. E object = null;
  69. while (iterator.hasNext()) {
  70. object = iterator.next();
  71. }
  72. return object;
  73. }
  74. /**
  75. * Offers an item to this queue, if the item exists in the queue it removes
  76. * it and re-adds it to the queue.
  77. *
  78. * @param e Object to add
  79. *
  80. * @return true iif the item was added
  81. */
  82. public boolean offerAndMove(final E e) {
  83. if (contains(e)) {
  84. remove(e);
  85. }
  86. return offer(e);
  87. }
  88. }