L2SyncMap.java 4.6 KB

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