This is an automated email from the ASF dual-hosted git repository.

theigl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git


The following commit(s) were added to refs/heads/master by this push:
     new 887cc75  WICKET-6830 Convert `Behaviors` into a static utility class 
to avoid allocating a new object on every invocation
     new d18ea34  Merge pull request #452 from 
theigl/WICKET-6830-static-behaviors
887cc75 is described below

commit 887cc751158df571d5778afb61484bbb564a8309
Author: Thomas Heigl <thomas.he...@gmail.com>
AuthorDate: Sun Sep 13 11:21:25 2020 +0200

    WICKET-6830 Convert `Behaviors` into a static utility class to avoid 
allocating a new object on every invocation
---
 .../src/main/java/org/apache/wicket/Behaviors.java | 52 ++++++++++------------
 .../src/main/java/org/apache/wicket/Component.java | 17 ++++---
 2 files changed, 32 insertions(+), 37 deletions(-)

diff --git a/wicket-core/src/main/java/org/apache/wicket/Behaviors.java 
b/wicket-core/src/main/java/org/apache/wicket/Behaviors.java
index 0573ec5..fc99c6c 100644
--- a/wicket-core/src/main/java/org/apache/wicket/Behaviors.java
+++ b/wicket-core/src/main/java/org/apache/wicket/Behaviors.java
@@ -22,25 +22,22 @@ import java.util.List;
 
 import org.apache.wicket.behavior.Behavior;
 import org.apache.wicket.behavior.InvalidBehaviorIdException;
-import org.apache.wicket.model.IDetachable;
 import org.apache.wicket.util.lang.Args;
 
 /**
- * Manages behaviors in a {@link Component} instance
+ * Manages behaviors for {@link Component} instances
  * 
  * @author igor
  */
