EventHandlerSet.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.ai2;
  16. import java.util.List;
  17. import com.l2jserver.gameserver.TaskPriority;
  18. import javolution.util.FastList;
  19. /**
  20. *
  21. * @author -Wooden-
  22. *
  23. */
  24. public class EventHandlerSet implements Comparable<EventHandlerSet>
  25. {
  26. private int _comparatorPrio;
  27. private long _insertionTime;
  28. private List<EventHandler> _handlers;
  29. private AiEventType _eventType;
  30. public EventHandlerSet(AiEventType event, List<EventHandler> handlers, TaskPriority prio)
  31. {
  32. _comparatorPrio = (prio.ordinal() + 1) * 3;
  33. _handlers = new FastList<EventHandler>();
  34. _eventType = event;
  35. for (EventHandler handler : handlers)
  36. addHandler(handler);
  37. }
  38. public EventHandlerSet(EventHandler handler, TaskPriority prio)
  39. {
  40. _comparatorPrio = (prio.ordinal() + 1) * 3;
  41. _handlers = new FastList<EventHandler>();
  42. _eventType = handler.getEvenType();
  43. addHandler(handler);
  44. }
  45. public void addHandler(EventHandler handler)
  46. {
  47. if (handler == null)
  48. return;
  49. int prio = handler.getPriority();
  50. int index = -1;
  51. for (EventHandler eventHandler : _handlers)
  52. {
  53. if (eventHandler.getPriority() <= prio)
  54. {
  55. index = eventHandler.getPriority();
  56. break;
  57. }
  58. }
  59. if (index != -1)
  60. {
  61. _handlers.add(index, handler);
  62. }
  63. else
  64. {
  65. _handlers.add(handler);
  66. }
  67. }
  68. public void setPrio(TaskPriority prio)
  69. {
  70. _comparatorPrio = (prio.ordinal() + 1) * 3;
  71. }
  72. public void stampInsertionTime()
  73. {
  74. _insertionTime = System.currentTimeMillis();
  75. }
  76. public int getComparatorPriority()
  77. {
  78. return _comparatorPrio;
  79. }
  80. public List<EventHandler> getHandlers()
  81. {
  82. return _handlers;
  83. }
  84. public AiEventType getEventType()
  85. {
  86. return _eventType;
  87. }
  88. /* (non-Javadoc)
  89. * @see java.lang.Comparable#compareTo(T)
  90. */
  91. public int compareTo(EventHandlerSet es)
  92. {
  93. return (int) ((System.currentTimeMillis() - _insertionTime) / 1000) + _comparatorPrio - es.getComparatorPriority();
  94. }
  95. @Override
  96. public String toString()
  97. {
  98. String str = "EventHandlerSet: size:" + _handlers.size() + " Priority:" + _comparatorPrio + (_insertionTime != 0 ? " TimePoints: " + (int) ((System.currentTimeMillis() - _insertionTime) / 1000) : "");
  99. for (EventHandler handler : _handlers)
  100. {
  101. str = str.concat(" - " + handler.toString());
  102. }
  103. return str;
  104. }
  105. }