Java 5 code style

This commit is contained in:
Juergen Hoeller
2008-11-27 00:27:52 +00:00
parent 6bbc966a21
commit b0790bf5e7
248 changed files with 2374 additions and 3208 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@ package org.springframework.core;
import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import org.springframework.util.Assert;
@@ -36,7 +35,7 @@ import org.springframework.util.Assert;
public abstract class AttributeAccessorSupport implements AttributeAccessor, Serializable {
/** Map with String keys and Object values */
private final Map attributes = new LinkedHashMap();
private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
public void setAttribute(String name, Object value) {
@@ -65,8 +64,7 @@ public abstract class AttributeAccessorSupport implements AttributeAccessor, Ser
}
public String[] attributeNames() {
Set attributeNames = this.attributes.keySet();
return (String[]) attributeNames.toArray(new String[attributeNames.size()]);
return this.attributes.keySet().toArray(new String[this.attributes.size()]);
}
@@ -77,8 +75,7 @@ public abstract class AttributeAccessorSupport implements AttributeAccessor, Ser
protected void copyAttributesFrom(AttributeAccessor source) {
Assert.notNull(source, "Source must not be null");
String[] attributeNames = source.attributeNames();
for (int i = 0; i < attributeNames.length; i++) {
String attributeName = attributeNames[i];
for (String attributeName : attributeNames) {
setAttribute(attributeName, source.getAttribute(attributeName));
}
}

View File

@@ -64,10 +64,9 @@ public abstract class BridgeMethodResolver {
}
// Gather all methods with matching name and parameter size.
List candidateMethods = new ArrayList();
List<Method> candidateMethods = new ArrayList<Method>();
Method[] methods = ReflectionUtils.getAllDeclaredMethods(bridgeMethod.getDeclaringClass());
for (int i = 0; i < methods.length; i++) {
Method candidateMethod = methods[i];
for (Method candidateMethod : methods) {
if (isBridgedCandidateFor(candidateMethod, bridgeMethod)) {
candidateMethods.add(candidateMethod);
}
@@ -76,7 +75,7 @@ public abstract class BridgeMethodResolver {
Method result;
// Now perform simple quick checks.
if (candidateMethods.size() == 1) {
result = (Method) candidateMethods.get(0);
result = candidateMethods.get(0);
}
else {
result = searchCandidates(candidateMethods, bridgeMethod);
@@ -86,7 +85,6 @@ public abstract class BridgeMethodResolver {
throw new IllegalStateException(
"Unable to locate bridged method for bridge method '" + bridgeMethod + "'");
}
return result;
}
@@ -96,10 +94,9 @@ public abstract class BridgeMethodResolver {
* @param bridgeMethod the bridge method
* @return the bridged method, or <code>null</code> if none found
*/
private static Method searchCandidates(List candidateMethods, Method bridgeMethod) {
Map typeParameterMap = GenericTypeResolver.getTypeVariableMap(bridgeMethod.getDeclaringClass());
for (int i = 0; i < candidateMethods.size(); i++) {
Method candidateMethod = (Method) candidateMethods.get(i);
private static Method searchCandidates(List<Method> candidateMethods, Method bridgeMethod) {
Map<TypeVariable, Type> typeParameterMap = GenericTypeResolver.getTypeVariableMap(bridgeMethod.getDeclaringClass());
for (Method candidateMethod : candidateMethods) {
if (isBridgeMethodFor(bridgeMethod, candidateMethod, typeParameterMap)) {
return candidateMethod;
}
@@ -123,7 +120,7 @@ public abstract class BridgeMethodResolver {
* Determines whether or not the bridge {@link Method} is the bridge for the
* supplied candidate {@link Method}.
*/
static boolean isBridgeMethodFor(Method bridgeMethod, Method candidateMethod, Map typeVariableMap) {
static boolean isBridgeMethodFor(Method bridgeMethod, Method candidateMethod, Map<TypeVariable, Type> typeVariableMap) {
if (isResolvedTypeMatch(candidateMethod, bridgeMethod, typeVariableMap)) {
return true;
}
@@ -149,9 +146,8 @@ public abstract class BridgeMethodResolver {
// Search interfaces.
Class[] interfaces = ClassUtils.getAllInterfacesForClass(bridgeMethod.getDeclaringClass());
for (int i = 0; i < interfaces.length; i++) {
Class anInterface = interfaces[i];
Method method = searchForMatch(anInterface, bridgeMethod);
for (Class ifc : interfaces) {
Method method = searchForMatch(ifc, bridgeMethod);
if (method != null && !method.isBridge()) {
return method;
}
@@ -166,7 +162,9 @@ public abstract class BridgeMethodResolver {
* are equal after resolving all {@link TypeVariable TypeVariables} using the supplied
* TypeVariable Map, otherwise returns <code>false</code>.
*/
private static boolean isResolvedTypeMatch(Method genericMethod, Method candidateMethod, Map typeVariableMap) {
private static boolean isResolvedTypeMatch(
Method genericMethod, Method candidateMethod, Map<TypeVariable, Type> typeVariableMap) {
Type[] genericParameters = genericMethod.getGenericParameterTypes();
Class[] candidateParameters = candidateMethod.getParameterTypes();
if (genericParameters.length != candidateParameters.length) {

View File

@@ -34,12 +34,8 @@ import java.util.Map;
* target type of values to be added to a collection or map
* (to be able to attempt type conversion if appropriate).
*
* <p>Only usable on Java 5. Use an appropriate {@link JdkVersion} check
* before calling this class, if a fallback for JDK 1.4 is desirable.
*
* @author Juergen Hoeller
* @since 2.0
* @see org.springframework.beans.BeanWrapper
*/
public abstract class GenericCollectionTypeResolver {
@@ -283,7 +279,7 @@ public abstract class GenericCollectionTypeResolver {
Type resolvedType = type;
if (type instanceof TypeVariable && methodParam != null && methodParam.typeVariableMap != null) {
Type mappedType = (Type) methodParam.typeVariableMap.get(type);
Type mappedType = methodParam.typeVariableMap.get((TypeVariable) type);
if (mappedType != null) {
resolvedType = mappedType;
}
@@ -322,7 +318,7 @@ public abstract class GenericCollectionTypeResolver {
int nextLevel = currentLevel + 1;
Integer currentTypeIndex = (methodParam != null ? methodParam.getTypeIndexForLevel(nextLevel) : null);
// Default is last parameter type: Collection element or Map value.
int indexToUse = (currentTypeIndex != null ? currentTypeIndex.intValue() : paramTypes.length - 1);
int indexToUse = (currentTypeIndex != null ? currentTypeIndex : paramTypes.length - 1);
Type paramType = paramTypes[indexToUse];
return extractType(methodParam, paramType, source, typeIndex, nestingLevel, nextLevel);
}
@@ -339,7 +335,7 @@ public abstract class GenericCollectionTypeResolver {
}
Type paramType = paramTypes[typeIndex];
if (paramType instanceof TypeVariable && methodParam != null && methodParam.typeVariableMap != null) {
Type mappedType = (Type) methodParam.typeVariableMap.get(paramType);
Type mappedType = methodParam.typeVariableMap.get((TypeVariable) paramType);
if (mappedType != null) {
paramType = mappedType;
}
@@ -399,8 +395,7 @@ public abstract class GenericCollectionTypeResolver {
}
Type[] ifcs = clazz.getGenericInterfaces();
if (ifcs != null) {
for (int i = 0; i < ifcs.length; i++) {
Type ifc = ifcs[i];
for (Type ifc : ifcs) {
Type rawType = ifc;
if (ifc instanceof ParameterizedType) {
rawType = ((ParameterizedType) ifc).getRawType();

View File

@@ -34,9 +34,6 @@ import org.springframework.util.Assert;
* <p>Mainly intended for usage within the framework, resolving method
* parameter types even when they are declared generically.
*
* <p>Only usable on Java 5. Use an appropriate JdkVersion check before
* calling this class, if a fallback for JDK 1.4 is desirable.
*
* @author Juergen Hoeller
* @author Rob Harrop
* @since 2.5.2
@@ -46,7 +43,8 @@ import org.springframework.util.Assert;
public abstract class GenericTypeResolver {
/** Cache from Class to TypeVariable Map */
private static final Map typeVariableCache = Collections.synchronizedMap(new WeakHashMap());
private static final Map<Class, Map<TypeVariable, Type>> typeVariableCache =
Collections.synchronizedMap(new WeakHashMap<Class, Map<TypeVariable, Type>>());
/**
@@ -78,7 +76,7 @@ public abstract class GenericTypeResolver {
public static Class resolveParameterType(MethodParameter methodParam, Class clazz) {
Type genericType = getTargetType(methodParam);
Assert.notNull(clazz, "Class must not be null");
Map typeVariableMap = getTypeVariableMap(clazz);
Map<TypeVariable, Type> typeVariableMap = getTypeVariableMap(clazz);
Type rawType = getRawType(genericType, typeVariableMap);
Class result = (rawType instanceof Class ? (Class) rawType : methodParam.getParameterType());
methodParam.setParameterType(result);
@@ -96,7 +94,7 @@ public abstract class GenericTypeResolver {
Assert.notNull(method, "Method must not be null");
Type genericType = method.getGenericReturnType();
Assert.notNull(clazz, "Class must not be null");
Map typeVariableMap = getTypeVariableMap(clazz);
Map<TypeVariable, Type> typeVariableMap = getTypeVariableMap(clazz);
Type rawType = getRawType(genericType, typeVariableMap);
return (rawType instanceof Class ? (Class) rawType : method.getReturnType());
}
@@ -108,7 +106,7 @@ public abstract class GenericTypeResolver {
* @param typeVariableMap the TypeVariable Map to resolved against
* @return the type if it resolves to a Class, or <code>Object.class</code> otherwise
*/
static Class resolveType(Type genericType, Map typeVariableMap) {
static Class resolveType(Type genericType, Map<TypeVariable, Type> typeVariableMap) {
Type rawType = getRawType(genericType, typeVariableMap);
return (rawType instanceof Class ? (Class) rawType : Object.class);
}
@@ -119,11 +117,11 @@ public abstract class GenericTypeResolver {
* @param typeVariableMap the TypeVariable Map to resolved against
* @return the resolved raw type
*/
static Type getRawType(Type genericType, Map typeVariableMap) {
static Type getRawType(Type genericType, Map<TypeVariable, Type> typeVariableMap) {
Type resolvedType = genericType;
if (genericType instanceof TypeVariable) {
TypeVariable tv = (TypeVariable) genericType;
resolvedType = (Type) typeVariableMap.get(tv);
resolvedType = typeVariableMap.get(tv);
if (resolvedType == null) {
resolvedType = extractBoundForTypeVariable(tv);
}
@@ -141,11 +139,11 @@ public abstract class GenericTypeResolver {
* {@link Class} for the specified {@link Class}. Searches all super types,
* enclosing types and interfaces.
*/
static Map getTypeVariableMap(Class clazz) {
Map typeVariableMap = (Map) typeVariableCache.get(clazz);
static Map<TypeVariable, Type> getTypeVariableMap(Class clazz) {
Map<TypeVariable, Type> typeVariableMap = typeVariableCache.get(clazz);
if (typeVariableMap == null) {
typeVariableMap = new HashMap();
typeVariableMap = new HashMap<TypeVariable, Type>();
// interfaces
extractTypeVariablesFromGenericInterfaces(clazz.getGenericInterfaces(), typeVariableMap);
@@ -195,9 +193,8 @@ public abstract class GenericTypeResolver {
return bound;
}
private static void extractTypeVariablesFromGenericInterfaces(Type[] genericInterfaces, Map typeVariableMap) {
for (int i = 0; i < genericInterfaces.length; i++) {
Type genericInterface = genericInterfaces[i];
private static void extractTypeVariablesFromGenericInterfaces(Type[] genericInterfaces, Map<TypeVariable, Type> typeVariableMap) {
for (Type genericInterface : genericInterfaces) {
if (genericInterface instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) genericInterface;
populateTypeMapFromParameterizedType(pt, typeVariableMap);
@@ -229,7 +226,7 @@ public abstract class GenericTypeResolver {
* For '<code>FooImpl</code>' the following mappings would be added to the {@link Map}:
* {S=java.lang.String, T=java.lang.Integer}.
*/
private static void populateTypeMapFromParameterizedType(ParameterizedType type, Map typeVariableMap) {
private static void populateTypeMapFromParameterizedType(ParameterizedType type, Map<TypeVariable, Type> typeVariableMap) {
if (type.getRawType() instanceof Class) {
Type[] actualTypeArguments = type.getActualTypeArguments();
TypeVariable[] typeVariables = ((Class) type.getRawType()).getTypeParameters();
@@ -249,7 +246,7 @@ public abstract class GenericTypeResolver {
// We have a type that is parameterized at instantiation time
// the nearest match on the bridge method will be the bounded type.
TypeVariable typeVariableArgument = (TypeVariable) actualTypeArgument;
Type resolvedType = (Type) typeVariableMap.get(typeVariableArgument);
Type resolvedType = typeVariableMap.get(typeVariableArgument);
if (resolvedType == null) {
resolvedType = extractBoundForTypeVariable(typeVariableArgument);
}

View File

@@ -19,6 +19,8 @@ package org.springframework.core;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
@@ -59,7 +61,7 @@ public class MethodParameter {
/** Map from Integer level to Integer type index */
private Map<Integer,Integer> typeIndexesPerLevel;
Map typeVariableMap;
Map<TypeVariable, Type> typeVariableMap;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.core.task;
import java.util.concurrent.Executor;
/**
* Simple task executor interface that abstracts the execution
* of a {@link Runnable}.
@@ -24,15 +26,16 @@ package org.springframework.core.task;
* such as: synchronous, asynchronous, using a thread pool, and more.
*
* <p>Equivalent to JDK 1.5's {@link java.util.concurrent.Executor}
* interface. Separate mainly for compatibility with JDK 1.4.
* Implementations can simply implement the JDK 1.5 <code>Executor</code>
* interface as well, as it defines the exact same method signature.
* interface; extending it now in Spring 3.0, so that clients may declare
* a dependency on an Executor and receive any TaskExecutor implementation.
* This interface remains separate from the standard Executor interface
* mainly for backwards compatibility with JDK 1.4 in Spring 2.x.
*
* @author Juergen Hoeller
* @since 2.0
* @see java.util.concurrent.Executor
*/
public interface TaskExecutor {
public interface TaskExecutor extends Executor {
/**
* Execute the given <code>task</code>.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
package org.springframework.core.task;
import org.springframework.core.NestedRuntimeException;
import java.util.concurrent.RejectedExecutionException;
/**
* Exception thrown when a {@link TaskExecutor} rejects to accept
@@ -27,7 +27,7 @@ import org.springframework.core.NestedRuntimeException;
* @see TaskExecutor#execute(Runnable)
* @see TaskTimeoutException
*/
public class TaskRejectedException extends NestedRuntimeException {
public class TaskRejectedException extends RejectedExecutionException {
/**
* Create a new <code>TaskRejectedException</code>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,21 +17,19 @@
package org.springframework.core.task.support;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;
import org.springframework.core.task.TaskExecutor;
import org.springframework.core.task.TaskRejectedException;
import org.springframework.util.Assert;
/**
* Adapter that exposes the {@link java.util.concurrent.Executor java.util.concurrent.Executor}
* Adapter that exposes the {@link java.util.concurrent.Executor}
* interface for any Spring {@link org.springframework.core.task.TaskExecutor}.
* Follows the JDK executor contract for exception handling.
*
* @author Juergen Hoeller
* @since 2.5
* @see java.util.concurrent.Executor
* @see org.springframework.core.task.TaskExecutor
* @deprecated as of Spring 3.0 since TaskExecutor itself implements the Executor interface now
*/
public class ConcurrentExecutorAdapter implements Executor {
@@ -49,12 +47,7 @@ public class ConcurrentExecutorAdapter implements Executor {
public void execute(Runnable command) {
try {
this.taskExecutor.execute(command);
}
catch (TaskRejectedException ex) {
throw new RejectedExecutionException(ex.getMessage(), ex);
}
this.taskExecutor.execute(command);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -51,10 +51,11 @@ public class WeakReferenceMonitor {
private static final Log logger = LogFactory.getLog(WeakReferenceMonitor.class);
// Queue receiving reachability events
private static final ReferenceQueue handleQueue = new ReferenceQueue();
private static final ReferenceQueue<Object> handleQueue = new ReferenceQueue<Object>();
// All tracked entries (WeakReference => ReleaseListener)
private static final Map trackedEntries = Collections.synchronizedMap(new HashMap());
private static final Map<Reference, ReleaseListener> trackedEntries =
Collections.synchronizedMap(new HashMap<Reference, ReleaseListener>());
// Thread polling handleQueue, lazy initialized
private static Thread monitoringThread = null;
@@ -73,7 +74,7 @@ public class WeakReferenceMonitor {
// Make weak reference to this handle, so we can say when
// handle is not used any more by polling on handleQueue.
WeakReference weakRef = new WeakReference(handle, handleQueue);
WeakReference<Object> weakRef = new WeakReference<Object>(handle, handleQueue);
// Add monitored entry to internal map of all monitored entries.
addEntry(weakRef, listener);
@@ -105,7 +106,7 @@ public class WeakReferenceMonitor {
* @return entry object associated with given reference
*/
private static ReleaseListener removeEntry(Reference reference) {
return (ReleaseListener) trackedEntries.remove(reference);
return trackedEntries.remove(reference);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2005 the original author or authors.
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@ import java.util.Comparator;
* @author Keith Donald
* @since 1.2.2
*/
public final class BooleanComparator implements Comparator, Serializable {
public final class BooleanComparator implements Comparator<Boolean>, Serializable {
/**
* A shared default instance of this comparator, treating true lower
@@ -58,9 +58,7 @@ public final class BooleanComparator implements Comparator, Serializable {
}
public int compare(Object o1, Object o2) {
boolean v1 = ((Boolean) o1).booleanValue();
boolean v2 = ((Boolean) o2).booleanValue();
public int compare(Boolean v1, Boolean v2) {
return (v1 ^ v2) ? ((v1 ^ this.trueLow) ? 1 : -1) : 0;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2005 the original author or authors.
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,12 +29,10 @@ import org.springframework.util.Assert;
* @since 1.2.2
* @see Comparable
*/
public class ComparableComparator implements Comparator {
public class ComparableComparator<T extends Comparable<T>> implements Comparator<T> {
public int compare(Object o1, Object o2) {
Assert.isTrue(o1 instanceof Comparable, "The first object provided is not Comparable");
Assert.isTrue(o2 instanceof Comparable, "The second object provided is not Comparable");
return ((Comparable) o1).compareTo(o2);
public int compare(T o1, T o2) {
return o1.compareTo(o2);
}
}

View File

@@ -37,9 +37,9 @@ import org.springframework.util.Assert;
* @author Juergen Hoeller
* @since 1.2.2
*/
public class CompoundComparator implements Comparator, Serializable {
public class CompoundComparator<T> implements Comparator<T>, Serializable {
private final List<InvertibleComparator> comparators;
private final List<InvertibleComparator<T>> comparators;
/**
@@ -48,7 +48,7 @@ public class CompoundComparator implements Comparator, Serializable {
* IllegalStateException is thrown.
*/
public CompoundComparator() {
this.comparators = new ArrayList<InvertibleComparator>();
this.comparators = new ArrayList<InvertibleComparator<T>>();
}
/**
@@ -59,8 +59,8 @@ public class CompoundComparator implements Comparator, Serializable {
* @see InvertibleComparator
*/
public CompoundComparator(Comparator[] comparators) {
this.comparators = new ArrayList<InvertibleComparator>(comparators.length);
for (Comparator comparator : comparators) {
this.comparators = new ArrayList<InvertibleComparator<T>>(comparators.length);
for (Comparator<T> comparator : comparators) {
addComparator(comparator);
}
}
@@ -73,12 +73,12 @@ public class CompoundComparator implements Comparator, Serializable {
* @param comparator the Comparator to add to the end of the chain
* @see InvertibleComparator
*/
public void addComparator(Comparator comparator) {
public void addComparator(Comparator<T> comparator) {
if (comparator instanceof InvertibleComparator) {
this.comparators.add((InvertibleComparator) comparator);
this.comparators.add((InvertibleComparator<T>) comparator);
}
else {
this.comparators.add(new InvertibleComparator(comparator));
this.comparators.add(new InvertibleComparator<T>(comparator));
}
}
@@ -87,8 +87,8 @@ public class CompoundComparator implements Comparator, Serializable {
* @param comparator the Comparator to add to the end of the chain
* @param ascending the sort order: ascending (true) or descending (false)
*/
public void addComparator(Comparator comparator, boolean ascending) {
this.comparators.add(new InvertibleComparator(comparator, ascending));
public void addComparator(Comparator<T> comparator, boolean ascending) {
this.comparators.add(new InvertibleComparator<T>(comparator, ascending));
}
/**
@@ -99,13 +99,12 @@ public class CompoundComparator implements Comparator, Serializable {
* @param comparator the Comparator to place at the given index
* @see InvertibleComparator
*/
public void setComparator(int index, Comparator comparator) {
public void setComparator(int index, Comparator<T> comparator) {
if (comparator instanceof InvertibleComparator) {
this.comparators.set(index, (InvertibleComparator) comparator);
this.comparators.set(index, (InvertibleComparator<T>) comparator);
}
else {
InvertibleComparator invComp = new InvertibleComparator(comparator);
this.comparators.set(index, invComp);
this.comparators.set(index, new InvertibleComparator<T>(comparator));
}
}
@@ -115,8 +114,8 @@ public class CompoundComparator implements Comparator, Serializable {
* @param comparator the Comparator to place at the given index
* @param ascending the sort order: ascending (true) or descending (false)
*/
public void setComparator(int index, Comparator comparator, boolean ascending) {
this.comparators.set(index, new InvertibleComparator(comparator, ascending));
public void setComparator(int index, Comparator<T> comparator, boolean ascending) {
this.comparators.set(index, new InvertibleComparator<T>(comparator, ascending));
}
/**
@@ -161,10 +160,10 @@ public class CompoundComparator implements Comparator, Serializable {
}
public int compare(Object o1, Object o2) {
public int compare(T o1, T o2) {
Assert.state(this.comparators.size() > 0,
"No sort definitions have been added to this CompoundComparator to compare");
for (InvertibleComparator comparator : this.comparators) {
for (InvertibleComparator<T> comparator : this.comparators) {
int result = comparator.compare(o1, o2);
if (result != 0) {
return result;

View File

@@ -28,9 +28,9 @@ import java.util.Comparator;
* @author Juergen Hoeller
* @since 1.2.2
*/
public class InvertibleComparator implements Comparator, Serializable {
public class InvertibleComparator<T> implements Comparator<T>, Serializable {
private final Comparator comparator;
private final Comparator<T> comparator;
private boolean ascending = true;
@@ -40,7 +40,7 @@ public class InvertibleComparator implements Comparator, Serializable {
* For the actual comparison, the specified Comparator will be used.
* @param comparator the comparator to decorate
*/
public InvertibleComparator(Comparator comparator) {
public InvertibleComparator(Comparator<T> comparator) {
this.comparator = comparator;
}
@@ -50,7 +50,7 @@ public class InvertibleComparator implements Comparator, Serializable {
* @param comparator the comparator to decorate
* @param ascending the sort order: ascending (true) or descending (false)
*/
public InvertibleComparator(Comparator comparator, boolean ascending) {
public InvertibleComparator(Comparator<T> comparator, boolean ascending) {
this.comparator = comparator;
setAscending(ascending);
}
@@ -67,7 +67,7 @@ public class InvertibleComparator implements Comparator, Serializable {
* Return the sort order: ascending (true) or descending (false).
*/
public boolean isAscending() {
return ascending;
return this.ascending;
}
/**
@@ -79,7 +79,7 @@ public class InvertibleComparator implements Comparator, Serializable {
}
public int compare(Object o1, Object o2) {
public int compare(T o1, T o2) {
int result = this.comparator.compare(o1, o2);
if (result != 0) {
// Invert the order if it is a reverse sort.

View File

@@ -29,7 +29,7 @@ import org.springframework.util.Assert;
* @since 1.2.2
* @see Comparable
*/
public class NullSafeComparator implements Comparator {
public class NullSafeComparator<T> implements Comparator<T> {
/**
* A shared default instance of this comparator, treating nulls lower
@@ -44,7 +44,7 @@ public class NullSafeComparator implements Comparator {
public static final NullSafeComparator NULLS_HIGH = new NullSafeComparator(false);
private final Comparator nonNullComparator;
private final Comparator<T> nonNullComparator;
private final boolean nullsLow;
@@ -63,8 +63,10 @@ public class NullSafeComparator implements Comparator {
* @see #NULLS_LOW
* @see #NULLS_HIGH
*/
@SuppressWarnings("unchecked")
private NullSafeComparator(boolean nullsLow) {
this(new ComparableComparator(), nullsLow);
this.nonNullComparator = new ComparableComparator();
this.nullsLow = nullsLow;
}
/**
@@ -76,14 +78,14 @@ public class NullSafeComparator implements Comparator {
* @param comparator the comparator to use when comparing two non-null objects
* @param nullsLow whether to treat nulls lower or higher than non-null objects
*/
public NullSafeComparator(Comparator comparator, boolean nullsLow) {
public NullSafeComparator(Comparator<T> comparator, boolean nullsLow) {
Assert.notNull(comparator, "The non-null comparator is required");
this.nonNullComparator = comparator;
this.nullsLow = nullsLow;
}
public int compare(Object o1, Object o2) {
public int compare(T o1, T o2) {
if (o1 == o2) {
return 0;
}