L2SyncMap.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 net.sf.l2j.util;
  16. import java.util.Collection;
  17. import java.util.Map;
  18. import java.util.Set;
  19. import net.sf.l2j.util.L2FastMap.I2ForEach;
  20. import net.sf.l2j.util.L2FastMap.I2ForEachKey;
  21. import net.sf.l2j.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. public synchronized boolean equals(Object o) {
  81. return _map.equals(o);
  82. }
  83. public synchronized int hashCode() {
  84. return _map.hashCode();
  85. }
  86. public synchronized String toString() {
  87. return _map.toString();
  88. }
  89. /**
  90. * Public method that iterate entire collection.<br>
  91. * <br>
  92. * @param func - a class method that must be executed on every element of collection.<br>
  93. * @return - returns true if entire collection is iterated, false if it`s been interrupted by
  94. * check method (I2ForEach.forEach())<br>
  95. */
  96. public synchronized final boolean ForEach(I2ForEach<K,V> func) {
  97. return _map.ForEach(func);
  98. }
  99. public synchronized final boolean ForEachValue(I2ForEachValue<V> func) {
  100. return _map.ForEachValue(func);
  101. }
  102. public synchronized final boolean ForEachKey(I2ForEachKey<K> func) {
  103. return _map.ForEachKey(func);
  104. }
  105. /**
  106. * <font color="red">Unsupported operation!!!</font>
  107. * @deprecated
  108. * @throws UnsupportedOperationException
  109. * @see java.util.Map#values()
  110. */
  111. public Collection<V> values() {
  112. throw new UnsupportedOperationException();
  113. }
  114. /**
  115. * <font color="red">Unsupported operation!!!</font>
  116. * @deprecated
  117. * @throws UnsupportedOperationException
  118. */
  119. public Set<K> keySet() {
  120. throw new UnsupportedOperationException();
  121. }
  122. /**
  123. * <font color="red">Unsupported operation!!!</font>
  124. * @deprecated
  125. * @throws UnsupportedOperationException
  126. * @see java.util.Map#entrySet()
  127. */
  128. public Set<Map.Entry<K,V>> entrySet() {
  129. throw new UnsupportedOperationException();
  130. }
  131. }