IN PROGRESS - BATCH-709: Change all collections to use generics

This commit is contained in:
robokaso
2008-07-15 13:43:42 +00:00
parent 827d5b0f66
commit 45ec739c3d
9 changed files with 37 additions and 50 deletions

View File

@@ -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<String, RetryPolicy> policyMap = new HashMap<String, RetryPolicy>();
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<String, RetryPolicy> 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<RetryPolicy, RetryContext> contexts = new HashMap<RetryPolicy, RetryContext>();
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));
}
}

View File

@@ -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<Object, RetryContext> map = Collections.synchronizedMap(new HashMap<Object, RetryContext>());
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);
}

View File

@@ -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);
}

View File

@@ -38,7 +38,7 @@ public class RetrySynchronizationManager {
private RetrySynchronizationManager() {}
private static final ThreadLocal context = new ThreadLocal();
private static final ThreadLocal<RetryContext> context = new ThreadLocal<RetryContext>();
/**
* Public accessor for the locally enclosing {@link RetryContext}.

View File

@@ -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<RetryListener> list = new ArrayList<RetryListener>(Arrays.asList(listeners));
list.add(listener);
listeners = (RetryListener[]) list.toArray(new RetryListener[list.size()]);
listeners = list.toArray(new RetryListener[list.size()]);
}
/**

View File

@@ -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<Class<?>, Object> temp = new HashMap<Class<?>, Object>();
for (int i = 0; i < exceptionClasses.length; i++) {
temp.put(exceptionClasses[i], NON_DEFAULT);
}

View File

@@ -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<Class<?>, 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<Class<?>, 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<? extends Object, ? extends PropertyEditor> customEditors) {
this.customEditors = new HashMap<Class<?>, PropertyEditor>();
for (Entry<? extends Object, ? extends PropertyEditor> 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);
}
}

View File

@@ -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

View File

@@ -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;