Daniel Kurka has submitted this change and it was merged.

Change subject: Utility to store multiple HandlerRegistrations
......................................................................


Utility to store multiple HandlerRegistrations

fixes issue 8246

Change-Id: I75ccd120b78e1be47fa6a1e4f20c607c68203c31
---
A user/src/com/google/web/bindery/event/shared/HandlerRegistrations.java
A user/test/com/google/web/bindery/event/shared/HandlerRegistrationsTest.java
2 files changed, 118 insertions(+), 0 deletions(-)

Approvals:
  Leeroy Jenkins: Verified
  Thomas Broyer: Looks good to me, approved
  Goktug Gokdogan: Looks good to me, but someone else must approve



diff --git a/user/src/com/google/web/bindery/event/shared/HandlerRegistrations.java b/user/src/com/google/web/bindery/event/shared/HandlerRegistrations.java
new file mode 100644
index 0000000..e64c3a7
--- /dev/null
+++ b/user/src/com/google/web/bindery/event/shared/HandlerRegistrations.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.web.bindery.event.shared;
+
+/**
+ * A utility class to help deal with {@link HandlerRegistration handler registrations}.
+ */
+public class HandlerRegistrations {
+
+ private static class HandlerRegistrationCollection implements HandlerRegistration {
+
+    private HandlerRegistration[] handlers;
+
+    public HandlerRegistrationCollection(HandlerRegistration... handlers) {
+      this.handlers = handlers;
+    }
+
+    @Override
+    public void removeHandler() {
+      if (handlers == null) {
+        return;
+      }
+      for (HandlerRegistration hr : handlers) {
+        hr.removeHandler();
+      }
+      // make sure we remove the handlers to avoid potential leaks
+      // if someone fails to null out their reference to us
+      handlers = null;
+    }
+  }
+
+  /**
+   * Create and return a {@link HandlerRegistration} that will call
+ * {@link HandlerRegistration#removeHandler()} on all supplied handlers if + * {@link HandlerRegistration#removeHandler()} is called on the returned object.
+   *
+   * <p>
+   * A simple example: <code><pre>
+   * HandlerRegistration hr1 = ...
+   * HandlerRegistration hr2 = ...
+   * return HandlerRegistrations.compose(hr1, hr2);
+   * </pre></code>
+   *
+ * @param handlers the {@link HandlerRegistration handler registrations} that should be composed
+   *          into a single {@link HandlerRegistration}
+   * @return the composed {@link HandlerRegistration}
+   */
+ public static HandlerRegistration compose(HandlerRegistration... handlers) {
+    return new HandlerRegistrationCollection(handlers);
+  }
+
+  /**
+   * For know this is a utility class, make it not instantiable.
+   */
+  private HandlerRegistrations() {
+  }
+}
diff --git a/user/test/com/google/web/bindery/event/shared/HandlerRegistrationsTest.java b/user/test/com/google/web/bindery/event/shared/HandlerRegistrationsTest.java
new file mode 100644
index 0000000..305d02b
--- /dev/null
+++ b/user/test/com/google/web/bindery/event/shared/HandlerRegistrationsTest.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.web.bindery.event.shared;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit test for {@link HandlerRegistrations}.
+ */
+public class HandlerRegistrationsTest extends TestCase {
+
+ private static class MockHandlerRegistration implements HandlerRegistration {
+
+    private int removeHandlerCallCount = 0;
+
+    @Override
+    public void removeHandler() {
+      removeHandlerCallCount++;
+    }
+  }
+
+  public void testHandlerRegistrationCollectionRemove() {
+    MockHandlerRegistration hr1 = new MockHandlerRegistration();
+    MockHandlerRegistration hr2 = new MockHandlerRegistration();
+
+    HandlerRegistration hrc = HandlerRegistrations.compose(hr1, hr2);
+    hrc.removeHandler();
+
+    // every handler registration needs to be called once
+    assertEquals(1, hr1.removeHandlerCallCount);
+    assertEquals(1, hr2.removeHandlerCallCount);
+
+    hrc.removeHandler();
+    // should not be called again
+    assertEquals(1, hr1.removeHandlerCallCount);
+    assertEquals(1, hr2.removeHandlerCallCount);
+  }
+
+}

--
To view, visit https://gwt-review.googlesource.com/3661
To unsubscribe, visit https://gwt-review.googlesource.com/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I75ccd120b78e1be47fa6a1e4f20c607c68203c31
Gerrit-PatchSet: 6
Gerrit-Project: gwt
Gerrit-Branch: master
Gerrit-Owner: Daniel Kurka <[email protected]>
Gerrit-Reviewer: Daniel Kurka <[email protected]>
Gerrit-Reviewer: Goktug Gokdogan <[email protected]>
Gerrit-Reviewer: Jens Nehlmeier <[email protected]>
Gerrit-Reviewer: Leeroy Jenkins <[email protected]>
Gerrit-Reviewer: Thomas Broyer <[email protected]>

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors
--- You received this message because you are subscribed to the Google Groups "GWT Contributors" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to