L2SyncMap.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.Collection;
  17. import java.util.Map;
  18. import java.util.Set;
  19. import com.l2jserver.util.L2FastMap;
  20. import com.l2jserver.util.L2FastMap.I2ForEach;
  21. import com.l2jserver.util.L2FastMap.I2ForEachKey;
  22. import com.l2jserver.util.L2FastMap.I2ForEachValue;
  23. /**
  24. * Fully synchronized version of L2FastMap class.<br>
  25. * In addition it`s provide ForEach methods and interfaces that can be used for iterating collection without using iterators.<br>
  26. * As addition its provide full lock on entire class if needed<br>
  27. * <font color="red">WARNING!!! methods: keySet(), values() and entrySet() are removed!</font> <br>
  28. * @author Julian Version: 1.0.0 <2008-02-07> - Original release
  29. * @author Julian Varsion: 1.0.1 <2008-06-17> - Changed underlying map to L2FastMap
  30. * @param <K>
  31. * @param <V>
  32. */
  33. public class L2SyncMap<K extends Object, V extends Object> implements Map<K, V>
  34. {
  35. private final L2FastMap<K, V> _map = new L2FastMap<>();
  36. @Override
  37. public synchronized V put(K key, V value)
  38. {
  39. return _map.put(key, value);
  40. }
  41. @Override
  42. public synchronized V get(Object key)
  43. {
  44. return _map.get(key);
  45. }
  46. @Override
  47. public synchronized V remove(Object key)
  48. {
  49. return _map.remove(key);
  50. }
  51. @Override
  52. public synchronized boolean containsKey(Object key)
  53. {
  54. return _map.containsKey(key);
  55. }
  56. @Override
  57. public synchronized int size()
  58. {
  59. return _map.size();
  60. }
  61. @Override
  62. public synchronized boolean isEmpty()
  63. {
  64. return _map.isEmpty();
  65. }
  66. @Override
  67. public synchronized void clear()
  68. {
  69. _map.clear();
  70. }
  71. /**
  72. * This method use specific locking strategy: map which have lowest hashCode() will be locked first
  73. * @see java.util.Map#putAll(java.util.Map)
  74. */
  75. @Override
  76. public void putAll(Map<? extends K, ? extends V> map)
  77. {
  78. if ((map == null) || (this == map))
  79. {
  80. return;
  81. }
  82. if (this.hashCode() <= map.hashCode())
  83. {
  84. synchronized (this)
  85. {
  86. synchronized (map)
  87. {
  88. _map.putAll(map);
  89. }
  90. }
  91. }
  92. else
  93. {
  94. synchronized (map)
  95. {
  96. synchronized (this)
  97. {
  98. _map.putAll(map);
  99. }
  100. }
  101. }
  102. }
  103. @Override
  104. public synchronized boolean containsValue(Object value)
  105. {
  106. return _map.containsValue(value);
  107. }
  108. @Override
  109. public synchronized boolean equals(Object o)
  110. {
  111. return _map.equals(o);
  112. }
  113. @Override
  114. public synchronized int hashCode()
  115. {
  116. return _map.hashCode();
  117. }
  118. @Override
  119. public synchronized String toString()
  120. {
  121. return _map.toString();
  122. }
  123. /**
  124. * Public method that iterate entire collection.<br>
  125. * <br>
  126. * @param func - a class method that must be executed on every element of collection.<br>
  127. * @return - returns true if entire collection is iterated, false if it`s been interrupted by check method (I2ForEach.forEach())<br>
  128. */
  129. public synchronized final boolean ForEach(I2ForEach<K, V> func)
  130. {
  131. return _map.ForEach(func);
  132. }
  133. public synchronized final boolean ForEachValue(I2ForEachValue<V> func)
  134. {
  135. return _map.ForEachValue(func);
  136. }
  137. public synchronized final boolean ForEachKey(I2ForEachKey<K> func)
  138. {
  139. return _map.ForEachKey(func);
  140. }
  141. /**
  142. * <font color="red">Unsupported operation!!!</font>
  143. * @deprecated
  144. * @throws UnsupportedOperationException
  145. * @see java.util.Map#values()
  146. */
  147. @Override
  148. @Deprecated
  149. public Collection<V> values()
  150. {
  151. throw new UnsupportedOperationException();
  152. }
  153. /**
  154. * <font color="red">Unsupported operation!!!</font>
  155. * @return
  156. * @deprecated
  157. * @throws UnsupportedOperationException
  158. */
  159. @Override
  160. @Deprecated
  161. public Set<K> keySet()
  162. {
  163. throw new UnsupportedOperationException();
  164. }
  165. /**
  166. * <font color="red">Unsupported operation!!!</font>
  167. * @deprecated
  168. * @throws UnsupportedOperationException
  169. * @see java.util.Map#entrySet()
  170. */
  171. @Override
  172. @Deprecated
  173. public Set<Map.Entry<K, V>> entrySet()
  174. {
  175. throw new UnsupportedOperationException();
  176. }
  177. }