2
0

L2SyncMap.java 4.7 KB

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