-final class Behaviors implements IDetachable
+final class Behaviors
 {
-       private static final long serialVersionUID = 1L;
-       private final Component component;
 
-       public Behaviors(Component component)
+       private Behaviors()
        {
-               this.component = component;
+               // utility class
        }
 
-       public void add(Behavior... behaviors)
+       public static void add(Component component, Behavior... behaviors)
        {
                Args.notNull(behaviors, "behaviors");
 
@@ -48,7 +45,7 @@ final class Behaviors implements IDetachable
                {
                        Args.notNull(behavior, "behavior");
 
-                       internalAdd(behavior);
+                       internalAdd(component, behavior);
 
                        if (!behavior.isTemporary(component))
                        {
@@ -60,17 +57,17 @@ final class Behaviors implements IDetachable
                }
        }
 
-       private void internalAdd(final Behavior behavior)
+       private static void internalAdd(Component component, Behavior behavior)
        {
                component.data_add(behavior);
                if (behavior.getStatelessHint(component) == false)
                {
-                       getBehaviorId(behavior);
+                       getBehaviorId(component, behavior);
                }
        }
 
        @SuppressWarnings("unchecked")
-       public <M extends Behavior> List<M> getBehaviors(Class<M> type)
+       public static <M extends Behavior> List<M> getBehaviors(Component 
component, Class<M> type)
        {
                int len = component.data_length();
                if (len == 0)
@@ -110,11 +107,11 @@ final class Behaviors implements IDetachable
        }
 
 
-       public void remove(Behavior behavior)
+       public static void remove(Component component, Behavior behavior)
        {
                Args.notNull(behavior, "behavior");
 
-               if (internalRemove(behavior))
+               if (internalRemove(component, behavior))
                {
                        if (!behavior.isTemporary(component))
                        {
@@ -138,8 +135,7 @@ final class Behaviors implements IDetachable
         * component's behaviors after header contribution has been done (which 
is separated from
         * component render).
         */
-       @Override
-       public final void detach()
+       public static void detach(Component component)
        {
                int len = component.data_length();
                if (len == 0)
@@ -162,7 +158,7 @@ final class Behaviors implements IDetachable
 
                                if (behavior.isTemporary(component))
                                {
-                                       internalRemove(behavior);
+                                       internalRemove(component, behavior);
                                        i--;
                                        len--;
                                }
@@ -170,7 +166,7 @@ final class Behaviors implements IDetachable
                }
        }
 
-       private boolean internalRemove(final Behavior behavior)
+       private static boolean internalRemove(Component component, Behavior 
behavior)
        {
                final int len = component.data_length();
                for (int i = component.data_start(); i < len; i++)
@@ -182,7 +178,7 @@ final class Behaviors implements IDetachable
                                behavior.unbind(component);
 
                                // remove behavior from behavior-ids
-                               ArrayList<Behavior> ids = 
getBehaviorsIdList(false);
+                               ArrayList<Behavior> ids = 
getBehaviorsIdList(component, false);
                                if (ids != null)
                                {
                                        int idx = ids.indexOf(behavior);
@@ -198,7 +194,7 @@ final class Behaviors implements IDetachable
 
                                        if (ids.isEmpty())
                                        {
-                                               removeBehaviorsIdList();
+                                               
removeBehaviorsIdList(component);
                                        }
 
                                }
@@ -208,7 +204,7 @@ final class Behaviors implements IDetachable
                return false;
        }
 
-       private void removeBehaviorsIdList()
+       private static void removeBehaviorsIdList(Component component)
        {
                for (int i = component.data_start(); i < 
component.data_length(); i++)
                {
@@ -221,7 +217,7 @@ final class Behaviors implements IDetachable
                }
        }
 
-       private BehaviorIdList getBehaviorsIdList(boolean createIfNotFound)
+       private static BehaviorIdList getBehaviorsIdList(Component component, 
boolean createIfNotFound)
        {
                int len = component.data_length();
                for (int i = component.data_start(); i < len; i++)
@@ -248,7 +244,7 @@ final class Behaviors implements IDetachable
         * @param component
         *      the component that will be removed from its parent
         */
-       public void onRemove(Component component)
+       public static void onRemove(Component component)
        {
                int len = component.data_length();
                if (len == 0)
@@ -282,7 +278,7 @@ final class Behaviors implements IDetachable
                }
        }
 
-       public final int getBehaviorId(Behavior behavior)
+       public static int getBehaviorId(Component component, Behavior behavior)
        {
                Args.notNull(behavior, "behavior");
 
@@ -299,10 +295,10 @@ final class Behaviors implements IDetachable
                {
                        throw new IllegalStateException(
                                "Behavior must be added to component before its 
id can be generated. Behavior: " +
-                                       behavior + ", Component: " + this);
+                                       behavior + ", Component: " + component);
                }
 
-               ArrayList<Behavior> ids = getBehaviorsIdList(true);
+               ArrayList<Behavior> ids = getBehaviorsIdList(component, true);
 
                int id = ids.indexOf(behavior);
 
@@ -331,11 +327,11 @@ final class Behaviors implements IDetachable
                return id;
        }
 
-       public final Behavior getBehaviorById(int id)
+       public static Behavior getBehaviorById(Component component, int id)
        {
                Behavior behavior = null;
 
-               ArrayList<Behavior> ids = getBehaviorsIdList(false);
+               ArrayList<Behavior> ids = getBehaviorsIdList(component, false);
                if (ids != null)
                {
                        if (id >= 0 && id < ids.size())
diff --git a/wicket-core/src/main/java/org/apache/wicket/Component.java 
b/wicket-core/src/main/java/org/apache/wicket/Component.java
index 423355c..ca787c9 100644
--- a/wicket-core/src/main/java/org/apache/wicket/Component.java
+++ b/wicket-core/src/main/java/org/apache/wicket/Component.java
@@ -1086,11 +1086,11 @@ public abstract class Component
                if (getRequestFlag(RFLAG_REMOVING_FROM_HIERARCHY))
                {
                        throw new 
IllegalStateException(Component.class.getName() +
-                               " has not been properly removed from hierachy. 
Something in the hierarchy of " +
+                               " has not been properly removed from hierarchy. 
Something in the hierarchy of " +
                                getClass().getName() +
                                " has not called super.onRemove() in the 
override of onRemove() method");
                }
-               new Behaviors(this).onRemove(this);
+               Behaviors.onRemove(this);
                removeChildren();
        }
 
@@ -1119,7 +1119,7 @@ public abstract class Component
                        detachModels();
 
                        // detach any behaviors
-                       new Behaviors(this).detach();
+                       Behaviors.detach(this);
                }
                catch (Exception x)
                {
@@ -3629,7 +3629,7 @@ public abstract class Component
         */
        public <M extends Behavior> List<M> getBehaviors(Class<M> type)
        {
-               return new Behaviors(this).getBehaviors(type);
+               return Behaviors.getBehaviors(this, type);
        }
 
        /**
@@ -4438,10 +4438,9 @@ public abstract class Component
         */
        public Component remove(final Behavior... behaviors)
        {
-               Behaviors helper = new Behaviors(this);
                for (Behavior behavior : behaviors)
                {
-                       helper.remove(behavior);
+                       Behaviors.remove(this, behavior);
                }
                return this;
        }
@@ -4450,7 +4449,7 @@ public abstract class Component
        @Override
        public final Behavior getBehaviorById(int id)
        {
-               return new Behaviors(this).getBehaviorById(id);
+               return Behaviors.getBehaviorById(this, id);
        }
 
        /** {@inheritDoc} */
@@ -4462,7 +4461,7 @@ public abstract class Component
                        throw new IllegalArgumentException(
                                "Cannot get a stable id for temporary behavior 
" + behavior);
                }
-               return new Behaviors(this).getBehaviorId(behavior);
+               return Behaviors.getBehaviorId(this, behavior);
        }
 
        /**
@@ -4474,7 +4473,7 @@ public abstract class Component
         */
        public Component add(final Behavior... behaviors)
        {
-               new Behaviors(this).add(behaviors);
+               Behaviors.add(this, behaviors);
                return this;
        }
 

Reply via email to