Updated Branches: refs/heads/trunk d45add2f8 -> 2ac9a8567
use ConcurrentHashMap in place of read/write lock for the session attribute container Project: http://git-wip-us.apache.org/repos/asf/mina/repo Commit: http://git-wip-us.apache.org/repos/asf/mina/commit/2ac9a856 Tree: http://git-wip-us.apache.org/repos/asf/mina/tree/2ac9a856 Diff: http://git-wip-us.apache.org/repos/asf/mina/diff/2ac9a856 Branch: refs/heads/trunk Commit: 2ac9a856794bc792ef942b1ed3b3d895565c51e6 Parents: d940fc0 Author: jvermillard <[email protected]> Authored: Mon May 20 12:35:42 2013 +0200 Committer: jvermillard <[email protected]> Committed: Mon May 20 12:37:45 2013 +0200 ---------------------------------------------------------------------- .../mina/session/DefaultAttributeContainer.java | 154 +++++---------- 1 files changed, 49 insertions(+), 105 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina/blob/2ac9a856/core/src/main/java/org/apache/mina/session/DefaultAttributeContainer.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/mina/session/DefaultAttributeContainer.java b/core/src/main/java/org/apache/mina/session/DefaultAttributeContainer.java index 687d4a7..7d4a02e 100644 --- a/core/src/main/java/org/apache/mina/session/DefaultAttributeContainer.java +++ b/core/src/main/java/org/apache/mina/session/DefaultAttributeContainer.java @@ -23,16 +23,13 @@ import static java.util.Collections.unmodifiableSet; import static org.apache.mina.util.Assert.assertNotNull; import java.util.Collections; -import java.util.HashMap; import java.util.Map; import java.util.Set; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.concurrent.ConcurrentHashMap; /** - * An {@link AttributeContainer} provides type-safe access to attribute values, - * using {@link AttributeKey}' s which as reference-key to an attribute value. <br> + * An {@link AttributeContainer} provides type-safe access to attribute values, using {@link AttributeKey}' s which as + * reference-key to an attribute value. <br> * <br> * This class is Thread-Safe ! * @@ -46,123 +43,84 @@ final class DefaultAttributeContainer implements AttributeContainer { * <li>Value: the attribute value * </ul> */ - private final Map<AttributeKey<?>, Object> attributes = new HashMap<AttributeKey<?>, Object>(); - - /** Synchronizes the read operations */ - private final Lock readLock; - - /** Synchronizes the write operations */ - private final Lock writeLock; - - { - ReadWriteLock rwLock = new ReentrantReadWriteLock(); - readLock = rwLock.readLock(); - writeLock = rwLock.writeLock(); - } + private final Map<AttributeKey<?>, Object> attributes = new ConcurrentHashMap<AttributeKey<?>, Object>(); /** - * Returns the value of the user-defined attribute for the given - * <code>key</code>. + * Returns the value of the user-defined attribute for the given <code>key</code>. * - * @param key - * the attribute's key, must not be <code>null</code> + * @param key the attribute's key, must not be <code>null</code> * @return <tt>null</tt> if there is no attribute with the specified key - * @exception IllegalArgumentException - * if <code>key==null</code> + * @exception IllegalArgumentException if <code>key==null</code> * @see #setAttribute(AttributeKey, Object) */ + @Override @SuppressWarnings("unchecked") public <T> T getAttribute(AttributeKey<T> key) { assertNotNull(key, "key"); - readLock.lock(); - try { - T value = (T) attributes.get(key); + T value = (T) attributes.get(key); - return value; - } finally { - readLock.unlock(); - } + return value; } /** - * Returns the value of the user-defined attribute for the given - * <code>key</code>. + * Returns the value of the user-defined attribute for the given <code>key</code>. * - * @param key - * the attribute's key, must not be <code>null</code> + * @param key the attribute's key, must not be <code>null</code> * @return <tt>null</tt> if there is no attribute with the specified key - * @exception IllegalArgumentException - * if <code>key==null</code> + * @exception IllegalArgumentException if <code>key==null</code> * @see #setAttribute(AttributeKey, Object) */ + @Override @SuppressWarnings("unchecked") public <T> T getAttribute(AttributeKey<T> key, T defaultValue) { assertNotNull(key, "key"); - readLock.lock(); - - try { - T value = (T) attributes.get(key); - if (value != null) { - return value; - } + T value = (T) attributes.get(key); - return defaultValue; - } finally { - readLock.unlock(); + if (value != null) { + return value; } + + return defaultValue; } /** - * Sets a user-defined attribute. If the <code>value</code> is - * <code>null</code> the attribute will be removed from this container. + * Sets a user-defined attribute. If the <code>value</code> is <code>null</code> the attribute will be removed from + * this container. * - * @param key - * the attribute's key, must not be <code>null</code> - * @param value - * the attribute's value, <code>null</code> to remove the - * attribute - * @return The old attribute's value, <code>null</code> if there is no - * previous value - * @exception IllegalArgumentException - * <ul> - * <li>if <code>key==null</code> - * <li>if <code>value</code> is not <code>null</code> and not - * an instance of type that is specified in by the given - * <code>key</code> (see {@link AttributeKey#getType()}) + * @param key the attribute's key, must not be <code>null</code> + * @param value the attribute's value, <code>null</code> to remove the attribute + * @return The old attribute's value, <code>null</code> if there is no previous value + * @exception IllegalArgumentException <ul> + * <li>if <code>key==null</code> + * <li>if <code>value</code> is not <code>null</code> and not an instance of type that is specified in by + * the given <code>key</code> (see {@link AttributeKey#getType()}) * - * </ul> + * </ul> * * @see #getAttribute(AttributeKey) */ + @Override @SuppressWarnings("unchecked") public <T> T setAttribute(AttributeKey<? extends T> key, T value) { assertNotNull(key, "key"); assertValueIsOfExpectedType(key, value); - writeLock.lock(); - - try { - if (value == null) { - return removeAttribute(key); - } - - return (T) attributes.put(key, value); - } finally { - writeLock.unlock(); + if (value == null) { + return removeAttribute(key); } + + return (T) attributes.put(key, value); } /** - * Throws an {@link IllegalArgumentException} if the given - * <code>value</code> is not of the expected type and not <code>null</code>. + * Throws an {@link IllegalArgumentException} if the given <code>value</code> is not of the expected type and not + * <code>null</code>. * * @param <T> * @param key * @param value - * @exception IllegalArgumentException - * if <code>value</code> is not an instance of - * {@link AttributeKey#getType()} + * @exception IllegalArgumentException if <code>value</code> is not an instance of {@link AttributeKey#getType()} */ private static <T> void assertValueIsOfExpectedType(AttributeKey<? extends T> key, T value) { if (value == null) { @@ -179,45 +137,31 @@ final class DefaultAttributeContainer implements AttributeContainer { } /** - * Returns an unmodifiable {@link Set} of all Keys of this container. If - * this container contains no key's an empty {@link Set} will be returned. + * Returns an unmodifiable {@link Set} of all Keys of this container. If this container contains no key's an empty + * {@link Set} will be returned. * * @return all Keys, never <code>null</code> * @see Collections#unmodifiableSet(Set) */ + @Override public Set<AttributeKey<?>> getAttributeKeys() { - readLock.lock(); - - try { - return unmodifiableSet(attributes.keySet()); - } finally { - readLock.unlock(); - } + return unmodifiableSet(attributes.keySet()); } /** - * Removes the specified Attribute from this container. The old value will - * be returned, <code>null</code> will be returned if there is no such - * attribute in this container.<br> + * Removes the specified Attribute from this container. The old value will be returned, <code>null</code> will be + * returned if there is no such attribute in this container.<br> * <br> * This method is equivalent to <code>setAttribute(key,null)</code>. * - * @param key - * of the attribute to be removed,must not be <code>null</code> - * @return the removed value, <code>null</code> if this container doesn't - * contain the specified attribute - * @exception IllegalArgumentException - * if <code>key==null</code> + * @param key of the attribute to be removed,must not be <code>null</code> + * @return the removed value, <code>null</code> if this container doesn't contain the specified attribute + * @exception IllegalArgumentException if <code>key==null</code> */ + @Override @SuppressWarnings("unchecked") public <T> T removeAttribute(AttributeKey<T> key) { assertNotNull(key, "key"); - writeLock.lock(); - - try { - return (T) attributes.remove(key); - } finally { - writeLock.unlock(); - } + return (T) attributes.remove(key); } -} +} \ No newline at end of file
