diff options
author | Fabian Lange <lange.fabian@gmail.com> | 2014-07-03 23:52:05 +0200 |
---|---|---|
committer | Fabian Lange <lange.fabian@gmail.com> | 2014-07-04 12:11:50 +0200 |
commit | a3578d3ac293442b64cbc5f5feb4cb9106799fdb (patch) | |
tree | 7fd80df9f84ac6ea14a2078fc266bdd1f3745526 /server/src | |
parent | 3db6df2eabcb7c3d9461ea6ff2b89b95cf6446d3 (diff) | |
download | vaadin-framework-a3578d3ac293442b64cbc5f5feb4cb9106799fdb.tar.gz vaadin-framework-a3578d3ac293442b64cbc5f5feb4cb9106799fdb.zip |
Avoid object creation in getAllChildrenIterable in most cases. (#14142)
API change from:
public static Iterable<ClientConnector> getAllChildrenIterable(final ClientConnector connector)
to
public static Iterable<? extends ClientConnector> getAllChildrenIterable(final ClientConnector connector)
avoids creating wrappers in case the component has either no subcomponents
or no extensions. This covers the vast majority of components.
Change-Id: I48ffd2f26f09c265fae6e1aaabdbaa655d52ffb8
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/com/vaadin/server/AbstractClientConnector.java | 118 | ||||
-rw-r--r-- | server/src/com/vaadin/ui/ConnectorTracker.java | 2 |
2 files changed, 57 insertions, 63 deletions
diff --git a/server/src/com/vaadin/server/AbstractClientConnector.java b/server/src/com/vaadin/server/AbstractClientConnector.java index bafecdabf4..03300b20e2 100644 --- a/server/src/com/vaadin/server/AbstractClientConnector.java +++ b/server/src/com/vaadin/server/AbstractClientConnector.java @@ -41,6 +41,7 @@ import com.vaadin.shared.communication.ClientRpc; import com.vaadin.shared.communication.ServerRpc; import com.vaadin.shared.communication.SharedState; import com.vaadin.shared.ui.ComponentStateUtil; +import com.vaadin.ui.Component; import com.vaadin.ui.Component.Event; import com.vaadin.ui.HasComponents; import com.vaadin.ui.LegacyComponent; @@ -339,31 +340,6 @@ public abstract class AbstractClientConnector implements ClientConnector, } } - private static final class AllChildrenIterable implements - Iterable<ClientConnector>, Serializable { - private final ClientConnector connector; - - private AllChildrenIterable(ClientConnector connector) { - this.connector = connector; - } - - @Override - public Iterator<ClientConnector> iterator() { - CombinedIterator<ClientConnector> iterator = new CombinedIterator<ClientConnector>(); - - if (connector instanceof HasComponents) { - HasComponents hasComponents = (HasComponents) connector; - iterator.addIterator(hasComponents.iterator()); - } - - Collection<Extension> extensions = connector.getExtensions(); - if (extensions.size() > 0) { - iterator.addIterator(extensions.iterator()); - } - return iterator; - } - } - private class RpcInvocationHandler implements InvocationHandler, Serializable { @@ -493,41 +469,6 @@ public abstract class AbstractClientConnector implements ClientConnector, } } - private static final class CombinedIterator<T> implements Iterator<T>, - Serializable { - - private final Collection<Iterator<? extends T>> iterators = new ArrayList<Iterator<? extends T>>(); - - public void addIterator(Iterator<? extends T> iterator) { - iterators.add(iterator); - } - - @Override - public boolean hasNext() { - for (Iterator<? extends T> i : iterators) { - if (i.hasNext()) { - return true; - } - } - return false; - } - - @Override - public T next() { - for (Iterator<? extends T> i : iterators) { - if (i.hasNext()) { - return i.next(); - } - } - throw new NoSuchElementException(); - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - } - /** * Get an Iterable for iterating over all child connectors, including both * extensions and child components. @@ -536,9 +477,62 @@ public abstract class AbstractClientConnector implements ClientConnector, * the connector to get children for * @return an Iterable giving all child connectors. */ - public static Iterable<ClientConnector> getAllChildrenIterable( + public static Iterable<? extends ClientConnector> getAllChildrenIterable( final ClientConnector connector) { - return new AllChildrenIterable(connector); + + Collection<Extension> extensions = connector.getExtensions(); + boolean hasComponents = connector instanceof HasComponents; + boolean hasExtensions = extensions.size() > 0; + if (!hasComponents && !hasExtensions) { + // If has neither component nor extensions, return immutable empty + // list as iterable. + return Collections.emptyList(); + } + if (hasComponents && !hasExtensions) { + // only components + return (HasComponents) connector; + } + if (!hasComponents && hasExtensions) { + // only extensions + return extensions; + } + + // combine the iterators of extensions and components to a new iterable. + final Iterator<Component> componentsIterator = ((HasComponents) connector) + .iterator(); + final Iterator<Extension> extensionsIterator = extensions.iterator(); + Iterable<? extends ClientConnector> combinedIterable = new Iterable<ClientConnector>() { + + @Override + public Iterator<ClientConnector> iterator() { + return new Iterator<ClientConnector>() { + + @Override + public boolean hasNext() { + return componentsIterator.hasNext() + || extensionsIterator.hasNext(); + } + + @Override + public ClientConnector next() { + if (componentsIterator.hasNext()) { + return componentsIterator.next(); + } + if (extensionsIterator.hasNext()) { + return extensionsIterator.next(); + } + throw new NoSuchElementException(); + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + + }; + } + }; + return combinedIterable; } @Override diff --git a/server/src/com/vaadin/ui/ConnectorTracker.java b/server/src/com/vaadin/ui/ConnectorTracker.java index c0f973106b..b5a0227d99 100644 --- a/server/src/com/vaadin/ui/ConnectorTracker.java +++ b/server/src/com/vaadin/ui/ConnectorTracker.java @@ -366,7 +366,7 @@ public class ConnectorTracker implements Serializable { ClientConnector connector = stack.pop(); danglingConnectors.remove(connector); - Iterable<ClientConnector> children = AbstractClientConnector + Iterable<? extends ClientConnector> children = AbstractClientConnector .getAllChildrenIterable(connector); for (ClientConnector child : children) { stack.add(child); |