L2SyncMap.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License as published by
  3. * the Free Software Foundation; either version 2, or (at your option)
  4. * any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  14. * 02111-1307, USA.
  15. *
  16. * http://www.gnu.org/copyleft/gpl.html
  17. */
  18. package net.sf.l2j.util;
  19. import java.util.Collection;
  20. import java.util.Map;
  21. import java.util.Set;
  22. import net.sf.l2j.util.L2FastMap.I2ForEach;
  23. import net.sf.l2j.util.L2FastMap.I2ForEachKey;
  24. import net.sf.l2j.util.L2FastMap.I2ForEachValue;
  25. /**
  26. *
  27. * Fully synchronized version of L2FastMap class.<br>
  28. * In addition it`s provide ForEach methods and interfaces that can be used for iterating collection<br>
  29. * without using iterators. As addition its provide full lock on entire class if needed<br>
  30. * <font color="red">WARNING!!! methods: keySet(), values() and entrySet() are removed!</font>
  31. * <br>
  32. * @author Julian Version: 1.0.0 <2008-02-07> - Original release
  33. * @author Julian Varsion: 1.0.1 <2008-06-17> - Changed underlayng map to L2FastMap
  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. public synchronized V put(K key, V value) {
  40. return _map.put(key, value);
  41. }
  42. public synchronized V get(Object key) {
  43. return _map.get(key);
  44. }
  45. public synchronized V remove(Object key) {
  46. return _map.remove(key);
  47. }
  48. public synchronized boolean containsKey(Object key) {
  49. return _map.containsKey(key);
  50. }
  51. public synchronized int size() {
  52. return _map.size();
  53. }
  54. public synchronized boolean isEmpty() {
  55. return _map.isEmpty();
  56. }
  57. public synchronized void clear() {
  58. _map.clear();
  59. }
  60. /**
  61. * This method use specific locking strategy: map which have lowest hashCode() will be locked first
  62. * @see java.util.Map#putAll(java.util.Map)
  63. */
  64. public void putAll(Map<? extends K, ? extends V> map) {
  65. if (map == null || this == map) return;
  66. if (this.hashCode() <= map.hashCode())
  67. synchronized (this) {
  68. synchronized (map) {
  69. _map.putAll(map);
  70. }
  71. }
  72. else {
  73. synchronized (map) {
  74. synchronized(this) {
  75. _map.putAll(map);
  76. }
  77. }
  78. }
  79. }
  80. public synchronized boolean containsValue(Object value) {
  81. return _map.containsValue(value);
  82. }
  83. public synchronized boolean equals(Object o) {
  84. return _map.equals(o);
  85. }
  86. public synchronized int hashCode() {
  87. return _map.hashCode();
  88. }
  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. }