L2TIntObjectHashMap.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 gnu.trove.TIntObjectHashMap;
  17. import gnu.trove.TIntObjectProcedure;
  18. import gnu.trove.TIntProcedure;
  19. import gnu.trove.TObjectFunction;
  20. import gnu.trove.TObjectProcedure;
  21. import java.util.concurrent.locks.Lock;
  22. import java.util.concurrent.locks.ReentrantReadWriteLock;
  23. /**
  24. * Custom extension of TIntObjectHashMap that is synchronised via ReentrantReadWriteLock.
  25. * The purpose of this map is to replace the use of FastMap<K,V>.shared() which requires a lot of resources.
  26. *
  27. * @author Nik
  28. *
  29. * @param <V> value object.
  30. */
  31. public class L2TIntObjectHashMap<V extends Object> extends TIntObjectHashMap<V>
  32. {
  33. private final Lock _readLock;
  34. private final Lock _writeLock;
  35. public L2TIntObjectHashMap()
  36. {
  37. super();
  38. ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
  39. _readLock = lock.readLock();
  40. _writeLock = lock.writeLock();
  41. }
  42. public V put(int key, V value)
  43. {
  44. _writeLock.lock();
  45. try
  46. {
  47. return super.put(key, value);
  48. }
  49. finally
  50. {
  51. _writeLock.unlock();
  52. }
  53. }
  54. public V get(int key)
  55. {
  56. _readLock.lock();
  57. try
  58. {
  59. return super.get(key);
  60. }
  61. finally
  62. {
  63. _readLock.unlock();
  64. }
  65. }
  66. public void clear()
  67. {
  68. _writeLock.lock();
  69. try
  70. {
  71. super.clear();
  72. }
  73. finally
  74. {
  75. _writeLock.unlock();
  76. }
  77. }
  78. public V remove(int key)
  79. {
  80. _writeLock.lock();
  81. try
  82. {
  83. return super.remove(key);
  84. }
  85. finally
  86. {
  87. _writeLock.unlock();
  88. }
  89. }
  90. public boolean equals(Object other)
  91. {
  92. _readLock.lock();
  93. try
  94. {
  95. return super.equals(other);
  96. }
  97. finally
  98. {
  99. _readLock.unlock();
  100. }
  101. }
  102. public Object[] getValues()
  103. {
  104. _readLock.lock();
  105. try
  106. {
  107. return super.getValues();
  108. }
  109. finally
  110. {
  111. _readLock.unlock();
  112. }
  113. }
  114. public <T> T[] getValues(T[] arg0)
  115. {
  116. _readLock.lock();
  117. try
  118. {
  119. return super.getValues(arg0);
  120. }
  121. finally
  122. {
  123. _readLock.unlock();
  124. }
  125. }
  126. public int[] keys()
  127. {
  128. _readLock.lock();
  129. try
  130. {
  131. return super.keys();
  132. }
  133. finally
  134. {
  135. _readLock.unlock();
  136. }
  137. }
  138. public int[] keys(int[] arg0)
  139. {
  140. _readLock.lock();
  141. try
  142. {
  143. return super.keys(arg0);
  144. }
  145. finally
  146. {
  147. _readLock.unlock();
  148. }
  149. }
  150. public boolean contains(int val)
  151. {
  152. _readLock.lock();
  153. try
  154. {
  155. return super.contains(val);
  156. }
  157. finally
  158. {
  159. _readLock.unlock();
  160. }
  161. }
  162. public boolean containsValue(V arg0)
  163. {
  164. _readLock.lock();
  165. try
  166. {
  167. return super.containsValue(arg0);
  168. }
  169. finally
  170. {
  171. _readLock.unlock();
  172. }
  173. }
  174. public boolean containsKey(int key)
  175. {
  176. _readLock.lock();
  177. try
  178. {
  179. return super.containsKey(key);
  180. }
  181. finally
  182. {
  183. _readLock.unlock();
  184. }
  185. }
  186. public boolean forEachKey(TIntProcedure procedure)
  187. {
  188. _readLock.lock();
  189. try
  190. {
  191. return super.forEachKey(procedure);
  192. }
  193. finally
  194. {
  195. _readLock.unlock();
  196. }
  197. }
  198. public boolean forEachValue(TObjectProcedure<V> arg0)
  199. {
  200. _readLock.lock();
  201. try
  202. {
  203. return super.forEachValue(arg0);
  204. }
  205. finally
  206. {
  207. _readLock.unlock();
  208. }
  209. }
  210. public boolean forEachEntry(TIntObjectProcedure<V> arg0)
  211. {
  212. _readLock.lock();
  213. try
  214. {
  215. return super.forEachEntry(arg0);
  216. }
  217. finally
  218. {
  219. _readLock.unlock();
  220. }
  221. }
  222. public boolean retainEntries(TIntObjectProcedure<V> arg0)
  223. {
  224. _writeLock.lock();
  225. try
  226. {
  227. return super.retainEntries(arg0);
  228. }
  229. finally
  230. {
  231. _writeLock.unlock();
  232. }
  233. }
  234. public void transformValues(TObjectFunction<V,V> arg0)
  235. {
  236. _writeLock.lock();
  237. try
  238. {
  239. super.transformValues(arg0);
  240. }
  241. finally
  242. {
  243. _writeLock.unlock();
  244. }
  245. }
  246. }