Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

QueueHandler.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2006-2009 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.parser.irc.outputqueue;
  23. import com.dmdirc.parser.common.QueuePriority;
  24. import java.io.PrintWriter;
  25. import java.util.Comparator;
  26. import java.util.concurrent.BlockingQueue;
  27. /**
  28. * Sending queue.
  29. *
  30. * @author shane
  31. */
  32. public abstract class QueueHandler extends Thread implements Comparator<QueueItem> {
  33. /** Queue we are handling. */
  34. protected final BlockingQueue<QueueItem> queue;
  35. /** Where to send the output. */
  36. private final PrintWriter out;
  37. /** The output queue that owns us. */
  38. protected OutputQueue outputQueue;
  39. /**
  40. * Create a new Queue Thread
  41. *
  42. * @param outputQueue the OutputQueue that owns us.
  43. * @param queue Queue to handle
  44. * @param out Writer to send to.
  45. */
  46. public QueueHandler(final OutputQueue outputQueue, final BlockingQueue<QueueItem> queue, final PrintWriter out) {
  47. this.queue = queue;
  48. this.out = out;
  49. this.outputQueue = outputQueue;
  50. }
  51. /**
  52. * Send the given item
  53. *
  54. * @param line Line to send.
  55. */
  56. public void sendLine(final String line) {
  57. out.printf("%s\r\n", line);
  58. }
  59. /**
  60. * Get a new QueueItem for the given line and priority.
  61. * By default this will just create a new QueueItem with the given
  62. * parameters, but QueueHandlers are free to override it if they need to
  63. * instead produce subclasses of QueueItem or do anything else with the
  64. * data given.
  65. *
  66. * @param line Line to send
  67. * @param priority Priority of the line.
  68. * @return A QueueItem for teh given parameters
  69. */
  70. public QueueItem getQueueItem(final String line, final QueuePriority priority) {
  71. return new QueueItem(this, line, priority);
  72. }
  73. /**
  74. * Compare two QueueItems for sorting purposes.
  75. * This is called by the default QueueItem in its compareTo method. The
  76. * calling object will be the first parameter, the object to compare it to
  77. * will be second.
  78. * This allows QueueHandlers to sort items differently if needed.
  79. *
  80. * The default implementation works as follows:
  81. * Compare based on priorty firstly, if the priorities are the same,
  82. * compare based on the order the items were added to the queue.
  83. *
  84. * If an item has been in the queue longer than 10 seconds, it will not
  85. * check its priority and soley position itself based on adding order.
  86. *
  87. * @param mainObject Main object we are comparing against.
  88. * @param otherObject Object we are comparing to.
  89. * @return A QueueItem for teh given parameters
  90. */
  91. public int compare(final QueueItem mainObject, final QueueItem otherObject) {
  92. if (mainObject.getTime() < 10 * 1000 && mainObject.getPriority().compareTo(otherObject.getPriority()) != 0) {
  93. return mainObject.getPriority().compareTo(otherObject.getPriority());
  94. }
  95. if (mainObject.getItemNumber() > otherObject.getItemNumber()) {
  96. return 1;
  97. } else if (mainObject.getItemNumber() < otherObject.getItemNumber()) {
  98. return -1;
  99. } else {
  100. // This can't happen.
  101. return 0;
  102. }
  103. }
  104. /**
  105. * This is the main even loop of the queue.
  106. * It needs to handle pulling items out of the queue and calling
  107. * sendLine.
  108. *
  109. * It also needs to handle any delays in sending that it deems needed.
  110. */
  111. @Override
  112. public abstract void run();
  113. }