Java 5 code style

This commit is contained in:
Juergen Hoeller
2008-11-27 17:35:44 +00:00
parent b0790bf5e7
commit 85661c6882
49 changed files with 353 additions and 477 deletions

View File

@@ -48,7 +48,7 @@ public abstract class Conventions {
* Set of interfaces that are supposed to be ignored
* when searching for the 'primary' interface of a proxy.
*/
private static final Set ignoredInterfaces = new HashSet();
private static final Set<Class> ignoredInterfaces = new HashSet<Class>();
static {
ignoredInterfaces.add(Serializable.class);
@@ -212,15 +212,14 @@ public abstract class Conventions {
*/
public static String attributeNameToPropertyName(String attributeName) {
Assert.notNull(attributeName, "'attributeName' must not be null");
if (attributeName.indexOf("-") == -1) {
if (!attributeName.contains("-")) {
return attributeName;
}
char[] chars = attributeName.toCharArray();
char[] result = new char[chars.length -1]; // not completely accurate but good guess
int currPos = 0;
boolean upperCaseNext = false;
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
for (char c : chars) {
if (c == '-') {
upperCaseNext = true;
}
@@ -260,8 +259,7 @@ public abstract class Conventions {
Class valueClass = value.getClass();
if (Proxy.isProxyClass(valueClass)) {
Class[] ifcs = valueClass.getInterfaces();
for (int i = 0; i < ifcs.length; i++) {
Class ifc = ifcs[i];
for (Class ifc : ifcs) {
if (!ignoredInterfaces.contains(ifc)) {
return ifc;
}

View File

@@ -21,7 +21,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.springframework.core.io.Resource;
@@ -94,17 +93,15 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
public void setValue(Object value) throws IllegalArgumentException {
if (value instanceof Collection || (value instanceof Object[] && !(value instanceof Resource[]))) {
Collection input = (value instanceof Collection ? (Collection) value : Arrays.asList((Object[]) value));
List merged = new ArrayList();
for (Iterator it = input.iterator(); it.hasNext();) {
Object element = it.next();
List<Resource> merged = new ArrayList<Resource>();
for (Object element : input) {
if (element instanceof String) {
// A location pattern: resolve it into a Resource array.
// Might point to a single resource or to multiple resources.
String pattern = resolvePath((String) element).trim();
try {
Resource[] resources = this.resourcePatternResolver.getResources(pattern);
for (int i = 0; i < resources.length; i++) {
Resource resource = resources[i];
for (Resource resource : resources) {
if (!merged.contains(resource)) {
merged.add(resource);
}
@@ -117,8 +114,9 @@ public class ResourceArrayPropertyEditor extends PropertyEditorSupport {
}
else if (element instanceof Resource) {
// A Resource object: add it to the result.
if (!merged.contains(element)) {
merged.add(element);
Resource resource = (Resource) element;
if (!merged.contains(resource)) {
merged.add(resource);
}
}
else {

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.
@@ -39,18 +39,18 @@ import java.util.ListIterator;
* @author Juergen Hoeller
* @since 2.0
*/
public class AutoPopulatingList implements List, Serializable {
public class AutoPopulatingList<E> implements List<E>, Serializable {
/**
* The {@link List} that all operations are eventually delegated to.
*/
private final List backingList;
private final List<E> backingList;
/**
* The {@link ElementFactory} to use to create new {@link List} elements
* on demand.
*/
private final ElementFactory elementFactory;
private final ElementFactory<E> elementFactory;
/**
@@ -58,8 +58,8 @@ public class AutoPopulatingList implements List, Serializable {
* {@link ArrayList} and adds new instances of the supplied {@link Class element Class}
* to the backing {@link List} on demand.
*/
public AutoPopulatingList(Class elementClass) {
this(new ArrayList(), elementClass);
public AutoPopulatingList(Class<? extends E> elementClass) {
this(new ArrayList<E>(), elementClass);
}
/**
@@ -67,23 +67,23 @@ public class AutoPopulatingList implements List, Serializable {
* and adds new instances of the supplied {@link Class element Class} to the backing
* {@link List} on demand.
*/
public AutoPopulatingList(List backingList, Class elementClass) {
this(backingList, new ReflectiveElementFactory(elementClass));
public AutoPopulatingList(List<E> backingList, Class<? extends E> elementClass) {
this(backingList, new ReflectiveElementFactory<E>(elementClass));
}
/**
* Creates a new <code>AutoPopulatingList</code> that is backed by a standard
* {@link ArrayList} and creates new elements on demand using the supplied {@link ElementFactory}.
*/
public AutoPopulatingList(ElementFactory elementFactory) {
this(new ArrayList(), elementFactory);
public AutoPopulatingList(ElementFactory<E> elementFactory) {
this(new ArrayList<E>(), elementFactory);
}
/**
* Creates a new <code>AutoPopulatingList</code> that is backed by the supplied {@link List}
* and creates new elements on demand using the supplied {@link ElementFactory}.
*/
public AutoPopulatingList(List backingList, ElementFactory elementFactory) {
public AutoPopulatingList(List<E> backingList, ElementFactory<E> elementFactory) {
Assert.notNull(backingList, "Backing List must not be null");
Assert.notNull(elementFactory, "Element factory must not be null");
this.backingList = backingList;
@@ -91,19 +91,19 @@ public class AutoPopulatingList implements List, Serializable {
}
public void add(int index, Object element) {
public void add(int index, E element) {
this.backingList.add(index, element);
}
public boolean add(Object o) {
public boolean add(E o) {
return this.backingList.add(o);
}
public boolean addAll(Collection c) {
public boolean addAll(Collection<? extends E> c) {
return this.backingList.addAll(c);
}
public boolean addAll(int index, Collection c) {
public boolean addAll(int index, Collection<? extends E> c) {
return this.backingList.addAll(index, c);
}
@@ -119,19 +119,13 @@ public class AutoPopulatingList implements List, Serializable {
return this.backingList.containsAll(c);
}
@Override
public boolean equals(Object o) {
return this.backingList.equals(o);
}
/**
* Get the element at the supplied index, creating it if there is
* no element at that index.
*/
public Object get(int index) {
public E get(int index) {
int backingListSize = this.backingList.size();
Object element = null;
E element = null;
if (index < backingListSize) {
element = this.backingList.get(index);
if (element == null) {
@@ -149,11 +143,6 @@ public class AutoPopulatingList implements List, Serializable {
return element;
}
@Override
public int hashCode() {
return this.backingList.hashCode();
}
public int indexOf(Object o) {
return this.backingList.indexOf(o);
}
@@ -162,7 +151,7 @@ public class AutoPopulatingList implements List, Serializable {
return this.backingList.isEmpty();
}
public Iterator iterator() {
public Iterator<E> iterator() {
return this.backingList.iterator();
}
@@ -170,15 +159,15 @@ public class AutoPopulatingList implements List, Serializable {
return this.backingList.lastIndexOf(o);
}
public ListIterator listIterator() {
public ListIterator<E> listIterator() {
return this.backingList.listIterator();
}
public ListIterator listIterator(int index) {
public ListIterator<E> listIterator(int index) {
return this.backingList.listIterator(index);
}
public Object remove(int index) {
public E remove(int index) {
return this.backingList.remove(index);
}
@@ -186,15 +175,15 @@ public class AutoPopulatingList implements List, Serializable {
return this.backingList.remove(o);
}
public boolean removeAll(Collection c) {
public boolean removeAll(Collection<?> c) {
return this.backingList.removeAll(c);
}
public boolean retainAll(Collection c) {
public boolean retainAll(Collection<?> c) {
return this.backingList.retainAll(c);
}
public Object set(int index, Object element) {
public E set(int index, E element) {
return this.backingList.set(index, element);
}
@@ -202,7 +191,7 @@ public class AutoPopulatingList implements List, Serializable {
return this.backingList.size();
}
public List subList(int fromIndex, int toIndex) {
public List<E> subList(int fromIndex, int toIndex) {
return this.backingList.subList(fromIndex, toIndex);
}
@@ -210,16 +199,27 @@ public class AutoPopulatingList implements List, Serializable {
return this.backingList.toArray();
}
public Object[] toArray(Object[] a) {
public <T> T[] toArray(T[] a) {
return this.backingList.toArray(a);
}
@Override
public boolean equals(Object other) {
return this.backingList.equals(other);
}
@Override
public int hashCode() {
return this.backingList.hashCode();
}
/**
* Factory interface for creating elements for an index-based access
* data structure such as a {@link java.util.List}.
*/
public interface ElementFactory {
public interface ElementFactory<E> {
/**
* Create the element for the supplied index.
@@ -227,7 +227,7 @@ public class AutoPopulatingList implements List, Serializable {
* @throws ElementInstantiationException if the instantiation process failed
* (any exception thrown by a target constructor should be propagated as-is)
*/
Object createElement(int index) throws ElementInstantiationException;
E createElement(int index) throws ElementInstantiationException;
}
@@ -247,18 +247,18 @@ public class AutoPopulatingList implements List, Serializable {
* using <code>Class.newInstance()</code> on a given element class.
* @see java.lang.Class#newInstance()
*/
private static class ReflectiveElementFactory implements ElementFactory, Serializable {
private static class ReflectiveElementFactory<E> implements ElementFactory<E>, Serializable {
private final Class elementClass;
private final Class<? extends E> elementClass;
public ReflectiveElementFactory(Class elementClass) {
public ReflectiveElementFactory(Class<? extends E> elementClass) {
Assert.notNull(elementClass, "Element clas must not be null");
Assert.isTrue(!elementClass.isInterface(), "Element class must not be an interface type");
Assert.isTrue(!Modifier.isAbstract(elementClass.getModifiers()), "Element class cannot be an abstract class");
this.elementClass = elementClass;
}
public Object createElement(int index) {
public E createElement(int index) {
try {
return this.elementClass.newInstance();
}

View File

@@ -67,10 +67,9 @@ public abstract class ReflectionUtils {
Class searchType = clazz;
while (!Object.class.equals(searchType) && searchType != null) {
Field[] fields = searchType.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
if ((name == null || name.equals(field.getName()))
&& (type == null || type.equals(field.getType()))) {
for (Field field : fields) {
if ((name == null || name.equals(field.getName())) &&
(type == null || type.equals(field.getType()))) {
return field;
}
}
@@ -152,8 +151,7 @@ public abstract class ReflectionUtils {
Class searchType = clazz;
while (!Object.class.equals(searchType) && searchType != null) {
Method[] methods = (searchType.isInterface() ? searchType.getMethods() : searchType.getDeclaredMethods());
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
for (Method method : methods) {
if (name.equals(method.getName()) &&
(paramTypes == null || Arrays.equals(paramTypes, method.getParameterTypes()))) {
return method;
@@ -454,16 +452,16 @@ public abstract class ReflectionUtils {
// Keep backing up the inheritance hierarchy.
do {
Method[] methods = targetClass.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
if (mf != null && !mf.matches(methods[i])) {
for (Method method : methods) {
if (mf != null && !mf.matches(method)) {
continue;
}
try {
mc.doWith(methods[i]);
mc.doWith(method);
}
catch (IllegalAccessException ex) {
throw new IllegalStateException(
"Shouldn't be illegal to access method '" + methods[i].getName() + "': " + ex);
"Shouldn't be illegal to access method '" + method.getName() + "': " + ex);
}
}
targetClass = targetClass.getSuperclass();
@@ -476,13 +474,13 @@ public abstract class ReflectionUtils {
* Leaf class methods are included first.
*/
public static Method[] getAllDeclaredMethods(Class leafClass) throws IllegalArgumentException {
final List list = new ArrayList(32);
final List<Method> methods = new ArrayList<Method>(32);
doWithMethods(leafClass, new MethodCallback() {
public void doWith(Method method) {
list.add(method);
methods.add(method);
}
});
return (Method[]) list.toArray(new Method[list.size()]);
return methods.toArray(new Method[methods.size()]);
}
@@ -510,17 +508,17 @@ public abstract class ReflectionUtils {
do {
// Copy each field declared on this class unless it's static or file.
Field[] fields = targetClass.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
for (Field field : fields) {
// Skip static and final fields.
if (ff != null && !ff.matches(fields[i])) {
if (ff != null && !ff.matches(field)) {
continue;
}
try {
fc.doWith(fields[i]);
fc.doWith(field);
}
catch (IllegalAccessException ex) {
throw new IllegalStateException(
"Shouldn't be illegal to access field '" + fields[i].getName() + "': " + ex);
"Shouldn't be illegal to access field '" + field.getName() + "': " + ex);
}
}
targetClass = targetClass.getSuperclass();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-20078the 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.
@@ -48,8 +48,7 @@ public class StopWatch {
private boolean keepTaskList = true;
/** List of TaskInfo objects */
private final List taskList = new LinkedList();
private final List<TaskInfo> taskList = new LinkedList<TaskInfo>();
/** Start time of the current task */
private long startTimeMillis;
@@ -187,7 +186,7 @@ public class StopWatch {
if (!this.keepTaskList) {
throw new UnsupportedOperationException("Task info is not being kept!");
}
return (TaskInfo[]) this.taskList.toArray(new TaskInfo[this.taskList.size()]);
return this.taskList.toArray(new TaskInfo[this.taskList.size()]);
}
@@ -209,7 +208,6 @@ public class StopWatch {
sb.append("No task info kept");
}
else {
TaskInfo[] tasks = getTaskInfo();
sb.append("-----------------------------------------\n");
sb.append("ms % Task name\n");
sb.append("-----------------------------------------\n");
@@ -219,10 +217,10 @@ public class StopWatch {
NumberFormat pf = NumberFormat.getPercentInstance();
pf.setMinimumIntegerDigits(3);
pf.setGroupingUsed(false);
for (int i = 0; i < tasks.length; i++) {
sb.append(nf.format(tasks[i].getTimeMillis()) + " ");
sb.append(pf.format(tasks[i].getTimeSeconds() / getTotalTimeSeconds()) + " ");
sb.append(tasks[i].getTaskName() + "\n");
for (TaskInfo task : getTaskInfo()) {
sb.append(nf.format(task.getTimeMillis())).append(" ");
sb.append(pf.format(task.getTimeSeconds() / getTotalTimeSeconds())).append(" ");
sb.append(task.getTaskName()).append("\n");
}
}
return sb.toString();
@@ -236,11 +234,10 @@ public class StopWatch {
public String toString() {
StringBuilder sb = new StringBuilder(shortSummary());
if (this.keepTaskList) {
TaskInfo[] tasks = getTaskInfo();
for (int i = 0; i < tasks.length; i++) {
sb.append("; [" + tasks[i].getTaskName() + "] took " + tasks[i].getTimeMillis());
long percent = Math.round((100.0 * tasks[i].getTimeSeconds()) / getTotalTimeSeconds());
sb.append(" = " + percent + "%");
for (TaskInfo task : getTaskInfo()) {
sb.append("; [").append(task.getTaskName()).append("] took ").append(task.getTimeMillis());
long percent = Math.round((100.0 * task.getTimeSeconds()) / getTotalTimeSeconds());
sb.append(" = ").append(percent).append("%");
}
}
else {