Java 5 code style

This commit is contained in:
Juergen Hoeller
2008-11-25 01:29:54 +00:00
parent 1f9e63af49
commit 29657105da
71 changed files with 726 additions and 877 deletions

View File

@@ -19,7 +19,6 @@ package org.springframework.core;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
@@ -50,7 +49,7 @@ public class Constants {
private final String className;
/** Map from String field name to object value */
private final Map fieldCache = new HashMap();
private final Map<String, Object> fieldCache = new HashMap<String, Object>();
/**
@@ -63,8 +62,7 @@ public class Constants {
Assert.notNull(clazz);
this.className = clazz.getName();
Field[] fields = clazz.getFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
for (Field field : fields) {
if (ReflectionUtils.isPublicStaticFinal(field)) {
String name = field.getName();
try {
@@ -97,7 +95,7 @@ public class Constants {
* Exposes the field cache to subclasses:
* a Map from String field name to object value.
*/
protected final Map getFieldCache() {
protected final Map<String, Object> getFieldCache() {
return this.fieldCache;
}
@@ -159,11 +157,10 @@ public class Constants {
* @param namePrefix prefix of the constant names to search (may be <code>null</code>)
* @return the set of constant names
*/
public Set getNames(String namePrefix) {
public Set<String> getNames(String namePrefix) {
String prefixToUse = (namePrefix != null ? namePrefix.trim().toUpperCase(Locale.ENGLISH) : "");
Set names = new HashSet();
for (Iterator it = this.fieldCache.keySet().iterator(); it.hasNext();) {
String code = (String) it.next();
Set<String> names = new HashSet<String>();
for (String code : this.fieldCache.keySet()) {
if (code.startsWith(prefixToUse)) {
names.add(code);
}
@@ -178,7 +175,7 @@ public class Constants {
* @return the set of values
* @see #propertyToConstantNamePrefix
*/
public Set getNamesForProperty(String propertyName) {
public Set<String> getNamesForProperty(String propertyName) {
return getNames(propertyToConstantNamePrefix(propertyName));
}
@@ -194,9 +191,8 @@ public class Constants {
*/
public Set getNamesForSuffix(String nameSuffix) {
String suffixToUse = (nameSuffix != null ? nameSuffix.trim().toUpperCase(Locale.ENGLISH) : "");
Set names = new HashSet();
for (Iterator it = this.fieldCache.keySet().iterator(); it.hasNext();) {
String code = (String) it.next();
Set<String> names = new HashSet<String>();
for (String code : this.fieldCache.keySet()) {
if (code.endsWith(suffixToUse)) {
names.add(code);
}
@@ -215,11 +211,10 @@ public class Constants {
* @param namePrefix prefix of the constant names to search (may be <code>null</code>)
* @return the set of values
*/
public Set getValues(String namePrefix) {
public Set<Object> getValues(String namePrefix) {
String prefixToUse = (namePrefix != null ? namePrefix.trim().toUpperCase(Locale.ENGLISH) : "");
Set values = new HashSet();
for (Iterator it = this.fieldCache.keySet().iterator(); it.hasNext();) {
String code = (String) it.next();
Set<Object> values = new HashSet<Object>();
for (String code : this.fieldCache.keySet()) {
if (code.startsWith(prefixToUse)) {
values.add(this.fieldCache.get(code));
}
@@ -234,7 +229,7 @@ public class Constants {
* @return the set of values
* @see #propertyToConstantNamePrefix
*/
public Set getValuesForProperty(String propertyName) {
public Set<Object> getValuesForProperty(String propertyName) {
return getValues(propertyToConstantNamePrefix(propertyName));
}
@@ -248,11 +243,10 @@ public class Constants {
* @param nameSuffix suffix of the constant names to search (may be <code>null</code>)
* @return the set of values
*/
public Set getValuesForSuffix(String nameSuffix) {
public Set<Object> getValuesForSuffix(String nameSuffix) {
String suffixToUse = (nameSuffix != null ? nameSuffix.trim().toUpperCase(Locale.ENGLISH) : "");
Set values = new HashSet();
for (Iterator it = this.fieldCache.keySet().iterator(); it.hasNext();) {
String code = (String) it.next();
Set<Object> values = new HashSet<Object>();
for (String code : this.fieldCache.keySet()) {
if (code.endsWith(suffixToUse)) {
values.add(this.fieldCache.get(code));
}
@@ -271,11 +265,9 @@ public class Constants {
*/
public String toCode(Object value, String namePrefix) throws ConstantException {
String prefixToUse = (namePrefix != null ? namePrefix.trim().toUpperCase(Locale.ENGLISH) : null);
for (Iterator it = this.fieldCache.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
if (key.startsWith(prefixToUse) && entry.getValue().equals(value)) {
return key;
for (Map.Entry<String, Object> entry : this.fieldCache.entrySet()) {
if (entry.getKey().startsWith(prefixToUse) && entry.getValue().equals(value)) {
return entry.getKey();
}
}
throw new ConstantException(this.className, prefixToUse, value);
@@ -304,11 +296,9 @@ public class Constants {
*/
public String toCodeForSuffix(Object value, String nameSuffix) throws ConstantException {
String suffixToUse = (nameSuffix != null ? nameSuffix.trim().toUpperCase(Locale.ENGLISH) : null);
for (Iterator it = this.fieldCache.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
if (key.endsWith(suffixToUse) && entry.getValue().equals(value)) {
return key;
for (Map.Entry<String, Object> entry : this.fieldCache.entrySet()) {
if (entry.getKey().endsWith(suffixToUse) && entry.getValue().equals(value)) {
return entry.getKey();
}
}
throw new ConstantException(this.className, suffixToUse, value);

View File

@@ -17,7 +17,6 @@
package org.springframework.core;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.springframework.util.Assert;
@@ -33,9 +32,9 @@ import org.springframework.util.Assert;
*/
public abstract class DecoratingClassLoader extends ClassLoader {
private final Set excludedPackages = new HashSet();
private final Set<String> excludedPackages = new HashSet<String>();
private final Set excludedClasses = new HashSet();
private final Set<String> excludedClasses = new HashSet<String>();
private final Object exclusionMonitor = new Object();
@@ -95,8 +94,7 @@ public abstract class DecoratingClassLoader extends ClassLoader {
if (this.excludedClasses.contains(className)) {
return true;
}
for (Iterator it = this.excludedPackages.iterator(); it.hasNext();) {
String packageName = (String) it.next();
for (String packageName : this.excludedPackages) {
if (className.startsWith(packageName)) {
return true;
}

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.
@@ -36,7 +36,8 @@ import java.util.List;
*/
public class PrioritizedParameterNameDiscoverer implements ParameterNameDiscoverer {
private final List parameterNameDiscoverers = new LinkedList();
private final List<ParameterNameDiscoverer> parameterNameDiscoverers =
new LinkedList<ParameterNameDiscoverer>();
/**
@@ -49,8 +50,7 @@ public class PrioritizedParameterNameDiscoverer implements ParameterNameDiscover
public String[] getParameterNames(Method method) {
for (Iterator it = this.parameterNameDiscoverers.iterator(); it.hasNext(); ) {
ParameterNameDiscoverer pnd = (ParameterNameDiscoverer) it.next();
for (ParameterNameDiscoverer pnd : this.parameterNameDiscoverers) {
String[] result = pnd.getParameterNames(method);
if (result != null) {
return result;
@@ -60,8 +60,7 @@ public class PrioritizedParameterNameDiscoverer implements ParameterNameDiscover
}
public String[] getParameterNames(Constructor ctor) {
for (Iterator it = this.parameterNameDiscoverers.iterator(); it.hasNext(); ) {
ParameterNameDiscoverer pnd = (ParameterNameDiscoverer) it.next();
for (ParameterNameDiscoverer pnd : this.parameterNameDiscoverers) {
String[] result = pnd.getParameterNames(ctor);
if (result != null) {
return result;

View File

@@ -18,7 +18,6 @@ package org.springframework.core.enums;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
@@ -46,48 +45,16 @@ public abstract class AbstractCachingLabeledEnumResolver implements LabeledEnumR
protected transient final Log logger = LogFactory.getLog(getClass());
private final CachingMapDecorator labeledEnumCache = new CachingMapDecorator(true) {
@Override
protected Object create(Object key) {
Class enumType = (Class) key;
Set typeEnums = findLabeledEnums(enumType);
if (typeEnums == null || typeEnums.isEmpty()) {
throw new IllegalArgumentException(
"Unsupported labeled enumeration type '" + key + "': " +
"make sure you've properly defined this enumeration! " +
"If it is static, are the class and its fields public/static/final?");
}
Map typeEnumMap = new HashMap(typeEnums.size());
for (Iterator it = typeEnums.iterator(); it.hasNext();) {
LabeledEnum labeledEnum = (LabeledEnum) it.next();
typeEnumMap.put(labeledEnum.getCode(), labeledEnum);
}
return Collections.unmodifiableMap(typeEnumMap);
}
@Override
protected boolean useWeakValue(Object key, Object value) {
Class enumType = (Class) key;
if (!ClassUtils.isCacheSafe(enumType, AbstractCachingLabeledEnumResolver.this.getClass().getClassLoader())) {
if (logger.isDebugEnabled()) {
logger.debug("Not strongly caching class [" + enumType.getName() + "] because it is not cache-safe");
}
return true;
}
else {
return false;
}
}
};
private final LabeledEnumCache labeledEnumCache = new LabeledEnumCache();
public Set getLabeledEnumSet(Class type) throws IllegalArgumentException {
return new TreeSet(getLabeledEnumMap(type).values());
public Set<LabeledEnum> getLabeledEnumSet(Class type) throws IllegalArgumentException {
return new TreeSet<LabeledEnum>(getLabeledEnumMap(type).values());
}
public Map getLabeledEnumMap(Class type) throws IllegalArgumentException {
public Map<Comparable, LabeledEnum> getLabeledEnumMap(Class type) throws IllegalArgumentException {
Assert.notNull(type, "No type specified");
return (Map) this.labeledEnumCache.get(type);
return this.labeledEnumCache.get(type);
}
public LabeledEnum getLabeledEnumByCode(Class type, Comparable code) throws IllegalArgumentException {
@@ -104,10 +71,8 @@ public abstract class AbstractCachingLabeledEnumResolver implements LabeledEnumR
}
public LabeledEnum getLabeledEnumByLabel(Class type, String label) throws IllegalArgumentException {
Map typeEnums = getLabeledEnumMap(type);
Iterator it = typeEnums.values().iterator();
while (it.hasNext()) {
LabeledEnum value = (LabeledEnum) it.next();
Map<Comparable, LabeledEnum> typeEnums = getLabeledEnumMap(type);
for (LabeledEnum value : typeEnums.values()) {
if (value.getLabel().equalsIgnoreCase(label)) {
return value;
}
@@ -126,6 +91,43 @@ public abstract class AbstractCachingLabeledEnumResolver implements LabeledEnumR
* @return the Set of LabeledEnum instances
* @see org.springframework.core.enums.LabeledEnum
*/
protected abstract Set findLabeledEnums(Class type);
protected abstract Set<LabeledEnum> findLabeledEnums(Class type);
private class LabeledEnumCache extends CachingMapDecorator<Class, Map<Comparable, LabeledEnum>> {
public LabeledEnumCache() {
super(true);
}
@Override
protected Map<Comparable, LabeledEnum> create(Class key) {
Set<LabeledEnum> typeEnums = findLabeledEnums(key);
if (typeEnums == null || typeEnums.isEmpty()) {
throw new IllegalArgumentException(
"Unsupported labeled enumeration type '" + key + "': " +
"make sure you've properly defined this enumeration! " +
"If it is static, are the class and its fields public/static/final?");
}
Map<Comparable, LabeledEnum> typeEnumMap = new HashMap<Comparable, LabeledEnum>(typeEnums.size());
for (LabeledEnum labeledEnum : typeEnums) {
typeEnumMap.put(labeledEnum.getCode(), labeledEnum);
}
return Collections.unmodifiableMap(typeEnumMap);
}
@Override
protected boolean useWeakValue(Class key, Map<Comparable, LabeledEnum> value) {
if (!ClassUtils.isCacheSafe(key, AbstractCachingLabeledEnumResolver.this.getClass().getClassLoader())) {
if (logger.isDebugEnabled()) {
logger.debug("Not strongly caching class [" + key.getName() + "] because it is not cache-safe");
}
return true;
}
else {
return false;
}
}
}
}

View File

@@ -51,17 +51,15 @@ public class StaticLabeledEnumResolver extends AbstractCachingLabeledEnumResolve
@Override
protected Set findLabeledEnums(Class type) {
Set typeEnums = new TreeSet();
Field[] fields = type.getFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
protected Set<LabeledEnum> findLabeledEnums(Class type) {
Set<LabeledEnum> typeEnums = new TreeSet<LabeledEnum>();
for (Field field : type.getFields()) {
if (Modifier.isStatic(field.getModifiers()) && Modifier.isPublic(field.getModifiers())) {
if (type.isAssignableFrom(field.getType())) {
try {
Object value = field.get(null);
Assert.isTrue(value instanceof LabeledEnum, "Field value must be a LabeledEnum instance");
typeEnums.add(value);
typeEnums.add((LabeledEnum) value);
}
catch (IllegalAccessException ex) {
logger.warn("Unable to access field value: " + field, ex);

View File

@@ -151,8 +151,8 @@ public abstract class PropertiesLoaderSupport {
}
if (this.localProperties != null) {
for (int i = 0; i < this.localProperties.length; i++) {
CollectionUtils.mergePropertiesIntoMap(this.localProperties[i], result);
for (Properties localProp : this.localProperties) {
CollectionUtils.mergePropertiesIntoMap(localProp, result);
}
}
@@ -172,8 +172,7 @@ public abstract class PropertiesLoaderSupport {
*/
protected void loadProperties(Properties props) throws IOException {
if (this.locations != null) {
for (int i = 0; i < this.locations.length; i++) {
Resource location = this.locations[i];
for (Resource location : this.locations) {
if (logger.isInfoEnabled()) {
logger.info("Loading properties file from " + location);
}

View File

@@ -130,7 +130,7 @@ public class DefaultValueStyler implements ValueStyler {
private String styleArray(Object[] array) {
StringBuilder result = new StringBuilder(array.length * 8 + 16);
result.append(ARRAY + "<" + ClassUtils.getShortName(array.getClass().getComponentType()) + ">[");
result.append(ARRAY + "<").append(ClassUtils.getShortName(array.getClass().getComponentType())).append(">[");
for (int i = 0; i < array.length - 1; i++) {
result.append(style(array[i]));
result.append(',').append(' ');

View File

@@ -22,7 +22,7 @@ import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
@@ -41,12 +41,12 @@ import java.util.WeakHashMap;
* @author Juergen Hoeller
* @since 1.2.2
*/
public class CachingMapDecorator implements Map, Serializable {
public class CachingMapDecorator<K, V> implements Map<K, V>, Serializable {
protected static Object NULL_VALUE = new Object();
private final Map targetMap;
private final Map<K, Object> targetMap;
private final boolean synchronize;
@@ -67,7 +67,7 @@ public class CachingMapDecorator implements Map, Serializable {
* @param weak whether to use weak references for keys and values
*/
public CachingMapDecorator(boolean weak) {
Map internalMap = weak ? (Map) new WeakHashMap() : new HashMap();
Map<K, Object> internalMap = (weak ? new WeakHashMap<K, Object>() : new HashMap<K, Object>());
this.targetMap = Collections.synchronizedMap(internalMap);
this.synchronize = true;
this.weak = weak;
@@ -80,7 +80,7 @@ public class CachingMapDecorator implements Map, Serializable {
* @param size the initial cache size
*/
public CachingMapDecorator(boolean weak, int size) {
Map internalMap = weak ? (Map) new WeakHashMap(size) : new HashMap(size);
Map<K, Object> internalMap = weak ? new WeakHashMap<K, Object> (size) : new HashMap<K, Object>(size);
this.targetMap = Collections.synchronizedMap(internalMap);
this.synchronize = true;
this.weak = weak;
@@ -92,7 +92,7 @@ public class CachingMapDecorator implements Map, Serializable {
* so make sure to pass in a properly synchronized Map, if desired.
* @param targetMap the Map to decorate
*/
public CachingMapDecorator(Map targetMap) {
public CachingMapDecorator(Map<K, V> targetMap) {
this(targetMap, false, false);
}
@@ -104,9 +104,10 @@ public class CachingMapDecorator implements Map, Serializable {
* @param synchronize whether to synchronize on the given Map
* @param weak whether to use weak references for values
*/
public CachingMapDecorator(Map targetMap, boolean synchronize, boolean weak) {
@SuppressWarnings("unchecked")
public CachingMapDecorator(Map<K, V> targetMap, boolean synchronize, boolean weak) {
Assert.notNull(targetMap, "Target Map is required");
this.targetMap = (synchronize ? Collections.synchronizedMap(targetMap) : targetMap);
this.targetMap = (Map<K, Object>) (synchronize ? Collections.synchronizedMap(targetMap) : targetMap);
this.synchronize = synchronize;
this.weak = weak;
}
@@ -125,10 +126,7 @@ public class CachingMapDecorator implements Map, Serializable {
}
public boolean containsValue(Object value) {
Object valueToCheck = value;
if (valueToCheck == null) {
valueToCheck = NULL_VALUE;
}
Object valueToCheck = (value != null ? value : NULL_VALUE);
if (this.synchronize) {
synchronized (this.targetMap) {
return containsValueOrReference(valueToCheck);
@@ -143,8 +141,7 @@ public class CachingMapDecorator implements Map, Serializable {
if (this.targetMap.containsValue(value)) {
return true;
}
for (Iterator it = this.targetMap.values().iterator(); it.hasNext();) {
Object mapVal = it.next();
for (Object mapVal : this.targetMap.values()) {
if (mapVal instanceof Reference && value.equals(((Reference) mapVal).get())) {
return true;
}
@@ -152,11 +149,11 @@ public class CachingMapDecorator implements Map, Serializable {
return false;
}
public Object remove(Object key) {
return this.targetMap.remove(key);
public V remove(Object key) {
return unwrapIfNecessary(this.targetMap.remove(key));
}
public void putAll(Map map) {
public void putAll(Map<? extends K, ? extends V> map) {
this.targetMap.putAll(map);
}
@@ -164,18 +161,18 @@ public class CachingMapDecorator implements Map, Serializable {
this.targetMap.clear();
}
public Set keySet() {
public Set<K> keySet() {
if (this.synchronize) {
synchronized (this.targetMap) {
return new LinkedHashSet(this.targetMap.keySet());
return new LinkedHashSet<K>(this.targetMap.keySet());
}
}
else {
return new LinkedHashSet(this.targetMap.keySet());
return new LinkedHashSet<K>(this.targetMap.keySet());
}
}
public Collection values() {
public Collection<V> values() {
if (this.synchronize) {
synchronized (this.targetMap) {
return valuesCopy();
@@ -186,41 +183,49 @@ public class CachingMapDecorator implements Map, Serializable {
}
}
private Collection valuesCopy() {
LinkedList values = new LinkedList();
for (Iterator it = this.targetMap.values().iterator(); it.hasNext();) {
Object value = it.next();
values.add(value instanceof Reference ? ((Reference) value).get() : value);
@SuppressWarnings("unchecked")
private Collection<V> valuesCopy() {
LinkedList<V> values = new LinkedList<V>();
for (Object value : this.targetMap.values()) {
values.add(value instanceof Reference ? ((Reference<V>) value).get() : (V) value);
}
return values;
}
public Set entrySet() {
public Set<Map.Entry<K, V>> entrySet() {
if (this.synchronize) {
synchronized (this.targetMap) {
return new LinkedHashSet(this.targetMap.entrySet());
return entryCopy();
}
}
else {
return new LinkedHashSet(this.targetMap.entrySet());
return entryCopy();
}
}
private Set<Map.Entry<K, V>> entryCopy() {
Map<K,V> entries = new LinkedHashMap<K, V>();
for (Entry<K, Object> entry : this.targetMap.entrySet()) {
entries.put(entry.getKey(), unwrapIfNecessary(entry.getValue()));
}
return entries.entrySet();
}
/**
* Put an object into the cache, possibly wrapping it with a weak
* reference.
* @see #useWeakValue(Object, Object)
*/
public Object put(Object key, Object value) {
public V put(K key, V value) {
Object newValue = value;
if (newValue == null) {
if (value == null) {
newValue = NULL_VALUE;
}
if (useWeakValue(key, newValue)) {
newValue = new WeakReference(newValue);
else if (useWeakValue(key, value)) {
newValue = new WeakReference<V>(value);
}
return this.targetMap.put(key, newValue);
return unwrapIfNecessary(this.targetMap.put(key, newValue));
}
/**
@@ -231,7 +236,7 @@ public class CachingMapDecorator implements Map, Serializable {
* @return <code>true</code> in order to use a weak reference;
* <code>false</code> otherwise.
*/
protected boolean useWeakValue(Object key, Object value) {
protected boolean useWeakValue(K key, V value) {
return this.weak;
}
@@ -244,18 +249,30 @@ public class CachingMapDecorator implements Map, Serializable {
* Consider overriding this method to synchronize it, if desired.
* @see #create(Object)
*/
public Object get(Object key) {
@SuppressWarnings("unchecked")
public V get(Object key) {
Object value = this.targetMap.get(key);
if (value instanceof Reference) {
value = ((Reference) value).get();
}
if (value == null) {
value = create(key);
if (value != null) {
put(key, value);
V newVal = create((K) key);
if (newVal != null) {
put((K) key, newVal);
}
return newVal;
}
return unwrapIfNecessary(value);
}
@SuppressWarnings("unchecked")
private V unwrapIfNecessary(Object value) {
if (value instanceof Reference) {
return ((Reference<V>) value).get();
}
else if (value != null) {
return (V) value;
}
else {
return null;
}
return (value == NULL_VALUE ? null : value);
}
/**
@@ -264,7 +281,7 @@ public class CachingMapDecorator implements Map, Serializable {
* @param key the cache key
* @see #get(Object)
*/
protected Object create(Object key) {
protected V create(K key) {
return null;
}

View File

@@ -492,7 +492,7 @@ public abstract class ClassUtils {
* @return whether the class has a corresponding constructor
* @see java.lang.Class#getMethod
*/
public static boolean hasConstructor(Class clazz, Class[] paramTypes) {
public static boolean hasConstructor(Class clazz, Class... paramTypes) {
return (getConstructorIfAvailable(clazz, paramTypes) != null);
}
@@ -505,7 +505,7 @@ public abstract class ClassUtils {
* @return the constructor, or <code>null</code> if not found
* @see java.lang.Class#getConstructor
*/
public static Constructor getConstructorIfAvailable(Class clazz, Class[] paramTypes) {
public static Constructor getConstructorIfAvailable(Class clazz, Class... paramTypes) {
Assert.notNull(clazz, "Class must not be null");
try {
return clazz.getConstructor(paramTypes);
@@ -524,7 +524,7 @@ public abstract class ClassUtils {
* @return whether the class has a corresponding method
* @see java.lang.Class#getMethod
*/
public static boolean hasMethod(Class clazz, String methodName, Class[] paramTypes) {
public static boolean hasMethod(Class clazz, String methodName, Class... paramTypes) {
return (getMethodIfAvailable(clazz, methodName, paramTypes) != null);
}
@@ -538,7 +538,7 @@ public abstract class ClassUtils {
* @return the method, or <code>null</code> if not found
* @see java.lang.Class#getMethod
*/
public static Method getMethodIfAvailable(Class clazz, String methodName, Class[] paramTypes) {
public static Method getMethodIfAvailable(Class clazz, String methodName, Class... paramTypes) {
Assert.notNull(clazz, "Class must not be null");
Assert.notNull(methodName, "Method name must not be null");
try {
@@ -640,7 +640,7 @@ public abstract class ClassUtils {
* @return the static method, or <code>null</code> if no static method was found
* @throws IllegalArgumentException if the method name is blank or the clazz is null
*/
public static Method getStaticMethod(Class clazz, String methodName, Class[] args) {
public static Method getStaticMethod(Class clazz, String methodName, Class... args) {
Assert.notNull(clazz, "Class must not be null");
Assert.notNull(methodName, "Method name must not be null");
try {
@@ -809,7 +809,7 @@ public abstract class ClassUtils {
* @return a String of form "[com.foo.Bar, com.foo.Baz]"
* @see java.util.AbstractCollection#toString()
*/
public static String classNamesToString(Class[] classes) {
public static String classNamesToString(Class... classes) {
return classNamesToString(Arrays.asList(classes));
}
@@ -822,13 +822,13 @@ public abstract class ClassUtils {
* @return a String of form "[com.foo.Bar, com.foo.Baz]"
* @see java.util.AbstractCollection#toString()
*/
public static String classNamesToString(Collection classes) {
public static String classNamesToString(Collection<Class> classes) {
if (CollectionUtils.isEmpty(classes)) {
return "[]";
}
StringBuilder sb = new StringBuilder("[");
for (Iterator it = classes.iterator(); it.hasNext(); ) {
Class clazz = (Class) it.next();
for (Iterator<Class> it = classes.iterator(); it.hasNext(); ) {
Class clazz = it.next();
sb.append(clazz.getName());
if (it.hasNext()) {
sb.append(", ");
@@ -894,7 +894,7 @@ public abstract class ClassUtils {
* @param instance the instance to analyse for interfaces
* @return all interfaces that the given instance implements as Set
*/
public static Set getAllInterfacesAsSet(Object instance) {
public static Set<Class> getAllInterfacesAsSet(Object instance) {
Assert.notNull(instance, "Instance must not be null");
return getAllInterfacesForClassAsSet(instance.getClass());
}

View File

@@ -72,13 +72,14 @@ public abstract class CollectionUtils {
* @param array the array to merge (may be <code>null</code>)
* @param collection the target Collection to merge the array into
*/
@SuppressWarnings("unchecked")
public static void mergeArrayIntoCollection(Object array, Collection collection) {
if (collection == null) {
throw new IllegalArgumentException("Collection must not be null");
}
Object[] arr = ObjectUtils.toObjectArray(array);
for (int i = 0; i < arr.length; i++) {
collection.add(arr[i]);
for (Object elem : arr) {
collection.add(elem);
}
}
@@ -90,6 +91,7 @@ public abstract class CollectionUtils {
* @param props the Properties instance to merge (may be <code>null</code>)
* @param map the target Map to merge the properties into
*/
@SuppressWarnings("unchecked")
public static void mergePropertiesIntoMap(Properties props, Map map) {
if (map == null) {
throw new IllegalArgumentException("Map must not be null");
@@ -149,8 +151,7 @@ public abstract class CollectionUtils {
*/
public static boolean containsInstance(Collection collection, Object element) {
if (collection != null) {
for (Iterator it = collection.iterator(); it.hasNext();) {
Object candidate = it.next();
for (Object candidate : collection) {
if (candidate == element) {
return true;
}
@@ -170,8 +171,8 @@ public abstract class CollectionUtils {
if (isEmpty(source) || isEmpty(candidates)) {
return false;
}
for (Iterator it = candidates.iterator(); it.hasNext();) {
if (source.contains(it.next())) {
for (Object candidate : candidates) {
if (source.contains(candidate)) {
return true;
}
}
@@ -191,8 +192,7 @@ public abstract class CollectionUtils {
if (isEmpty(source) || isEmpty(candidates)) {
return null;
}
for (Iterator it = candidates.iterator(); it.hasNext();) {
Object candidate = it.next();
for (Object candidate : candidates) {
if (source.contains(candidate)) {
return candidate;
}
@@ -207,19 +207,19 @@ public abstract class CollectionUtils {
* @return a value of the given type found if there is a clear match,
* or <code>null</code> if none or more than one such value found
*/
public static Object findValueOfType(Collection collection, Class type) {
@SuppressWarnings("unchecked")
public static <T> T findValueOfType(Collection collection, Class<T> type) {
if (isEmpty(collection)) {
return null;
}
Object value = null;
for (Iterator it = collection.iterator(); it.hasNext();) {
Object obj = it.next();
if (type == null || type.isInstance(obj)) {
T value = null;
for (Object element : collection) {
if (type == null || type.isInstance(element)) {
if (value != null) {
// More than one value found... no clear single value.
return null;
}
value = obj;
value = (T) element;
}
}
return value;
@@ -238,8 +238,8 @@ public abstract class CollectionUtils {
if (isEmpty(collection) || ObjectUtils.isEmpty(types)) {
return null;
}
for (int i = 0; i < types.length; i++) {
Object value = findValueOfType(collection, types[i]);
for (Class type : types) {
Object value = findValueOfType(collection, type);
if (value != null) {
return value;
}

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.
@@ -19,7 +19,6 @@ package org.springframework.util.comparator;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import org.springframework.util.Assert;
@@ -40,7 +39,7 @@ import org.springframework.util.Assert;
*/
public class CompoundComparator implements Comparator, Serializable {
private final List comparators;
private final List<InvertibleComparator> comparators;
/**
@@ -49,7 +48,7 @@ public class CompoundComparator implements Comparator, Serializable {
* IllegalStateException is thrown.
*/
public CompoundComparator() {
this.comparators = new ArrayList();
this.comparators = new ArrayList<InvertibleComparator>();
}
/**
@@ -60,9 +59,9 @@ public class CompoundComparator implements Comparator, Serializable {
* @see InvertibleComparator
*/
public CompoundComparator(Comparator[] comparators) {
this.comparators = new ArrayList(comparators.length);
for (int i = 0; i < comparators.length; i++) {
addComparator(comparators[i]);
this.comparators = new ArrayList<InvertibleComparator>(comparators.length);
for (Comparator comparator : comparators) {
addComparator(comparator);
}
}
@@ -76,7 +75,7 @@ public class CompoundComparator implements Comparator, Serializable {
*/
public void addComparator(Comparator comparator) {
if (comparator instanceof InvertibleComparator) {
this.comparators.add(comparator);
this.comparators.add((InvertibleComparator) comparator);
}
else {
this.comparators.add(new InvertibleComparator(comparator));
@@ -102,7 +101,7 @@ public class CompoundComparator implements Comparator, Serializable {
*/
public void setComparator(int index, Comparator comparator) {
if (comparator instanceof InvertibleComparator) {
this.comparators.set(index, comparator);
this.comparators.set(index, (InvertibleComparator) comparator);
}
else {
InvertibleComparator invComp = new InvertibleComparator(comparator);
@@ -117,8 +116,7 @@ public class CompoundComparator implements Comparator, Serializable {
* @param ascending the sort order: ascending (true) or descending (false)
*/
public void setComparator(int index, Comparator comparator, boolean ascending) {
InvertibleComparator invComp = new InvertibleComparator(comparator, ascending);
this.comparators.set(index, invComp);
this.comparators.set(index, new InvertibleComparator(comparator, ascending));
}
/**
@@ -126,9 +124,8 @@ public class CompoundComparator implements Comparator, Serializable {
* comparator.
*/
public void invertOrder() {
Iterator it = this.comparators.iterator();
while (it.hasNext()) {
((InvertibleComparator) it.next()).invertOrder();
for (InvertibleComparator comparator : this.comparators) {
comparator.invertOrder();
}
}
@@ -137,7 +134,7 @@ public class CompoundComparator implements Comparator, Serializable {
* @param index the index of the comparator to invert
*/
public void invertOrder(int index) {
getInvertibleComparator(index).invertOrder();
this.comparators.get(index).invertOrder();
}
/**
@@ -145,7 +142,7 @@ public class CompoundComparator implements Comparator, Serializable {
* @param index the index of the comparator to change
*/
public void setAscendingOrder(int index) {
getInvertibleComparator(index).setAscending(true);
this.comparators.get(index).setAscending(true);
}
/**
@@ -153,30 +150,22 @@ public class CompoundComparator implements Comparator, Serializable {
* @param index the index of the comparator to change
*/
public void setDescendingOrder(int index) {
getInvertibleComparator(index).setAscending(false);
}
/**
* Return the InvertibleComparator for the given index, if any.
*/
private InvertibleComparator getInvertibleComparator(int index) {
return (InvertibleComparator) this.comparators.get(index);
this.comparators.get(index).setAscending(false);
}
/**
* Returns the number of aggregated comparators.
*/
public int getComparatorCount() {
return comparators.size();
return this.comparators.size();
}
public int compare(Object o1, Object o2) {
Assert.state(this.comparators.size() > 0,
"No sort definitions have been added to this CompoundComparator to compare");
for (Iterator it = this.comparators.iterator(); it.hasNext();) {
InvertibleComparator def = (InvertibleComparator) it.next();
int result = def.compare(o1, o2);
for (InvertibleComparator comparator : this.comparators) {
int result = comparator.compare(o1, o2);
if (result != 0) {
return result;
}

View File

@@ -54,16 +54,16 @@ public abstract class DomUtils {
* @see org.w3c.dom.Element
* @see org.w3c.dom.Element#getElementsByTagName
*/
public static List getChildElementsByTagName(Element ele, String[] childEleNames) {
public static List<Element> getChildElementsByTagName(Element ele, String[] childEleNames) {
Assert.notNull(ele, "Element must not be null");
Assert.notNull(childEleNames, "Element names collection must not be null");
List childEleNameList = Arrays.asList(childEleNames);
List<String> childEleNameList = Arrays.asList(childEleNames);
NodeList nl = ele.getChildNodes();
List childEles = new ArrayList();
List<Element> childEles = new ArrayList<Element>();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element && nodeNameMatch(node, childEleNameList)) {
childEles.add(node);
childEles.add((Element) node);
}
}
return childEles;
@@ -80,7 +80,7 @@ public abstract class DomUtils {
* @see org.w3c.dom.Element
* @see org.w3c.dom.Element#getElementsByTagName
*/
public static List getChildElementsByTagName(Element ele, String childEleName) {
public static List<Element> getChildElementsByTagName(Element ele, String childEleName) {
return getChildElementsByTagName(ele, new String[] {childEleName});
}