WorldObjectTree.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. *
  23. * @author dishkols
  24. * @param <T>
  25. */
  26. public class WorldObjectTree<T extends L2Object> extends L2ObjectMap<T>
  27. {
  28. private final TreeMap<Integer, T> _objectMap = new TreeMap<Integer,T>();
  29. private final ReentrantReadWriteLock _rwl = new ReentrantReadWriteLock();
  30. private final Lock _r = _rwl.readLock();
  31. private final Lock _w = _rwl.writeLock();
  32. /**
  33. * @see com.l2jserver.gameserver.util.L2ObjectMap#size()
  34. */
  35. @Override
  36. public int size()
  37. {
  38. _r.lock();
  39. try {
  40. return _objectMap.size();
  41. }
  42. finally { _r.unlock(); }
  43. }
  44. /**
  45. * @see com.l2jserver.gameserver.util.L2ObjectMap#isEmpty()
  46. */
  47. @Override
  48. public boolean isEmpty()
  49. {
  50. _r.lock();
  51. try {
  52. return _objectMap.isEmpty();
  53. }
  54. finally { _r.unlock(); }
  55. }
  56. /**
  57. * @see com.l2jserver.gameserver.util.L2ObjectMap#clear()
  58. */
  59. @Override
  60. public void clear()
  61. {
  62. _w.lock();
  63. try {
  64. _objectMap.clear();
  65. }
  66. finally { _w.unlock(); }
  67. }
  68. /**
  69. * @see com.l2jserver.gameserver.util.L2ObjectMap#put(L2Object)
  70. */
  71. @Override
  72. public void put(T obj)
  73. {
  74. if ( obj != null) {
  75. _w.lock();
  76. try {
  77. _objectMap.put(obj.getObjectId(),obj);
  78. }
  79. finally { _w.unlock(); }
  80. }
  81. }
  82. /**
  83. * @see com.l2jserver.gameserver.util.L2ObjectMap#remove(L2Object)
  84. */
  85. @Override
  86. public void remove(T obj)
  87. {
  88. if (obj != null) {
  89. _w.lock();
  90. try {
  91. _objectMap.remove(obj.getObjectId());
  92. }
  93. finally { _w.unlock(); }
  94. }
  95. }
  96. /**
  97. * @see com.l2jserver.gameserver.util.L2ObjectMap#get(int)
  98. */
  99. @Override
  100. public T get(int id)
  101. {
  102. _r.lock();
  103. try {
  104. return _objectMap.get(id);
  105. }
  106. finally { _r.unlock(); }
  107. }
  108. /**
  109. * @see com.l2jserver.gameserver.util.L2ObjectMap#contains(L2Object)
  110. */
  111. @Override
  112. public boolean contains(T obj)
  113. {
  114. if (obj == null) return false;
  115. _r.lock();
  116. try {
  117. return _objectMap.containsValue(obj);
  118. }
  119. finally { _r.unlock(); }
  120. }
  121. /**
  122. * @see com.l2jserver.gameserver.util.L2ObjectMap#iterator()
  123. */
  124. @Override
  125. public Iterator<T> iterator()
  126. {
  127. _r.lock();
  128. try {
  129. return _objectMap.values().iterator();
  130. }
  131. finally { _r.unlock(); }
  132. }
  133. }