From c7825d49bebcfdcb410868581289b98504771bf3 Mon Sep 17 00:00:00 2001 From: robokaso Date: Tue, 15 Jul 2008 12:39:16 +0000 Subject: [PATCH] IN PROGRESS - BATCH-709: Change all collections to use generics --- .../RethrowOnThresholdExceptionHandler.java | 39 +++++++------------ .../SimpleLimitExceptionHandler.java | 10 ++--- .../listener/CompositeRepeatListener.java | 18 +++------ .../policy/CompositeCompletionPolicy.java | 6 +-- ...throwOnThresholdExceptionHandlerTests.java | 21 +++------- 5 files changed, 35 insertions(+), 59 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/exception/RethrowOnThresholdExceptionHandler.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/exception/RethrowOnThresholdExceptionHandler.java index b7776a134..654a305c4 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/exception/RethrowOnThresholdExceptionHandler.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/exception/RethrowOnThresholdExceptionHandler.java @@ -17,8 +17,8 @@ package org.springframework.batch.repeat.exception; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; +import java.util.Map.Entry; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -39,12 +39,11 @@ import org.springframework.util.Assert; */ public class RethrowOnThresholdExceptionHandler implements ExceptionHandler { - protected final Log logger = LogFactory - .getLog(RethrowOnThresholdExceptionHandler.class); + protected final Log logger = LogFactory.getLog(RethrowOnThresholdExceptionHandler.class); private ExceptionClassifier exceptionClassifier = new ExceptionClassifierSupport(); - private Map thresholds = new HashMap(); + private Map thresholds = new HashMap(); private boolean useParent = false; @@ -52,9 +51,8 @@ public class RethrowOnThresholdExceptionHandler implements ExceptionHandler { * Flag to indicate the the exception counters should be shared between * sibling contexts in a nested batch. Default is false. * - * @param useParent - * true if the parent context should be used to store the - * counters. + * @param useParent true if the parent context should be used to store the + * counters. */ public void setUseParent(boolean useParent) { this.useParent = useParent; @@ -75,21 +73,16 @@ public class RethrowOnThresholdExceptionHandler implements ExceptionHandler { * are usually String literals, depending on the {@link ExceptionClassifier} * implementation used. * - * @param thresholds - * the threshold value map. + * @param thresholds the threshold value map. */ - public void setThresholds(Map thresholds) { - for (Iterator iter = thresholds.entrySet().iterator(); iter.hasNext();) { - Map.Entry entry = (Map.Entry) iter.next(); + public void setThresholds(Map thresholds) { + for (Entry entry : thresholds.entrySet()) { + if (!(entry.getKey() instanceof String)) { - logger.warn("Key in thresholds map is not of type String: " - + entry.getKey()); + logger.warn("Key in thresholds map is not of type String: " + entry.getKey()); } - Assert - .state( - entry.getValue() instanceof Integer, - "Threshold value must be of type Integer. " - + "Try using the value-type attribute if you care configuring this map via xml."); + Assert.state(entry.getValue() instanceof Integer, "Threshold value must be of type Integer. " + + "Try using the value-type attribute if you care configuring this map via xml."); } this.thresholds = thresholds; } @@ -114,14 +107,13 @@ public class RethrowOnThresholdExceptionHandler implements ExceptionHandler { * @throws Throwable * @see ExceptionHandler#handleException(RepeatContext, Throwable) */ - public void handleException(RepeatContext context, Throwable throwable) - throws Throwable { + public void handleException(RepeatContext context, Throwable throwable) throws Throwable { Object key = exceptionClassifier.classify(throwable); RepeatContextCounter counter = getCounter(context, key); counter.increment(); int count = counter.getCount(); - Integer threshold = (Integer) thresholds.get(key); + Integer threshold = thresholds.get(key); if (threshold == null || count > threshold.intValue()) { throw throwable; } @@ -129,8 +121,7 @@ public class RethrowOnThresholdExceptionHandler implements ExceptionHandler { } private RepeatContextCounter getCounter(RepeatContext context, Object key) { - String attribute = RethrowOnThresholdExceptionHandler.class.getName() + "." - + key.toString(); + String attribute = RethrowOnThresholdExceptionHandler.class.getName() + "." + key.toString(); // Creates a new counter and stores it in the correct context: return new RepeatContextCounter(context, attribute, useParent); } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/exception/SimpleLimitExceptionHandler.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/exception/SimpleLimitExceptionHandler.java index 8a297f991..f0663872b 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/exception/SimpleLimitExceptionHandler.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/exception/SimpleLimitExceptionHandler.java @@ -47,9 +47,9 @@ public class SimpleLimitExceptionHandler implements ExceptionHandler { private RethrowOnThresholdExceptionHandler delegate = new RethrowOnThresholdExceptionHandler(); - private Class[] exceptionClasses = new Class[] { Exception.class }; + private Class[] exceptionClasses = new Class[] { Exception.class }; - private Class[] fatalExceptionClasses = new Class[] { Error.class }; + private Class[] fatalExceptionClasses = new Class[] { Error.class }; /** * Flag to indicate the the exception counters should be shared between @@ -116,7 +116,7 @@ public class SimpleLimitExceptionHandler implements ExceptionHandler { * @param limit */ public void setLimit(final int limit) { - delegate.setThresholds(new HashMap() { + delegate.setThresholds(new HashMap() { { put(ExceptionClassifierSupport.DEFAULT, new Integer(0)); put(TX_INVALID, new Integer(limit)); @@ -131,7 +131,7 @@ public class SimpleLimitExceptionHandler implements ExceptionHandler { * handler uses single counter that is incremented when one of the * recognized exception exceptionClasses is handled. */ - public void setExceptionClasses(Class[] classes) { + public void setExceptionClasses(Class[] classes) { this.exceptionClasses = classes; } @@ -142,7 +142,7 @@ public class SimpleLimitExceptionHandler implements ExceptionHandler { * * @param fatalExceptionClasses defaults to {@link Error} */ - public void setFatalExceptionClasses(Class[] fatalExceptionClasses) { + public void setFatalExceptionClasses(Class[] fatalExceptionClasses) { this.fatalExceptionClasses = fatalExceptionClasses; } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/listener/CompositeRepeatListener.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/listener/CompositeRepeatListener.java index 29e087b3e..ddc49e3a2 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/listener/CompositeRepeatListener.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/listener/CompositeRepeatListener.java @@ -17,7 +17,6 @@ package org.springframework.batch.repeat.listener; import java.util.ArrayList; import java.util.Arrays; -import java.util.Iterator; import java.util.List; import org.springframework.batch.repeat.ExitStatus; @@ -30,7 +29,7 @@ import org.springframework.batch.repeat.RepeatListener; */ public class CompositeRepeatListener implements RepeatListener { - private List listeners = new ArrayList(); + private List listeners = new ArrayList(); /** * Public setter for the listeners. @@ -56,8 +55,7 @@ public class CompositeRepeatListener implements RepeatListener { * @see org.springframework.batch.repeat.RepeatListener#after(org.springframework.batch.repeat.RepeatContext, org.springframework.batch.repeat.ExitStatus) */ public void after(RepeatContext context, ExitStatus result) { - for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { - RepeatListener listener = (RepeatListener) iterator.next(); + for (RepeatListener listener : listeners) { listener.after(context, result); } } @@ -66,8 +64,7 @@ public class CompositeRepeatListener implements RepeatListener { * @see org.springframework.batch.repeat.RepeatListener#before(org.springframework.batch.repeat.RepeatContext) */ public void before(RepeatContext context) { - for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { - RepeatListener listener = (RepeatListener) iterator.next(); + for (RepeatListener listener : listeners) { listener.before(context); } } @@ -76,8 +73,7 @@ public class CompositeRepeatListener implements RepeatListener { * @see org.springframework.batch.repeat.RepeatListener#close(org.springframework.batch.repeat.RepeatContext) */ public void close(RepeatContext context) { - for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { - RepeatListener listener = (RepeatListener) iterator.next(); + for (RepeatListener listener : listeners) { listener.close(context); } } @@ -86,8 +82,7 @@ public class CompositeRepeatListener implements RepeatListener { * @see org.springframework.batch.repeat.RepeatListener#onError(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable) */ public void onError(RepeatContext context, Throwable e) { - for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { - RepeatListener listener = (RepeatListener) iterator.next(); + for (RepeatListener listener : listeners) { listener.onError(context, e); } } @@ -96,8 +91,7 @@ public class CompositeRepeatListener implements RepeatListener { * @see org.springframework.batch.repeat.RepeatListener#open(org.springframework.batch.repeat.RepeatContext) */ public void open(RepeatContext context) { - for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { - RepeatListener listener = (RepeatListener) iterator.next(); + for (RepeatListener listener : listeners) { listener.open(context); } } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/policy/CompositeCompletionPolicy.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/policy/CompositeCompletionPolicy.java index 78508ee1c..87f1b3ca0 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/policy/CompositeCompletionPolicy.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/repeat/policy/CompositeCompletionPolicy.java @@ -83,7 +83,7 @@ public class CompositeCompletionPolicy implements CompletionPolicy { * @see org.springframework.batch.repeat.CompletionPolicy#start(RepeatContext) */ public RepeatContext start(RepeatContext context) { - List list = new ArrayList(); + List list = new ArrayList(); for (int i = 0; i < policies.length; i++) { list.add(policies[i].start(context)); } @@ -121,9 +121,9 @@ public class CompositeCompletionPolicy implements CompletionPolicy { // change). private CompletionPolicy[] policies; - public CompositeBatchContext(RepeatContext context, List contexts) { + public CompositeBatchContext(RepeatContext context, List contexts) { super(context); - this.contexts = (RepeatContext[]) contexts.toArray(new RepeatContext[contexts.size()]); + this.contexts = contexts.toArray(new RepeatContext[contexts.size()]); this.policies = CompositeCompletionPolicy.this.policies; } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/repeat/exception/RethrowOnThresholdExceptionHandlerTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/repeat/exception/RethrowOnThresholdExceptionHandlerTests.java index 646d58074..115836063 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/repeat/exception/RethrowOnThresholdExceptionHandlerTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/repeat/exception/RethrowOnThresholdExceptionHandlerTests.java @@ -17,6 +17,7 @@ package org.springframework.batch.repeat.exception; import java.util.Collections; +import java.util.Map; import junit.framework.TestCase; @@ -55,7 +56,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase { return "RuntimeException"; } }); - handler.setThresholds(Collections.singletonMap("RuntimeException", new Integer(1))); + handler.setThresholds(Collections.singletonMap((Object)"RuntimeException", new Integer(1))); // No exception... handler.handleException(context, new RuntimeException("Foo")); RepeatContextCounter counter = new RepeatContextCounter(context, RethrowOnThresholdExceptionHandler.class.getName() + ".RuntimeException"); @@ -69,7 +70,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase { return "RuntimeException"; } }); - handler.setThresholds(Collections.singletonMap("RuntimeException", new Integer(2))); + handler.setThresholds(Collections.singletonMap((Object)"RuntimeException", new Integer(2))); // No exception... handler.handleException(context, new RuntimeException("Foo")); handler.handleException(context, new RuntimeException("Foo")); @@ -81,16 +82,6 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase { assertEquals("Foo", e.getMessage()); } } - - public void testNonIntegerAsThreshold() throws Exception { - try { - handler.setThresholds(Collections.singletonMap("RuntimeException", new Long(1))); - fail("Expected IllegalStateException"); - } - catch (IllegalStateException e) { - // expected - } - } public void testNotUseParent() throws Throwable { handler.setExceptionClassifier(new ExceptionClassifierSupport() { @@ -98,7 +89,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase { return "RuntimeException"; } }); - handler.setThresholds(Collections.singletonMap("RuntimeException", new Integer(1))); + handler.setThresholds(Collections.singletonMap((Object)"RuntimeException", new Integer(1))); // No exception... handler.handleException(context, new RuntimeException("Foo")); context = new RepeatContextSupport(parent); @@ -117,7 +108,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase { return "RuntimeException"; } }); - handler.setThresholds(Collections.singletonMap("RuntimeException", new Integer(1))); + handler.setThresholds(Collections.singletonMap((Object)"RuntimeException", new Integer(1))); handler.setUseParent(true); // No exception... handler.handleException(context, new RuntimeException("Foo")); @@ -133,7 +124,7 @@ public class RethrowOnThresholdExceptionHandlerTests extends TestCase { public void testNotStringAsKey() throws Exception { try { - handler.setThresholds(Collections.singletonMap(RuntimeException.class, new Integer(1))); + handler.setThresholds(Collections.singletonMap((Object)RuntimeException.class, new Integer(1))); // It's not an error, but not advised... } catch (RuntimeException e) {