From: Per-Åke Minborg Date: Fri, 28 Oct 2016 15:28:56 +0000 (-0700) Subject: Convert to for-each loop, Stream and switch-over-strings X-Git-Tag: 8.0.0.alpha6~6 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=73078c7560d40db80dae6a6fb8047c29a92dd8d0;p=vaadin-framework.git Convert to for-each loop, Stream and switch-over-strings Change-Id: I80b73b653e97904605dc62484a7448f3bfbf7218 --- diff --git a/server/src/main/java/com/vaadin/event/EventRouter.java b/server/src/main/java/com/vaadin/event/EventRouter.java index c5237d9aca..6b96e91f25 100644 --- a/server/src/main/java/com/vaadin/event/EventRouter.java +++ b/server/src/main/java/com/vaadin/event/EventRouter.java @@ -28,6 +28,7 @@ import java.util.logging.Logger; import com.vaadin.server.ErrorEvent; import com.vaadin.server.ErrorHandler; +import java.util.stream.Stream; /** * EventRouter class implementing the inheritable event listening @@ -124,15 +125,10 @@ public class EventRouter implements MethodEventSource { // Find the correct method final Method[] methods = target.getClass().getMethods(); - Method method = null; - for (int i = 0; i < methods.length; i++) { - if (methods[i].getName().equals(methodName)) { - method = methods[i]; - } - } - if (method == null) { - throw new IllegalArgumentException(); - } + + final Method method = Stream.of(methods) + .filter(m -> methodName.equals(m.getName())) + .findAny().orElseThrow(IllegalArgumentException::new); // Remove the listeners if (listenerList != null) { @@ -191,8 +187,8 @@ public class EventRouter implements MethodEventSource { // Send the event to all listeners. The listeners themselves // will filter out unwanted events. final Object[] listeners = listenerList.toArray(); - for (int i = 0; i < listeners.length; i++) { - ListenerMethod listenerMethod = (ListenerMethod) listeners[i]; + for (Object listener : listeners) { + ListenerMethod listenerMethod = (ListenerMethod) listener; if (null != errorHandler) { try { listenerMethod.receiveEvent(event); diff --git a/server/src/main/java/com/vaadin/event/ListenerMethod.java b/server/src/main/java/com/vaadin/event/ListenerMethod.java index ff556f4117..b81ebd28bc 100644 --- a/server/src/main/java/com/vaadin/event/ListenerMethod.java +++ b/server/src/main/java/com/vaadin/event/ListenerMethod.java @@ -25,6 +25,7 @@ import java.util.EventListener; import java.util.EventObject; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.Stream; /** *

@@ -118,9 +119,8 @@ public class ListenerMethod implements EventListener, Serializable { private static Method findHighestMethod(Class cls, String method, Class[] paramTypes) { Class[] ifaces = cls.getInterfaces(); - for (int i = 0; i < ifaces.length; i++) { - Method ifaceMethod = findHighestMethod(ifaces[i], method, - paramTypes); + for (Class iface : ifaces) { + Method ifaceMethod = findHighestMethod(iface, method, paramTypes); if (ifaceMethod != null) { return ifaceMethod; } @@ -132,14 +132,12 @@ public class ListenerMethod implements EventListener, Serializable { return parentMethod; } } - Method[] methods = cls.getMethods(); - for (int i = 0; i < methods.length; i++) { - // we ignore parameter types for now - you need to add this - if (methods[i].getName().equals(method)) { - return methods[i]; - } - } - return null; + + // we ignore parameter types for now - you need to add this + return Stream.of(cls.getMethods()) + .filter(m -> method.equals(m.getName())) + .findAny().orElse(null); + } /** @@ -247,9 +245,9 @@ public class ListenerMethod implements EventListener, Serializable { // Finds the correct method final Method[] methods = target.getClass().getMethods(); Method method = null; - for (int i = 0; i < methods.length; i++) { - if (methods[i].getName().equals(methodName)) { - method = methods[i]; + for (Method method1 : methods) { + if (method1.getName().equals(methodName)) { + method = method1; } } if (method == null) { @@ -359,9 +357,9 @@ public class ListenerMethod implements EventListener, Serializable { // Find the correct method final Method[] methods = target.getClass().getMethods(); Method method = null; - for (int i = 0; i < methods.length; i++) { - if (methods[i].getName().equals(methodName)) { - method = methods[i]; + for (Method method1 : methods) { + if (method1.getName().equals(methodName)) { + method = method1; } } if (method == null) { @@ -461,9 +459,9 @@ public class ListenerMethod implements EventListener, Serializable { // Finds the correct method final Method[] methods = target.getClass().getMethods(); Method method = null; - for (int i = 0; i < methods.length; i++) { - if (methods[i].getName().equals(methodName)) { - method = methods[i]; + for (Method method1 : methods) { + if (method1.getName().equals(methodName)) { + method = method1; } } if (method == null) { diff --git a/server/src/main/java/com/vaadin/server/ComponentSizeValidator.java b/server/src/main/java/com/vaadin/server/ComponentSizeValidator.java index e68fbb5903..c7c6541c9e 100644 --- a/server/src/main/java/com/vaadin/server/ComponentSizeValidator.java +++ b/server/src/main/java/com/vaadin/server/ComponentSizeValidator.java @@ -92,9 +92,7 @@ public class ComponentSizeValidator implements Serializable { } } else if (isForm(component)) { HasComponents form = (HasComponents) component; - for (Iterator iterator = form.iterator(); iterator - .hasNext();) { - Component child = iterator.next(); + for (Component child : form) { errors = validateComponentRelativeSizes(child, errors, parent); } } diff --git a/server/src/main/java/com/vaadin/server/JsonCodec.java b/server/src/main/java/com/vaadin/server/JsonCodec.java index 588443d55b..a85dd5c0e9 100644 --- a/server/src/main/java/com/vaadin/server/JsonCodec.java +++ b/server/src/main/java/com/vaadin/server/JsonCodec.java @@ -398,16 +398,19 @@ public class JsonCodec implements Serializable { connectorTracker); } - // Collections - if (JsonConstants.VTYPE_LIST.equals(transportType)) { - return decodeList(targetType, restrictToInternalTypes, + if (null != transportType) // Collections + switch (transportType) { + case JsonConstants.VTYPE_LIST: + return decodeList(targetType, restrictToInternalTypes, (JsonArray) encodedJsonValue, connectorTracker); - } else if (JsonConstants.VTYPE_SET.equals(transportType)) { - return decodeSet(targetType, restrictToInternalTypes, + case JsonConstants.VTYPE_SET: + return decodeSet(targetType, restrictToInternalTypes, (JsonArray) encodedJsonValue, connectorTracker); - } else if (JsonConstants.VTYPE_MAP.equals(transportType)) { - return decodeMap(targetType, restrictToInternalTypes, + case JsonConstants.VTYPE_MAP: + return decodeMap(targetType, restrictToInternalTypes, encodedJsonValue, connectorTracker); + default: + break; } // Arrays @@ -427,20 +430,23 @@ public class JsonCodec implements Serializable { return connectorTracker.getConnector(encodedJsonValue.asString()); } - // Legacy types - - if (JsonConstants.VTYPE_STRING.equals(transportType)) { - return encodedJsonValue.asString(); - } else if (JsonConstants.VTYPE_INTEGER.equals(transportType)) { - return (int) encodedJsonValue.asNumber(); - } else if (JsonConstants.VTYPE_LONG.equals(transportType)) { - return (long) encodedJsonValue.asNumber(); - } else if (JsonConstants.VTYPE_FLOAT.equals(transportType)) { - return (float) encodedJsonValue.asNumber(); - } else if (JsonConstants.VTYPE_DOUBLE.equals(transportType)) { - return encodedJsonValue.asNumber(); - } else if (JsonConstants.VTYPE_BOOLEAN.equals(transportType)) { - return encodedJsonValue.asBoolean(); + + if (null != transportType) // Legacy types + switch (transportType) { + case JsonConstants.VTYPE_STRING: + return encodedJsonValue.asString(); + case JsonConstants.VTYPE_INTEGER: + return (int) encodedJsonValue.asNumber(); + case JsonConstants.VTYPE_LONG: + return (long) encodedJsonValue.asNumber(); + case JsonConstants.VTYPE_FLOAT: + return (float) encodedJsonValue.asNumber(); + case JsonConstants.VTYPE_DOUBLE: + return encodedJsonValue.asNumber(); + case JsonConstants.VTYPE_BOOLEAN: + return encodedJsonValue.asBoolean(); + default: + break; } throw new JsonException("Unknown type " + transportType); diff --git a/server/src/main/java/com/vaadin/server/widgetsetutils/ClassPathExplorer.java b/server/src/main/java/com/vaadin/server/widgetsetutils/ClassPathExplorer.java index 7aec100255..42320cb02f 100644 --- a/server/src/main/java/com/vaadin/server/widgetsetutils/ClassPathExplorer.java +++ b/server/src/main/java/com/vaadin/server/widgetsetutils/ClassPathExplorer.java @@ -203,45 +203,42 @@ public class ClassPathExplorer { if (directory.exists() && !directory.isHidden()) { // Get the list of the files contained in the directory String[] files = directory.list(); - for (int i = 0; i < files.length; i++) { + for (String file : files) { // we are only interested in .gwt.xml files - if (!files[i].endsWith(".gwt.xml")) { + if (!file.endsWith(".gwt.xml")) { continue; } - // remove the .gwt.xml extension - String classname = files[i].substring(0, files[i].length() - 8); + String classname = file.substring(0, file.length() - 8); String packageName = locationString - .substring(locationString.lastIndexOf("/") + 1); + .substring(locationString.lastIndexOf("/") + 1); classname = packageName + "." + classname; - if (!WidgetSetBuilder.isWidgetset(classname)) { // Only return widgetsets and not GWT modules to avoid // comparing modules and widgetsets continue; } - if (!widgetsets.containsKey(classname)) { String packagePath = packageName.replaceAll("\\.", "/"); String basePath = location.getFile(); if (basePath.endsWith("/" + packagePath)) { basePath = basePath.replaceAll("/" + packagePath + "$", - ""); + ""); } else if (basePath.endsWith("/" + packagePath + "/")) { basePath = basePath.replaceAll("/" + packagePath + "/$", - ""); + ""); } else { throw new IllegalStateException( - "Error trying to find base path, location (" - + location.getFile() - + ") does not end in expected '/" - + packagePath + "'"); + "Error trying to find base path, location (" + + location.getFile() + + ") does not end in expected '/" + + packagePath + "'"); } try { URL url = new URL(location.getProtocol(), - location.getHost(), location.getPort(), - basePath); + location.getHost(), location.getPort(), + basePath); widgetsets.put(classname, url); } catch (MalformedURLException e) { // should never happen as based on an existing URL, @@ -273,8 +270,8 @@ public class ClassPathExplorer { .getValue("Vaadin-Widgetsets"); if (value != null) { String[] widgetsetNames = value.split(","); - for (int i = 0; i < widgetsetNames.length; i++) { - String widgetsetname = widgetsetNames[i].trim(); + for (String widgetsetName : widgetsetNames) { + String widgetsetname = widgetsetName.trim(); if (!widgetsetname.equals("")) { widgetsets.put(widgetsetname, location); } @@ -286,8 +283,8 @@ public class ClassPathExplorer { .getValue("Vaadin-Stylesheets"); if (value != null) { String[] stylesheets = value.split(","); - for (int i = 0; i < stylesheets.length; i++) { - String stylesheet = stylesheets[i].trim(); + for (String untrimmedStylesheet : stylesheets) { + String stylesheet = untrimmedStylesheet.trim(); if (!stylesheet.equals("")) { addonStyles.put(stylesheet, location); } @@ -326,8 +323,7 @@ public class ClassPathExplorer { debug("Classpath: " + classpath); String[] split = classpath.split(pathSep); - for (int i = 0; i < split.length; i++) { - String classpathEntry = split[i]; + for (String classpathEntry : split) { if (acceptClassPathEntry(classpathEntry)) { locations.add(classpathEntry); } @@ -459,20 +455,18 @@ public class ClassPathExplorer { // add all directories recursively File[] dirs = file.listFiles(DIRECTORIES_ONLY); - for (int i = 0; i < dirs.length; i++) { + for (File dir : dirs) { try { // add the present directory - if (!dirs[i].isHidden() - && !dirs[i].getPath().contains(File.separator + ".")) { - String key = dirs[i].getCanonicalPath() + "/" + name - + dirs[i].getName(); - URL url = dirs[i].getCanonicalFile().toURI().toURL(); + if (!dir.isHidden() && !dir.getPath().contains(File.separator + ".")) { + String key = dir.getCanonicalPath() + "/" + name + dir.getName(); + URL url = dir.getCanonicalFile().toURI().toURL(); locations.put(key, url); } - } catch (Exception ioe) { + }catch (Exception ioe) { return; } - include(name + dirs[i].getName(), dirs[i], locations); + include(name + dir.getName(), dir, locations); } } diff --git a/server/src/main/java/com/vaadin/ui/AbsoluteLayout.java b/server/src/main/java/com/vaadin/ui/AbsoluteLayout.java index 5aa008e829..cb0e1445ab 100644 --- a/server/src/main/java/com/vaadin/ui/AbsoluteLayout.java +++ b/server/src/main/java/com/vaadin/ui/AbsoluteLayout.java @@ -301,8 +301,8 @@ public class AbsoluteLayout extends AbstractLayout } String[] cssProperties = css.split(";"); - for (int i = 0; i < cssProperties.length; i++) { - String[] keyValuePair = cssProperties[i].split(":"); + for (String cssProperty : cssProperties) { + String[] keyValuePair = cssProperty.split(":"); String key = keyValuePair[0].trim(); if (key.equals("")) { continue; @@ -319,22 +319,27 @@ public class AbsoluteLayout extends AbstractLayout String symbol = value.replaceAll("[0-9\\.\\-]+", ""); if (!symbol.equals("")) { value = value.substring(0, value.indexOf(symbol)) - .trim(); + .trim(); } float v = Float.parseFloat(value); Unit unit = Unit.getUnitFromSymbol(symbol); - if (key.equals("top")) { - topValue = v; - topUnits = unit; - } else if (key.equals("right")) { - rightValue = v; - rightUnits = unit; - } else if (key.equals("bottom")) { - bottomValue = v; - bottomUnits = unit; - } else if (key.equals("left")) { - leftValue = v; - leftUnits = unit; + switch (key) { + case "top": + topValue = v; + topUnits = unit; + break; + case "right": + rightValue = v; + rightUnits = unit; + break; + case "bottom": + bottomValue = v; + bottomUnits = unit; + break; + case "left": + leftValue = v; + leftUnits = unit; + break; } } } diff --git a/server/src/main/java/com/vaadin/ui/Upload.java b/server/src/main/java/com/vaadin/ui/Upload.java index a08fce099e..155bdfb811 100644 --- a/server/src/main/java/com/vaadin/ui/Upload.java +++ b/server/src/main/java/com/vaadin/ui/Upload.java @@ -840,9 +840,7 @@ public class Upload extends AbstractComponent // this is implemented differently than other listeners to maintain // backwards compatibility if (progressListeners != null) { - for (Iterator it = progressListeners - .iterator(); it.hasNext();) { - ProgressListener l = it.next(); + for (ProgressListener l : progressListeners) { l.updateProgress(totalBytes, contentLength); } } diff --git a/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java b/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java index 44248d095b..2fad5fb14f 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java +++ b/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java @@ -318,11 +318,11 @@ public class DesignAttributeHandler implements Serializable { propertyName = removeSubsequentUppercase(propertyName); String[] words = propertyName.split("(? 0) { builder.append("-"); } - builder.append(words[i].toLowerCase()); + builder.append(word.toLowerCase()); } return builder.toString(); } diff --git a/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java b/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java index 0711ae24bf..ac530c266f 100644 --- a/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java +++ b/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java @@ -235,14 +235,15 @@ public class BinderConverterValidatorTest throw new IllegalArgumentException( "Value must be OK or NOTOK"); }, model -> { - if (model.equals("1")) { - return "OK"; - } else if (model.equals("2")) { - return "NOTOK"; - } else { - throw new IllegalArgumentException( - "Value in model must be 1 or 2"); - } + switch (model) { + case "1": + return "OK"; + case "2": + return "NOTOK"; + default: + throw new IllegalArgumentException( + "Value in model must be 1 or 2"); + } }); binding.bind(StatusBean::getStatus, StatusBean::setStatus); binder.setBean(bean); diff --git a/server/src/test/java/com/vaadin/tests/server/ClassesSerializableTest.java b/server/src/test/java/com/vaadin/tests/server/ClassesSerializableTest.java index 0a1629c2f9..1d30e41382 100644 --- a/server/src/test/java/com/vaadin/tests/server/ClassesSerializableTest.java +++ b/server/src/test/java/com/vaadin/tests/server/ClassesSerializableTest.java @@ -269,8 +269,7 @@ public class ClassesSerializableTest { } String[] split = classpath.split(pathSep); - for (int i = 0; i < split.length; i++) { - String classpathEntry = split[i]; + for (String classpathEntry : split) { locations.add(classpathEntry); } diff --git a/server/src/test/java/com/vaadin/tests/server/component/abstractcomponent/AbstractComponentDeclarativeTest.java b/server/src/test/java/com/vaadin/tests/server/component/abstractcomponent/AbstractComponentDeclarativeTest.java index b13499983e..b8875c34fd 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/abstractcomponent/AbstractComponentDeclarativeTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/abstractcomponent/AbstractComponentDeclarativeTest.java @@ -97,10 +97,8 @@ public class AbstractComponentDeclarativeTest Boolean[] explicitImmediate = { null, Boolean.FALSE, Boolean.TRUE, Boolean.TRUE }; boolean[] immediate = { true, false, true, true }; - for (int i = 0; i < design.length; i++) { - component = (AbstractComponent) Design - .read(new ByteArrayInputStream( - design[i].getBytes(Charset.forName("UTF-8")))); + for (String designElements : design) { + component = (AbstractComponent) Design.read(new ByteArrayInputStream(designElements.getBytes(Charset.forName("UTF-8")))); } }