Author: simonetripodi
Date: Wed Mar 2 09:08:54 2011
New Revision: 1076147
URL: http://svn.apache.org/viewvc?rev=1076147&view=rev
Log:
added 'auto-cast' feature for Digester stack operations
Modified:
commons/sandbox/digester3/trunk/pom.xml
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/Digester.java
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/DigesterImpl.java
commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/DigesterTestCase.java
commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/NamespaceSnapshotTestCase.java
commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/IncludeTestCase.java
Modified: commons/sandbox/digester3/trunk/pom.xml
URL:
http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/pom.xml?rev=1076147&r1=1076146&r2=1076147&view=diff
==============================================================================
--- commons/sandbox/digester3/trunk/pom.xml (original)
+++ commons/sandbox/digester3/trunk/pom.xml Wed Mar 2 09:08:54 2011
@@ -22,8 +22,8 @@
<parent>
<groupId>org.apache.commons</groupId>
- <artifactId>commons-sandbox-parent</artifactId>
- <version>9</version>
+ <artifactId>commons-parent</artifactId>
+ <version>19-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
Modified:
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/Digester.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/Digester.java?rev=1076147&r1=1076146&r2=1076147&view=diff
==============================================================================
---
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/Digester.java
(original)
+++
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/Digester.java
Wed Mar 2 09:08:54 2011
@@ -183,7 +183,7 @@ public interface Digester extends Conten
*
* @param object The new object
*/
- void push(Object object);
+ <T> void push(T object);
/**
* Pushes the given object onto the stack with the given name.
@@ -192,13 +192,13 @@ public interface Digester extends Conten
* @param stackName the name of the stack onto which the object should be
pushed
* @param value the Object to be pushed onto the named stack.
*/
- void push(String stackName, Object value);
+ <T> void push(String stackName, T value);
/**
* Pop the top object off of the stack, and return it. If there are
* no objects on the stack, return <code>null</code>.
*/
- Object pop();
+ <T> T pop();
/**
* <p>Pops (gets and removes) the top object from the stack with the given
name.</p>
@@ -211,13 +211,13 @@ public interface Digester extends Conten
* empty or has not been created yet
* @throws EmptyStackException if the named stack is empty
*/
- Object pop(String stackName);
+ <T> T pop(String stackName);
/**
* Return the top object on the stack without removing it. If there are
* no objects on the stack, return <code>null</code>.
*/
- Object peek();
+ <T> T peek();
/**
* <p>Gets the top object from the stack with the given name.
@@ -231,7 +231,7 @@ public interface Digester extends Conten
* empty or has not been created yet
* @throws EmptyStackException if the named stack is empty
*/
- Object peek(String stackName);
+ <T> T peek(String stackName);
/**
* Return the n'th object down the stack, where 0 is the top element
@@ -241,7 +241,7 @@ public interface Digester extends Conten
* @param n Index of the desired element, where 0 is the top of the stack,
* 1 is the next element down, and so on.
*/
- Object peek(int n);
+ <T> T peek(int n);
/**
* <p>Gets the top object from the stack with the given name.
@@ -256,7 +256,7 @@ public interface Digester extends Conten
* @return the specified <code>Object</code> on the stack.
* @throws EmptyStackException if the named stack is empty.
*/
- Object peek(String stackName, int n);
+ <T> T peek(String stackName, int n);
/**
* <p>Is the stack with the given name empty?</p>
Modified:
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/DigesterImpl.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/DigesterImpl.java?rev=1076147&r1=1076146&r2=1076147&view=diff
==============================================================================
---
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/DigesterImpl.java
(original)
+++
commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/internal/DigesterImpl.java
Wed Mar 2 09:08:54 2011
@@ -251,7 +251,7 @@ public final class DigesterImpl implemen
/**
* {@inheritDoc}
*/
- public void push(Object object) {
+ public <T> void push(T object) {
if (this.stack.size() == 0) {
this.root = object;
}
@@ -261,7 +261,7 @@ public final class DigesterImpl implemen
/**
* {@inheritDoc}
*/
- public void push(String stackName, Object value) {
+ public <T> void push(String stackName, T value) {
Stack<Object> namedStack = this.stacksByName.get(stackName);
if (namedStack == null) {
namedStack = new Stack<Object>();
@@ -273,9 +273,9 @@ public final class DigesterImpl implemen
/**
* {@inheritDoc}
*/
- public Object pop() {
+ public <T> T pop() {
try {
- return this.stack.pop();
+ return this.<T>npeSafeCast(this.stack.pop());
} catch (EmptyStackException e) {
this.log.warn("Empty stack (returning null)");
return null;
@@ -285,9 +285,7 @@ public final class DigesterImpl implemen
/**
* {@inheritDoc}
*/
- public Object pop(String stackName) {
- Object result = null;
-
+ public <T> T pop(String stackName) {
Stack<Object> namedStack = stacksByName.get(stackName);
if (namedStack == null) {
if (this.log.isDebugEnabled()) {
@@ -296,17 +294,15 @@ public final class DigesterImpl implemen
throw new EmptyStackException();
}
- result = namedStack.pop();
-
- return result;
+ return this.<T>npeSafeCast(namedStack.pop());
}
/**
* {@inheritDoc}
*/
- public Object peek() {
+ public <T> T peek() {
try {
- return this.stack.peek();
+ return this.<T>npeSafeCast(this.stack.peek());
} catch (EmptyStackException e) {
this.log.warn("Empty stack (returning null)");
return (null);
@@ -316,21 +312,21 @@ public final class DigesterImpl implemen
/**
* {@inheritDoc}
*/
- public Object peek(String stackName) {
- return this.peek(stackName, 0);
+ public <T> T peek(String stackName) {
+ return this.<T>npeSafeCast(this.peek(stackName, 0));
}
/**
* {@inheritDoc}
*/
- public Object peek(int n) {
+ public <T> T peek(int n) {
int index = (this.stack.size() - 1) - n;
if (index < 0) {
this.log.warn("Empty stack (returning null)");
return null;
}
try {
- return (this.stack.get(index));
+ return this.<T>npeSafeCast(this.stack.get(index));
} catch (EmptyStackException e) {
this.log.warn("Empty stack (returning null)");
return null;
@@ -340,8 +336,7 @@ public final class DigesterImpl implemen
/**
* {@inheritDoc}
*/
- public Object peek(String stackName, int n) {
- Object result = null;
+ public <T> T peek(String stackName, int n) {
Stack<Object> namedStack = this.stacksByName.get(stackName);
if (namedStack == null ) {
if (log.isDebugEnabled()) {
@@ -350,13 +345,23 @@ public final class DigesterImpl implemen
+ "' is empty");
}
throw new EmptyStackException();
- } else {
- int index = (namedStack.size() - 1) - n;
- if (index < 0) {
- throw new EmptyStackException();
- }
- result = namedStack.get(index);
}
+
+ int index = (namedStack.size() - 1) - n;
+ if (index < 0) {
+ throw new EmptyStackException();
+ }
+
+ return this.<T>npeSafeCast(namedStack.get(index));
+ }
+
+ private <T> T npeSafeCast(Object obj) {
+ if (obj == null) {
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ T result = (T) obj;
return result;
}
@@ -1043,13 +1048,7 @@ public final class DigesterImpl implemen
try {
this.reader.parse(input);
- if (this.root == null) {
- return null;
- }
-
- @SuppressWarnings("unchecked")
- T returned = (T) this.root;
- return returned;
+ return this.<T>npeSafeCast(this.root);
} finally {
this.cleanup();
}
Modified:
commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/DigesterTestCase.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/DigesterTestCase.java?rev=1076147&r1=1076146&r2=1076147&view=diff
==============================================================================
---
commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/DigesterTestCase.java
(original)
+++
commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/DigesterTestCase.java
Wed Mar 2 09:08:54 2011
@@ -180,7 +180,7 @@ public class DigesterTestCase extends Ab
public void testStackMethods() {
Digester digester = newEmptyDigester();
- Object value = null;
+ String value = null;
// New stack must be empty
assertEquals("New stack is empty", 0, digester.getCount());
Modified:
commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/NamespaceSnapshotTestCase.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/NamespaceSnapshotTestCase.java?rev=1076147&r1=1076146&r2=1076147&view=diff
==============================================================================
---
commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/NamespaceSnapshotTestCase.java
(original)
+++
commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/NamespaceSnapshotTestCase.java
Wed Mar 2 09:08:54 2011
@@ -43,7 +43,8 @@ public class NamespaceSnapshotTestCase e
public final void begin(final String namespace, final String name,
final Attributes attributes) {
Digester d = getDigester();
Map<String, String> namespaces = d.getCurrentNamespaces();
- ((NamespacedBox) d.peek()).setNamespaces(namespaces);
+ NamespacedBox namespacedBox = d.peek();
+ namespacedBox.setNamespaces(namespaces);
}
}
Modified:
commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/IncludeTestCase.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/IncludeTestCase.java?rev=1076147&r1=1076146&r2=1076147&view=diff
==============================================================================
---
commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/IncludeTestCase.java
(original)
+++
commons/sandbox/digester3/trunk/src/test/java/org/apache/commons/digester3/xmlrules/IncludeTestCase.java
Wed Mar 2 09:08:54 2011
@@ -44,7 +44,8 @@ public final class IncludeTestCase exten
@Override
public void body(String namespace, String name, String text)
throws Exception {
- ((ArrayList<String>) this.getDigester().peek()).add(text);
+ ArrayList<String> stringList = this.getDigester().peek();
+ stringList.add(text);
}
});