WorldObjectTree.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.util;
  16. import java.util.Iterator;
  17. import java.util.TreeMap;
  18. import java.util.concurrent.locks.Lock;
  19. import java.util.concurrent.locks.ReentrantReadWriteLock;
  20. import com.l2jserver.gameserver.model.L2Object;
  21. /**
  22. * @author dishkols
  23. * @param <T>
  24. */
  25. public class WorldObjectTree<T extends L2Object> extends L2ObjectMap<T>
  26. {
  27. private final TreeMap<Integer, T> _objectMap = new TreeMap<>();
  28. private final ReentrantReadWriteLock _rwl = new ReentrantReadWriteLock();
  29. private final Lock _r = _rwl.readLock();
  30. private final Lock _w = _rwl.writeLock();
  31. @Override
  32. public int size()
  33. {
  34. _r.lock();
  35. try
  36. {
  37. return _objectMap.size();
  38. }
  39. finally
  40. {
  41. _r.unlock();
  42. }
  43. }
  44. /**
  45. * @see com.l2jserver.gameserver.util.L2ObjectMap#isEmpty()
  46. */
  47. @Override
  48. public boolean isEmpty()
  49. {
  50. _r.lock();
  51. try
  52. {
  53. return _objectMap.isEmpty();
  54. }
  55. finally
  56. {
  57. _r.unlock();
  58. }
  59. }
  60. /**
  61. * @see com.l2jserver.gameserver.util.L2ObjectMap#clear()
  62. */
  63. @Override
  64. public void clear()
  65. {
  66. _w.lock();
  67. try
  68. {
  69. _objectMap.clear();
  70. }
  71. finally
  72. {
  73. _w.unlock();
  74. }
  75. }
  76. /**
  77. * @see com.l2jserver.gameserver.util.L2ObjectMap#put(L2Object)
  78. */
  79. @Override
  80. public void put(T obj)
  81. {
  82. if (obj != null)
  83. {
  84. _w.lock();
  85. try
  86. {
  87. _objectMap.put(obj.getObjectId(), obj);
  88. }
  89. finally
  90. {
  91. _w.unlock();
  92. }
  93. }
  94. }
  95. /**
  96. * @see com.l2jserver.gameserver.util.L2ObjectMap#remove(L2Object)
  97. */
  98. @Override
  99. public void remove(T obj)
  100. {
  101. if (obj != null)
  102. {
  103. _w.lock();
  104. try
  105. {
  106. _objectMap.remove(obj.getObjectId());
  107. }
  108. finally
  109. {
  110. _w.unlock();
  111. }
  112. }
  113. }
  114. /**
  115. * @see com.l2jserver.gameserver.util.L2ObjectMap#get(int)
  116. */
  117. @Override
  118. public T get(int id)
  119. {
  120. _r.lock();
  121. try
  122. {
  123. return _objectMap.get(id);
  124. }
  125. finally
  126. {
  127. _r.unlock();
  128. }
  129. }
  130. /**
  131. * @see com.l2jserver.gameserver.util.L2ObjectMap#contains(L2Object)
  132. */
  133. @Override
  134. public boolean contains(T obj)
  135. {
  136. if (obj == null)
  137. {
  138. return false;
  139. }
  140. _r.lock();
  141. try
  142. {
  143. return _objectMap.containsValue(obj);
  144. }
  145. finally
  146. {
  147. _r.unlock();
  148. }
  149. }
  150. /**
  151. * @see com.l2jserver.gameserver.util.L2ObjectMap#iterator()
  152. */
  153. @Override
  154. public Iterator<T> iterator()
  155. {
  156. _r.lock();
  157. try
  158. {
  159. return _objectMap.values().iterator();
  160. }
  161. finally
  162. {
  163. _r.unlock();
  164. }
  165. }
  166. }