/hudson-core/src/main/java/hudson/util/CopyOnWriteMap.java

http://github.com/hudson/hudson · Java · 231 lines · 143 code · 38 blank · 50 comment · 0 complexity · 500e219558f9afec47782a9fd3588429 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.util;
  25. import com.thoughtworks.xstream.converters.MarshallingContext;
  26. import com.thoughtworks.xstream.converters.UnmarshallingContext;
  27. import com.thoughtworks.xstream.converters.collections.MapConverter;
  28. import com.thoughtworks.xstream.converters.collections.TreeMapConverter;
  29. import com.thoughtworks.xstream.io.HierarchicalStreamReader;
  30. import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
  31. import com.thoughtworks.xstream.mapper.Mapper;
  32. import java.util.Collection;
  33. import java.util.Collections;
  34. import java.util.Comparator;
  35. import java.util.HashMap;
  36. import java.util.LinkedHashMap;
  37. import java.util.Map;
  38. import java.util.Set;
  39. import java.util.TreeMap;
  40. /**
  41. * {@link Map} that has copy-on-write semantics.
  42. *
  43. * <p>
  44. * This class is suitable where highly concurrent access is needed, yet
  45. * the write operation is relatively uncommon.
  46. *
  47. * @author Kohsuke Kawaguchi
  48. */
  49. public abstract class CopyOnWriteMap<K,V> implements Map<K,V> {
  50. protected volatile Map<K,V> core;
  51. /**
  52. * Read-only view of {@link #core}.
  53. */
  54. private volatile Map<K,V> view;
  55. protected CopyOnWriteMap(Map<K,V> core) {
  56. update(core);
  57. }
  58. protected CopyOnWriteMap() {
  59. update(Collections.<K,V>emptyMap());
  60. }
  61. protected void update(Map<K,V> m) {
  62. core = m;
  63. view = Collections.unmodifiableMap(core);
  64. }
  65. public int size() {
  66. return core.size();
  67. }
  68. public boolean isEmpty() {
  69. return core.isEmpty();
  70. }
  71. public boolean containsKey(Object key) {
  72. return core.containsKey(key);
  73. }
  74. public boolean containsValue(Object value) {
  75. return core.containsValue(value);
  76. }
  77. public V get(Object key) {
  78. return core.get(key);
  79. }
  80. public synchronized V put(K key, V value) {
  81. Map<K,V> m = copy();
  82. V r = m.put(key,value);
  83. update(m);
  84. return r;
  85. }
  86. public synchronized V remove(Object key) {
  87. Map<K,V> m = copy();
  88. V r = m.remove(key);
  89. update(m);
  90. return r;
  91. }
  92. public synchronized void putAll(Map<? extends K, ? extends V> t) {
  93. Map<K,V> m = copy();
  94. m.putAll(t);
  95. update(m);
  96. }
  97. protected abstract Map<K,V> copy();
  98. public synchronized void clear() {
  99. update(Collections.<K,V>emptyMap());
  100. }
  101. /**
  102. * This method will return a read-only {@link Set}.
  103. */
  104. public Set<K> keySet() {
  105. return view.keySet();
  106. }
  107. /**
  108. * This method will return a read-only {@link Collection}.
  109. */
  110. public Collection<V> values() {
  111. return view.values();
  112. }
  113. /**
  114. * This method will return a read-only {@link Set}.
  115. */
  116. public Set<Entry<K,V>> entrySet() {
  117. return view.entrySet();
  118. }
  119. /**
  120. * {@link CopyOnWriteMap} backed by {@link HashMap}.
  121. */
  122. public static final class Hash<K,V> extends CopyOnWriteMap<K,V> {
  123. public Hash(Map<K,V> core) {
  124. super(new LinkedHashMap<K,V>(core));
  125. }
  126. public Hash() {
  127. }
  128. protected Map<K,V> copy() {
  129. return new LinkedHashMap<K,V>(core);
  130. }
  131. public static class ConverterImpl extends MapConverter {
  132. public ConverterImpl(Mapper mapper) {
  133. super(mapper);
  134. }
  135. @Override
  136. public boolean canConvert(Class type) {
  137. return type==Hash.class;
  138. }
  139. @Override
  140. public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
  141. return new Hash((Map) super.unmarshal(reader,context));
  142. }
  143. @Override
  144. public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
  145. super.marshal(((Hash)source).core,writer,context);
  146. }
  147. }
  148. }
  149. /**
  150. * {@link CopyOnWriteMap} backed by {@link TreeMap}.
  151. */
  152. public static final class Tree<K,V> extends CopyOnWriteMap<K,V> {
  153. private final Comparator<K> comparator;
  154. public Tree(Map<K,V> core, Comparator<K> comparator) {
  155. this(comparator);
  156. putAll(core);
  157. }
  158. public Tree(Comparator<K> comparator) {
  159. super(new TreeMap<K,V>(comparator));
  160. this.comparator = comparator;
  161. }
  162. public Tree() {
  163. this(null);
  164. }
  165. protected Map<K,V> copy() {
  166. TreeMap<K,V> m = new TreeMap<K,V>(comparator);
  167. m.putAll(core);
  168. return m;
  169. }
  170. @Override
  171. public synchronized void clear() {
  172. update(new TreeMap<K,V>(comparator));
  173. }
  174. public static class ConverterImpl extends TreeMapConverter {
  175. public ConverterImpl(Mapper mapper) {
  176. super(mapper);
  177. }
  178. @Override
  179. public boolean canConvert(Class type) {
  180. return type==Tree.class;
  181. }
  182. @Override
  183. public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
  184. TreeMap tm = (TreeMap) super.unmarshal(reader,context);
  185. return new Tree(tm,tm.comparator());
  186. }
  187. @Override
  188. public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
  189. super.marshal(((Tree)source).core,writer,context);
  190. }
  191. }
  192. }
  193. }