From 45ec739c3da7f014f4f11e2f7ad4eb9843cb323e Mon Sep 17 00:00:00 2001 From: robokaso Date: Tue, 15 Jul 2008 13:43:42 +0000 Subject: [PATCH] IN PROGRESS - BATCH-709: Change all collections to use generics --- .../ExceptionClassifierRetryPolicy.java | 15 ++++++------ .../retry/policy/MapRetryContextCache.java | 8 +++---- .../batch/retry/policy/SimpleRetryPolicy.java | 4 ++-- .../support/RetrySynchronizationManager.java | 2 +- .../batch/retry/support/RetryTemplate.java | 4 ++-- .../support/BinaryExceptionClassifier.java | 4 ++-- .../DefaultPropertyEditorRegistrar.java | 24 ++++++++----------- .../ExceptionClassifierRetryPolicyTests.java | 15 +++++++----- .../DefaultPropertEditorRegistrarTests.java | 11 --------- 9 files changed, 37 insertions(+), 50 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/ExceptionClassifierRetryPolicy.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/ExceptionClassifierRetryPolicy.java index 4b2510ea5..704f2a2ad 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/ExceptionClassifierRetryPolicy.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/ExceptionClassifierRetryPolicy.java @@ -17,7 +17,6 @@ package org.springframework.batch.retry.policy; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; import org.springframework.batch.retry.RetryCallback; @@ -40,7 +39,7 @@ public class ExceptionClassifierRetryPolicy extends AbstractStatelessRetryPolicy private ExceptionClassifier exceptionClassifier = new ExceptionClassifierSupport(); - private Map policyMap = new HashMap(); + private Map policyMap = new HashMap(); public ExceptionClassifierRetryPolicy() { policyMap.put(ExceptionClassifierSupport.DEFAULT, new NeverRetryPolicy()); @@ -55,7 +54,7 @@ public class ExceptionClassifierRetryPolicy extends AbstractStatelessRetryPolicy * applied to the result of the {@link ExceptionClassifier} to locate a * policy. */ - public void setPolicyMap(Map policyMap) { + public void setPolicyMap(Map policyMap) { this.policyMap = policyMap; } @@ -93,7 +92,8 @@ public class ExceptionClassifierRetryPolicy extends AbstractStatelessRetryPolicy * Create an active context that proxies a retry policy by chosing a target * from the policy map. * - * @see org.springframework.batch.retry.RetryPolicy#open(org.springframework.batch.retry.RetryCallback, RetryContext) + * @see org.springframework.batch.retry.RetryPolicy#open(org.springframework.batch.retry.RetryCallback, + * RetryContext) */ public RetryContext open(RetryCallback callback, RetryContext parent) { return new ExceptionClassifierRetryContext(parent, exceptionClassifier).open(callback, parent); @@ -124,7 +124,7 @@ public class ExceptionClassifierRetryPolicy extends AbstractStatelessRetryPolicy // The same for the life of the context: RetryCallback callback; - Map contexts = new HashMap(); + Map contexts = new HashMap(); public ExceptionClassifierRetryContext(RetryContext parent, ExceptionClassifier exceptionClassifier) { super(parent); @@ -135,7 +135,7 @@ public class ExceptionClassifierRetryPolicy extends AbstractStatelessRetryPolicy } public boolean canRetry(RetryContext context) { - if (this.context==null) { + if (this.context == null) { // there was no error yet return true; } @@ -148,8 +148,7 @@ public class ExceptionClassifierRetryPolicy extends AbstractStatelessRetryPolicy public void close(RetryContext context) { // Only close those policies that have been used (opened): - for (Iterator iter = contexts.keySet().iterator(); iter.hasNext();) { - RetryPolicy policy = (RetryPolicy) iter.next(); + for (RetryPolicy policy : contexts.keySet()) { policy.close(getContext(policy)); } } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/MapRetryContextCache.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/MapRetryContextCache.java index e4c039b98..9df77de80 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/MapRetryContextCache.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/MapRetryContextCache.java @@ -38,7 +38,7 @@ public class MapRetryContextCache implements RetryContextCache { */ public static final int DEFAULT_CAPACITY = 4096; - private Map map = Collections.synchronizedMap(new HashMap()); + private Map map = Collections.synchronizedMap(new HashMap()); private int capacity; @@ -80,9 +80,9 @@ public class MapRetryContextCache implements RetryContextCache { public void put(Object key, RetryContext context) { if (map.size() >= capacity) { - throw new RetryCacheCapacityExceededException("Retry cache capacity limit breached. " + - "Do you need to re-consider the implementation of the key generator, " + - "or the equals and hashCode of the items that failed?"); + throw new RetryCacheCapacityExceededException("Retry cache capacity limit breached. " + + "Do you need to re-consider the implementation of the key generator, " + + "or the equals and hashCode of the items that failed?"); } map.put(key, context); } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/SimpleRetryPolicy.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/SimpleRetryPolicy.java index 27a66ba12..703033f53 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/SimpleRetryPolicy.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/policy/SimpleRetryPolicy.java @@ -103,7 +103,7 @@ public class SimpleRetryPolicy extends AbstractStatelessRetryPolicy { * * @param retryableExceptionClasses defaults to {@link Exception}. */ - public final void setRetryableExceptionClasses(Class[] retryableExceptionClasses) { + public final void setRetryableExceptionClasses(Class[] retryableExceptionClasses) { retryableClassifier.setExceptionClasses(retryableExceptionClasses); } @@ -114,7 +114,7 @@ public class SimpleRetryPolicy extends AbstractStatelessRetryPolicy { * * @param fatalExceptionClasses defaults to {@link Exception}. */ - public final void setFatalExceptionClasses(Class[] fatalExceptionClasses) { + public final void setFatalExceptionClasses(Class[] fatalExceptionClasses) { fatalClassifier.setExceptionClasses(fatalExceptionClasses); } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/support/RetrySynchronizationManager.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/support/RetrySynchronizationManager.java index b1fa95642..268801959 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/support/RetrySynchronizationManager.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/support/RetrySynchronizationManager.java @@ -38,7 +38,7 @@ public class RetrySynchronizationManager { private RetrySynchronizationManager() {} - private static final ThreadLocal context = new ThreadLocal(); + private static final ThreadLocal context = new ThreadLocal(); /** * Public accessor for the locally enclosing {@link RetryContext}. diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/support/RetryTemplate.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/support/RetryTemplate.java index 8107378c8..27120fc4e 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/support/RetryTemplate.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/retry/support/RetryTemplate.java @@ -88,9 +88,9 @@ public class RetryTemplate implements RetryOperations { * @see #setListeners(RetryListener[]) */ public void registerListener(RetryListener listener) { - List list = new ArrayList(Arrays.asList(listeners)); + List list = new ArrayList(Arrays.asList(listeners)); list.add(listener); - listeners = (RetryListener[]) list.toArray(new RetryListener[list.size()]); + listeners = list.toArray(new RetryListener[list.size()]); } /** diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/BinaryExceptionClassifier.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/BinaryExceptionClassifier.java index 601b67458..1962a32ce 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/BinaryExceptionClassifier.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/BinaryExceptionClassifier.java @@ -41,8 +41,8 @@ public class BinaryExceptionClassifier extends ExceptionClassifierSupport { * * @param exceptionClasses defaults to {@link Exception}. */ - public final void setExceptionClasses(Class[] exceptionClasses) { - Map temp = new HashMap(); + public final void setExceptionClasses(Class[] exceptionClasses) { + Map, Object> temp = new HashMap, Object>(); for (int i = 0; i < exceptionClasses.length; i++) { temp.put(exceptionClasses[i], NON_DEFAULT); } diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DefaultPropertyEditorRegistrar.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DefaultPropertyEditorRegistrar.java index d3b478183..5adf928fd 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DefaultPropertyEditorRegistrar.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/support/DefaultPropertyEditorRegistrar.java @@ -17,8 +17,8 @@ package org.springframework.batch.support; import java.beans.PropertyEditor; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; +import java.util.Map.Entry; import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.PropertyEditorRegistry; @@ -35,7 +35,7 @@ import org.springframework.util.ClassUtils; */ public class DefaultPropertyEditorRegistrar implements PropertyEditorRegistrar { - private Map customEditors; + private Map, PropertyEditor> customEditors; /** * Register the custom editors with the given registry. @@ -44,11 +44,8 @@ public class DefaultPropertyEditorRegistrar implements PropertyEditorRegistrar { */ public void registerCustomEditors(PropertyEditorRegistry registry) { if (this.customEditors != null) { - for (Iterator it = customEditors.entrySet().iterator(); it.hasNext();) { - Map.Entry entry = (Map.Entry) it.next(); - Class key = (Class) entry.getKey(); - PropertyEditor value = (PropertyEditor) entry.getValue(); - registry.registerCustomEditor(key, value); + for (Entry, PropertyEditor> entry : customEditors.entrySet()) { + registry.registerCustomEditor(entry.getKey(), entry.getValue()); } } } @@ -61,14 +58,13 @@ public class DefaultPropertyEditorRegistrar implements PropertyEditorRegistrar { * PropertyEditor). * @see CustomEditorConfigurer#setCustomEditors(Map) */ - public void setCustomEditors(Map customEditors) { - this.customEditors = new HashMap(); - for (Iterator it = customEditors.entrySet().iterator(); it.hasNext();) { - Map.Entry entry = (Map.Entry) it.next(); + public void setCustomEditors(Map customEditors) { + this.customEditors = new HashMap, PropertyEditor>(); + for (Entry entry : customEditors.entrySet()) { Object key = entry.getKey(); - Class requiredType = null; + Class requiredType = null; if (key instanceof Class) { - requiredType = (Class) key; + requiredType = (Class) key; } else if (key instanceof String) { String className = (String) key; @@ -83,7 +79,7 @@ public class DefaultPropertyEditorRegistrar implements PropertyEditorRegistrar { throw new IllegalArgumentException("Mapped value [" + value + "] for custom editor key [" + key + "] is not of required type [" + PropertyEditor.class.getName() + "]"); } - this.customEditors.put(requiredType, value); + this.customEditors.put(requiredType, (PropertyEditor) value); } } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/policy/ExceptionClassifierRetryPolicyTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/policy/ExceptionClassifierRetryPolicyTests.java index 67753c6b9..a626d3966 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/policy/ExceptionClassifierRetryPolicyTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/retry/policy/ExceptionClassifierRetryPolicyTests.java @@ -23,6 +23,7 @@ import java.util.Map; import junit.framework.TestCase; import org.springframework.batch.retry.RetryContext; +import org.springframework.batch.retry.RetryPolicy; import org.springframework.batch.support.ExceptionClassifierSupport; public class ExceptionClassifierRetryPolicyTests extends TestCase { @@ -35,7 +36,8 @@ public class ExceptionClassifierRetryPolicyTests extends TestCase { } public void testTrivialPolicies() throws Exception { - policy.setPolicyMap(Collections.singletonMap(ExceptionClassifierSupport.DEFAULT, new MockRetryPolicySupport())); + policy.setPolicyMap(Collections.singletonMap(ExceptionClassifierSupport.DEFAULT, + (RetryPolicy) new MockRetryPolicySupport())); RetryContext context = policy.open(null, null); assertNotNull(context); assertTrue(policy.canRetry(context)); @@ -102,11 +104,12 @@ public class ExceptionClassifierRetryPolicyTests extends TestCase { int count = 0; public void testClose() throws Exception { - policy.setPolicyMap(Collections.singletonMap(ExceptionClassifierSupport.DEFAULT, new MockRetryPolicySupport() { - public void close(RetryContext context) { - count++; - } - })); + policy.setPolicyMap(Collections.singletonMap(ExceptionClassifierSupport.DEFAULT, + (RetryPolicy) new MockRetryPolicySupport() { + public void close(RetryContext context) { + count++; + } + })); RetryContext context = policy.open(null, null); // The mapped (child) policy hasn't been used yet, so if we close now diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/DefaultPropertEditorRegistrarTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/DefaultPropertEditorRegistrarTests.java index 89238be01..97cb08f65 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/DefaultPropertEditorRegistrarTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/support/DefaultPropertEditorRegistrarTests.java @@ -60,17 +60,6 @@ public class DefaultPropertEditorRegistrarTests extends TestCase { } } - - public void testSetCustomEditorsWithInvalidEditor() throws Exception { - - DefaultPropertyEditorRegistrar mapper = new DefaultPropertyEditorRegistrar(); - try { - mapper.setCustomEditors(Collections.singletonMap(Long.class, "FOO")); - } catch (IllegalArgumentException e) { - // expected - } - } - private static class BeanWithIntArray { private int[] numbers;