Author: simonetripodi
Date: Sun Sep 4 17:02:05 2011
New Revision: 1165075
URL: http://svn.apache.org/viewvc?rev=1165075&view=rev
Log:
[CHAIN-56] clever Context with generic type auto-cast feature
Modified:
commons/proper/chain/branches/version-2.0-work/src/main/java/org/apache/commons/chain/Context.java
commons/proper/chain/branches/version-2.0-work/src/main/java/org/apache/commons/chain/impl/ContextBase.java
commons/proper/chain/branches/version-2.0-work/src/test/java/org/apache/commons/chain/generic/DispatchCommandTestCase.java
Modified:
commons/proper/chain/branches/version-2.0-work/src/main/java/org/apache/commons/chain/Context.java
URL:
http://svn.apache.org/viewvc/commons/proper/chain/branches/version-2.0-work/src/main/java/org/apache/commons/chain/Context.java?rev=1165075&r1=1165074&r2=1165075&view=diff
==============================================================================
---
commons/proper/chain/branches/version-2.0-work/src/main/java/org/apache/commons/chain/Context.java
(original)
+++
commons/proper/chain/branches/version-2.0-work/src/main/java/org/apache/commons/chain/Context.java
Sun Sep 4 17:02:05 2011
@@ -62,5 +62,19 @@ import java.util.Map;
public interface Context extends Map<String, Object> {
+ /**
+ * That method enhances the {@link #get(Object)} method that helps users
+ * avoid the redundant code of type cast/checking when assignments are
already known.
+ *
+ * It throws {@code ClassCastException} if types are not assignable.
+ *
+ * @param <T> the target assignment type
+ * @param key the key whose associated value is to be returned
+ * @return the value to which the specified key is mapped,
+ * or {@code null} if this map contains no mapping for the key
+ * @see #get(Object)
+ * @since 2.0
+ */
+ <T> T retrieve(String key);
}
Modified:
commons/proper/chain/branches/version-2.0-work/src/main/java/org/apache/commons/chain/impl/ContextBase.java
URL:
http://svn.apache.org/viewvc/commons/proper/chain/branches/version-2.0-work/src/main/java/org/apache/commons/chain/impl/ContextBase.java?rev=1165075&r1=1165074&r2=1165075&view=diff
==============================================================================
---
commons/proper/chain/branches/version-2.0-work/src/main/java/org/apache/commons/chain/impl/ContextBase.java
(original)
+++
commons/proper/chain/branches/version-2.0-work/src/main/java/org/apache/commons/chain/impl/ContextBase.java
Sun Sep 4 17:02:05 2011
@@ -351,6 +351,20 @@ public class ContextBase extends Concurr
/**
+ * {@inheritDoc}
+ */
+ public <T> T retrieve(String key) {
+ Object valueObject = get(key);
+ if (valueObject == null) {
+ return null;
+ }
+ @SuppressWarnings("unchecked") // will throw ClassCastException if
type are not assignable
+ T value = (T) valueObject;
+ return value;
+ }
+
+
+ /**
* <p>Override the default <code>Map</code> behavior to call the
* <code>put()</code> method individually for each key-value pair
* in the specified <code>Map</code>.</p>
Modified:
commons/proper/chain/branches/version-2.0-work/src/test/java/org/apache/commons/chain/generic/DispatchCommandTestCase.java
URL:
http://svn.apache.org/viewvc/commons/proper/chain/branches/version-2.0-work/src/test/java/org/apache/commons/chain/generic/DispatchCommandTestCase.java?rev=1165075&r1=1165074&r2=1165075&view=diff
==============================================================================
---
commons/proper/chain/branches/version-2.0-work/src/test/java/org/apache/commons/chain/generic/DispatchCommandTestCase.java
(original)
+++
commons/proper/chain/branches/version-2.0-work/src/test/java/org/apache/commons/chain/generic/DispatchCommandTestCase.java
Sun Sep 4 17:02:05 2011
@@ -137,6 +137,10 @@ public class DispatchCommandTestCase ext
return this.wrappedContext.get(o);
}
+ public <T> T retrieve(String key) {
+ return wrappedContext.retrieve(key);
+ }
+
@Override
public Object put(String key, Object value) {
return this.wrappedContext.put(key, value);