You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2013/10/15 10:58:55 UTC
svn commit: r1532252 -
/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/BasicHttpContext.java
Author: olegk
Date: Tue Oct 15 08:58:55 2013
New Revision: 1532252
URL: http://svn.apache.org/r1532252
Log:
BasicHttpContext is now backed by ConcurrentHashMap
Modified:
httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/BasicHttpContext.java
Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/BasicHttpContext.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/BasicHttpContext.java?rev=1532252&r1=1532251&r2=1532252&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/BasicHttpContext.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/protocol/BasicHttpContext.java Tue Oct 15 08:58:55 2013
@@ -27,25 +27,25 @@
package org.apache.http.protocol;
-import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
-import org.apache.http.annotation.NotThreadSafe;
+import org.apache.http.annotation.ThreadSafe;
import org.apache.http.util.Args;
/**
* Default implementation of {@link HttpContext}.
* <p>
- * Please note methods of this class are not synchronized and therefore may
- * be threading unsafe.
+ * Please note instances of this class can be thread unsafe if the
+ * parent context is not thread safe.
*
* @since 4.0
*/
-@NotThreadSafe
+@ThreadSafe
public class BasicHttpContext implements HttpContext {
private final HttpContext parentContext;
- private Map<String, Object> map = null;
+ private final Map<String, Object> map;
public BasicHttpContext() {
this(null);
@@ -53,15 +53,13 @@ public class BasicHttpContext implements
public BasicHttpContext(final HttpContext parentContext) {
super();
+ this.map = new ConcurrentHashMap<String, Object>();
this.parentContext = parentContext;
}
public Object getAttribute(final String id) {
Args.notNull(id, "Id");
- Object obj = null;
- if (this.map != null) {
- obj = this.map.get(id);
- }
+ Object obj = this.map.get(id);
if (obj == null && this.parentContext != null) {
obj = this.parentContext.getAttribute(id);
}
@@ -70,36 +68,28 @@ public class BasicHttpContext implements
public void setAttribute(final String id, final Object obj) {
Args.notNull(id, "Id");
- if (this.map == null) {
- this.map = new HashMap<String, Object>();
+ if (obj != null) {
+ this.map.put(id, obj);
+ } else {
+ this.map.remove(id);
}
- this.map.put(id, obj);
}
public Object removeAttribute(final String id) {
Args.notNull(id, "Id");
- if (this.map != null) {
- return this.map.remove(id);
- } else {
- return null;
- }
+ return this.map.remove(id);
}
/**
* @since 4.2
*/
public void clear() {
- if (this.map != null) {
- this.map.clear();
- }
+ this.map.clear();
}
@Override
public String toString() {
- if (this.map != null) {
- return this.map.toString();
- } else {
- return "{}";
- }
+ return this.map.toString();
}
+
}