2
0

WorldObjectTree.java 2.9 KB

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