]> source.dussan.org Git - vaadin-framework.git/commitdiff
Native collections for ApplicationConnection & StateChangeEvent (#10937)
authorLeif Åstrand <leif@vaadin.com>
Tue, 5 Feb 2013 11:06:07 +0000 (13:06 +0200)
committerVaadin Code Review <review@vaadin.com>
Wed, 6 Feb 2013 14:29:59 +0000 (14:29 +0000)
Change-Id: I8dbda413f2148f2a28e3867bffe0bc7ea3c8ff93

client/src/com/vaadin/client/ApplicationConnection.java
client/src/com/vaadin/client/communication/StateChangeEvent.java
client/src/com/vaadin/client/ui/AbstractComponentConnector.java
client/src/com/vaadin/client/ui/AbstractConnector.java
client/src/com/vaadin/client/ui/button/ButtonConnector.java
client/src/com/vaadin/client/ui/colorpicker/AbstractColorPickerConnector.java
client/src/com/vaadin/client/ui/colorpicker/ColorPickerGradientConnector.java
client/src/com/vaadin/client/ui/colorpicker/ColorPickerGridConnector.java

index b2a9541d80438718d26c06d227e2668173a90ff3..0171d541ea92f2fa52d8c51fa3b09bbdade8907a 100644 (file)
@@ -153,6 +153,9 @@ public class ApplicationConnection {
     // will hold the UIDL security key (for XSS protection) once received
     private String uidlSecurityKey = "init";
 
+    private static final FastStringMap<FastStringSet> allStateFieldsCache = FastStringMap
+            .create();
+
     private final HashMap<String, String> resourcesMap = new HashMap<String, String>();
 
     /**
@@ -1605,9 +1608,11 @@ public class ApplicationConnection {
                         Type stateType = AbstractConnector
                                 .getStateType(component);
 
-                        Set<String> changedProperties = sce
-                                .getChangedProperties();
-                        for (String propertyName : changedProperties) {
+                        FastStringSet changedProperties = sce
+                                .getChangedPropertiesFastSet();
+                        JsArrayString dump = changedProperties.dump();
+                        for (int i = 0; i < dump.length(); i++) {
+                            String propertyName = dump.get(i);
                             Property property = stateType
                                     .getProperty(propertyName);
                             String method = property
@@ -1670,9 +1675,11 @@ public class ApplicationConnection {
 
             private void unregisterRemovedConnectors() {
                 int unregistered = 0;
-                List<ServerConnector> currentConnectors = new ArrayList<ServerConnector>(
-                        connectorMap.getConnectors());
-                for (ServerConnector c : currentConnectors) {
+                JsArrayObject<ServerConnector> currentConnectors = connectorMap
+                        .getConnectorsAsJsArray();
+                int size = currentConnectors.size();
+                for (int i = 0; i < size; i++) {
+                    ServerConnector c = currentConnectors.get(i);
                     if (c.getParent() != null) {
                         if (!c.getParent().getChildren().contains(c)) {
                             VConsole.error("ERROR: Connector is connected to a parent but the parent does not contain the connector");
@@ -1856,17 +1863,17 @@ public class ApplicationConnection {
                                     .getName(), null), stateJson, state,
                                     ApplicationConnection.this);
 
-                            Set<String> changedProperties = new HashSet<String>();
+                            FastStringSet changedProperties = FastStringSet
+                                    .create();
                             addJsonFields(stateJson, changedProperties, "");
 
                             if (newConnectors.contains(connector)) {
                                 remainingNewConnectors.remove(connector);
                                 // Fire events for properties using the default
                                 // value for newly created connectors
-                                addAllStateFields(
-                                        AbstractConnector
-                                                .getStateType(connector),
-                                        changedProperties, "");
+                                FastStringSet allStateFields = getAllStateFields(AbstractConnector
+                                        .getStateType(connector));
+                                changedProperties.addAll(allStateFields);
                             }
 
                             StateChangeEvent event = new StateChangeEvent(
@@ -1882,10 +1889,8 @@ public class ApplicationConnection {
                 // Fire events for properties using the default value for newly
                 // created connectors even if there were no state changes
                 for (ServerConnector connector : remainingNewConnectors) {
-                    Set<String> changedProperties = new HashSet<String>();
-                    addAllStateFields(
-                            AbstractConnector.getStateType(connector),
-                            changedProperties, "");
+                    FastStringSet changedProperties = getAllStateFields(AbstractConnector
+                            .getStateType(connector));
 
                     StateChangeEvent event = new StateChangeEvent(connector,
                             changedProperties);
@@ -1897,6 +1902,17 @@ public class ApplicationConnection {
                 return events;
             }
 
+            private FastStringSet getAllStateFields(Type type) {
+                FastStringSet fields;
+                fields = allStateFieldsCache.get(type.getBaseTypeName());
+                if (fields == null) {
+                    fields = FastStringSet.create();
+                    addAllStateFields(type, fields, "");
+                    allStateFieldsCache.put(type.getBaseTypeName(), fields);
+                }
+                return fields;
+            }
+
             /**
              * Recursively adds the names of all properties in the provided
              * state type.
@@ -1909,7 +1925,7 @@ public class ApplicationConnection {
              *            the base name of the current object
              */
             private void addAllStateFields(Type type,
-                    Set<String> foundProperties, String context) {
+                    FastStringSet foundProperties, String context) {
                 try {
                     JsArrayObject<Property> properties = type
                             .getPropertiesAsArray();
@@ -1945,7 +1961,7 @@ public class ApplicationConnection {
              * @param context
              *            the base name of the current object
              */
-            private void addJsonFields(JSONObject json, Set<String> fields,
+            private void addJsonFields(JSONObject json, FastStringSet fields,
                     String context) {
                 for (String key : json.keySet()) {
                     String fieldName = context + key;
@@ -1978,7 +1994,7 @@ public class ApplicationConnection {
                     return result;
                 }
 
-                HashSet<ServerConnector> maybeDetached = new HashSet<ServerConnector>();
+                FastStringSet maybeDetached = FastStringSet.create();
 
                 ValueMap hierarchies = json.getValueMap("hierarchy");
                 JsArrayString hierarchyKeys = hierarchies.getKeyArray();
@@ -2022,7 +2038,7 @@ public class ApplicationConnection {
                                 result.parentChanged.add(childConnector);
                                 // Not detached even if previously removed from
                                 // parent
-                                maybeDetached.remove(childConnector);
+                                maybeDetached.remove(childConnectorId);
                             }
                         }
 
@@ -2078,7 +2094,7 @@ public class ApplicationConnection {
                                  * cleared if it is later on added to some other
                                  * parent.
                                  */
-                                maybeDetached.add(oldChild);
+                                maybeDetached.add(oldChild.getConnectorId());
                             }
                         }
                     } catch (final Throwable e) {
@@ -2090,7 +2106,10 @@ public class ApplicationConnection {
                  * Connector is in maybeDetached at this point if it has been
                  * removed from its parent but not added to any other parent
                  */
-                for (ServerConnector removed : maybeDetached) {
+                JsArrayString maybeDetachedArray = maybeDetached.dump();
+                for (int i = 0; i < maybeDetachedArray.length(); i++) {
+                    ServerConnector removed = connectorMap
+                            .getConnector(maybeDetachedArray.get(i));
                     recursivelyDetach(removed, result.events);
                 }
 
index 8e72485a0c3d00e70a82e0fa8f1a92e920dc45de..35187b03d45fde17676e3916114618952d964e32 100644 (file)
 package com.vaadin.client.communication;
 
 import java.io.Serializable;
-import java.util.Collections;
+import java.util.HashSet;
 import java.util.Set;
 
 import com.google.gwt.event.shared.EventHandler;
+import com.vaadin.client.FastStringSet;
 import com.vaadin.client.ServerConnector;
 import com.vaadin.client.communication.StateChangeEvent.StateChangeHandler;
 
@@ -30,13 +31,41 @@ public class StateChangeEvent extends
      */
     public static final Type<StateChangeHandler> TYPE = new Type<StateChangeHandler>();
 
-    private Set<String> changedProperties;
+    private final FastStringSet changedProperties;
+
+    /**
+     * Used to cache a Set representation of the changedProperties if one is
+     * needed.
+     */
+    private Set<String> changedPropertiesSet;
 
     @Override
     public Type<StateChangeHandler> getAssociatedType() {
         return TYPE;
     }
 
+    /**
+     * Creates a new state change event.
+     * 
+     * @param connector
+     *            the event whose state has changed
+     * @param changedPropertiesSet
+     *            a set of names of the changed properties
+     * @deprecated As of 7.0.1, use
+     *             {@link #StateChangeEvent(ServerConnector, FastStringSet)}
+     *             instead for improved performance.
+     */
+    @Deprecated
+    public StateChangeEvent(ServerConnector connector,
+            Set<String> changedPropertiesSet) {
+        setConnector(connector);
+        this.changedPropertiesSet = changedPropertiesSet;
+        changedProperties = FastStringSet.create();
+        for (String property : changedPropertiesSet) {
+            changedProperties.add(property);
+        }
+    }
+
     /**
      * Creates a new state change event.
      * 
@@ -46,7 +75,7 @@ public class StateChangeEvent extends
      *            a set of names of the changed properties
      */
     public StateChangeEvent(ServerConnector connector,
-            Set<String> changedProperties) {
+            FastStringSet changedProperties) {
         setConnector(connector);
         this.changedProperties = changedProperties;
     }
@@ -78,8 +107,39 @@ public class StateChangeEvent extends
      * Gets the properties that have changed.
      * 
      * @return a set of names of the changed properties
+     * 
+     * @deprecated As of 7.0.1, use {@link #getChangedPropertiesFastSet()} or
+     *             {@link #hasPropertyChanged(String)} instead for improved
+     *             performance.
      */
+    @Deprecated
     public Set<String> getChangedProperties() {
-        return Collections.unmodifiableSet(changedProperties);
+        if (changedPropertiesSet == null) {
+            changedPropertiesSet = new HashSet<String>();
+            changedProperties.addAllTo(changedPropertiesSet);
+        }
+        return changedPropertiesSet;
+    }
+
+    /**
+     * Gets the properties that have changed.
+     * 
+     * @return a set of names of the changed properties
+     * 
+     */
+    public FastStringSet getChangedPropertiesFastSet() {
+        return changedProperties;
+    }
+
+    /**
+     * Checks whether the give property has changed.
+     * 
+     * @param property
+     *            the name of the property to check
+     * @return <code>true</code> if the property has changed, else
+     *         <code>false></code>
+     */
+    public boolean hasPropertyChanged(String property) {
+        return changedProperties.contains(property);
     }
 }
index c22bd0905c8d2c1224365ecb13fb198ec785e15e..e35e3eafcfc073351d7a8a1361d32a4d4b50fa8e 100644 (file)
@@ -17,7 +17,6 @@ package com.vaadin.client.ui;
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Set;
 
 import com.google.gwt.core.client.JsArrayString;
 import com.google.gwt.dom.client.Element;
@@ -26,7 +25,6 @@ import com.google.gwt.user.client.ui.HasEnabled;
 import com.google.gwt.user.client.ui.Widget;
 import com.vaadin.client.ApplicationConnection;
 import com.vaadin.client.ComponentConnector;
-import com.vaadin.client.ConnectorMap;
 import com.vaadin.client.HasComponentsConnector;
 import com.vaadin.client.LayoutManager;
 import com.vaadin.client.ServerConnector;
@@ -125,10 +123,7 @@ public abstract class AbstractComponentConnector extends AbstractConnector
 
     @Override
     public void onStateChanged(StateChangeEvent stateChangeEvent) {
-        ConnectorMap paintableMap = ConnectorMap.get(getConnection());
-
-        Set<String> changedProperties = stateChangeEvent.getChangedProperties();
-        if (changedProperties.contains("id")) {
+        if (stateChangeEvent.hasPropertyChanged("id")) {
             if (getState().id != null) {
                 getWidget().getElement().setId(getState().id);
             } else if (!initialStateEvent) {
@@ -425,6 +420,7 @@ public abstract class AbstractComponentConnector extends AbstractConnector
      * 
      * @see com.vaadin.client.ComponentConnector#flush()
      */
+    @Override
     public void flush() {
         // No generic implementation. Override if needed
     }
index 4c55f999b60a0cdcb4193a4b9d1f6ded7f4118ed..2791cfddd925d34396acc38226d55817692cecb1 100644 (file)
@@ -23,10 +23,12 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import com.google.gwt.core.client.JsArrayString;
 import com.google.gwt.event.shared.GwtEvent;
 import com.google.gwt.event.shared.HandlerManager;
 import com.google.web.bindery.event.shared.HandlerRegistration;
 import com.vaadin.client.ApplicationConnection;
+import com.vaadin.client.FastStringMap;
 import com.vaadin.client.ServerConnector;
 import com.vaadin.client.Util;
 import com.vaadin.client.VConsole;
@@ -55,7 +57,7 @@ public abstract class AbstractConnector implements ServerConnector,
     private String id;
 
     private HandlerManager handlerManager;
-    private Map<String, HandlerManager> statePropertyHandlerManagers;
+    private FastStringMap<HandlerManager> statePropertyHandlerManagers;
     private Map<String, Collection<ClientRpc>> rpcImplementations;
     private final boolean debugLogging = false;
 
@@ -202,12 +204,12 @@ public abstract class AbstractConnector implements ServerConnector,
         }
         if (statePropertyHandlerManagers != null
                 && event instanceof StateChangeEvent) {
-            for (String property : ((StateChangeEvent) event)
-                    .getChangedProperties()) {
-                HandlerManager manager = statePropertyHandlerManagers
-                        .get(property);
-                if (manager != null) {
-                    manager.fireEvent(event);
+            StateChangeEvent stateChangeEvent = (StateChangeEvent) event;
+            JsArrayString keys = statePropertyHandlerManagers.getKeys();
+            for (int i = 0; i < keys.length(); i++) {
+                String property = keys.get(i);
+                if (stateChangeEvent.hasPropertyChanged(property)) {
+                    statePropertyHandlerManagers.get(property).fireEvent(event);
                 }
             }
         }
@@ -248,7 +250,7 @@ public abstract class AbstractConnector implements ServerConnector,
 
     private HandlerManager ensureHandlerManager(String propertyName) {
         if (statePropertyHandlerManagers == null) {
-            statePropertyHandlerManagers = new HashMap<String, HandlerManager>();
+            statePropertyHandlerManagers = FastStringMap.create();
         }
         HandlerManager manager = statePropertyHandlerManagers.get(propertyName);
         if (manager == null) {
index 224da40b5e6aaad8a9dbb04b95ab1a536bfea98f..0439cd4fdc49bb2d4bef3bd6e83359f543e387ca 100644 (file)
@@ -16,8 +16,6 @@
 
 package com.vaadin.client.ui.button;
 
-import java.util.Set;
-
 import com.google.gwt.event.dom.client.BlurEvent;
 import com.google.gwt.event.dom.client.BlurHandler;
 import com.google.gwt.event.dom.client.ClickEvent;
@@ -109,9 +107,8 @@ public class ButtonConnector extends AbstractComponentConnector implements
         blurHandlerRegistration = EventHelper.updateBlurHandler(this,
                 blurHandlerRegistration);
 
-        Set<String> changedProperties = stateChangeEvent.getChangedProperties();
-        if (changedProperties.contains("caption")
-                || changedProperties.contains("htmlContentAllowed")) {
+        if (stateChangeEvent.hasPropertyChanged("caption")
+                || stateChangeEvent.hasPropertyChanged("htmlContentAllowed")) {
             // Set text
             if (getState().htmlContentAllowed) {
                 getWidget().setHtml(getState().caption);
index 777a7eba8f9be2fb4d4ed72249477fd0f6895b1d..ba0575d8fed4048c832dff6c9697e5cfdb390f08 100644 (file)
@@ -15,8 +15,6 @@
  */
 package com.vaadin.client.ui.colorpicker;
 
-import java.util.Set;
-
 import com.google.gwt.event.dom.client.ClickHandler;
 import com.google.gwt.event.dom.client.HasClickHandlers;
 import com.vaadin.client.communication.StateChangeEvent;
@@ -46,8 +44,7 @@ public abstract class AbstractColorPickerConnector extends
     public void onStateChanged(StateChangeEvent stateChangeEvent) {
         // NOTE: this method is called after @DelegateToWidget
         super.onStateChanged(stateChangeEvent);
-        Set<String> changedProperties = stateChangeEvent.getChangedProperties();
-        if (changedProperties.contains("color")) {
+        if (stateChangeEvent.hasPropertyChanged("color")) {
             refreshColor();
 
             if (getState().showDefaultCaption
@@ -57,9 +54,9 @@ public abstract class AbstractColorPickerConnector extends
                 setCaption(getState().color);
             }
         }
-        if (changedProperties.contains("caption")
-                || changedProperties.contains("htmlContentAllowed")
-                || changedProperties.contains("showDefaultCaption")) {
+        if (stateChangeEvent.hasPropertyChanged("caption")
+                || stateChangeEvent.hasPropertyChanged("htmlContentAllowed")
+                || stateChangeEvent.hasPropertyChanged("showDefaultCaption")) {
 
             setCaption(getCaption());
         }
index e381eb0b704a05f14aa5ef6f40e4cb4d4b42b9df..b28831b860427e41014cea8fec2d8ed5684793d0 100644 (file)
@@ -15,8 +15,6 @@
  */
 package com.vaadin.client.ui.colorpicker;
 
-import java.util.Set;
-
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.event.dom.client.MouseUpEvent;
 import com.google.gwt.event.dom.client.MouseUpHandler;
@@ -68,13 +66,12 @@ public class ColorPickerGradientConnector extends AbstractComponentConnector
     @Override
     public void onStateChanged(StateChangeEvent stateChangeEvent) {
         super.onStateChanged(stateChangeEvent);
-        Set<String> changedProperties = stateChangeEvent.getChangedProperties();
-        if (changedProperties.contains("cursorX")
-                || changedProperties.contains("cursorY")) {
+        if (stateChangeEvent.hasPropertyChanged("cursorX")
+                || stateChangeEvent.hasPropertyChanged("cursorY")) {
 
             getWidget().setCursor(getState().cursorX, getState().cursorY);
         }
-        if (changedProperties.contains("bgColor")) {
+        if (stateChangeEvent.hasPropertyChanged("bgColor")) {
             getWidget().setBGColor(getState().bgColor);
         }
     }
index 53abc78b4bd35c6a688d00b99f5ad8d6703d9890..730981d2dd814d26f3c907d0e22dece4b8ee9203 100644 (file)
@@ -15,8 +15,6 @@
  */
 package com.vaadin.client.ui.colorpicker;
 
-import java.util.Set;
-
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.event.dom.client.ClickEvent;
 import com.google.gwt.event.dom.client.ClickHandler;
@@ -68,17 +66,16 @@ public class ColorPickerGridConnector extends AbstractComponentConnector
     @Override
     public void onStateChanged(StateChangeEvent stateChangeEvent) {
         super.onStateChanged(stateChangeEvent);
-        Set<String> changedProperties = stateChangeEvent.getChangedProperties();
-        if (changedProperties.contains("rowCount")
-                || changedProperties.contains("columnCount")
-                || changedProperties.contains("updateGrid")) {
+        if (stateChangeEvent.hasPropertyChanged("rowCount")
+                || stateChangeEvent.hasPropertyChanged("columnCount")
+                || stateChangeEvent.hasPropertyChanged("updateGrid")) {
 
             getWidget().updateGrid(getState().rowCount, getState().columnCount);
         }
-        if (changedProperties.contains("changedX")
-                || changedProperties.contains("changedY")
-                || changedProperties.contains("changedColor")
-                || changedProperties.contains("updateColor")) {
+        if (stateChangeEvent.hasPropertyChanged("changedX")
+                || stateChangeEvent.hasPropertyChanged("changedY")
+                || stateChangeEvent.hasPropertyChanged("changedColor")
+                || stateChangeEvent.hasPropertyChanged("updateColor")) {
 
             getWidget().updateColor(getState().changedColor,
                     getState().changedX, getState().changedY);