Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ChildEventBusManager.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.util;
  23. import net.engio.mbassy.bus.MBassador;
  24. import net.engio.mbassy.bus.config.BusConfiguration;
  25. import net.engio.mbassy.listener.Handler;
  26. import static com.google.common.base.Preconditions.checkNotNull;
  27. /**
  28. * Utility for creating and managing instances of {@link net.engio.mbassy.bus.MBassador} that are slaved to a parent bus.
  29. * <p>
  30. * Any events sent on the child bus will be propagated up to the parent bus.
  31. */
  32. public class ChildEventBusManager {
  33. private final MBassador parent;
  34. private final MBassador child;
  35. private final EventPropagator propagator;
  36. public ChildEventBusManager(final MBassador parent) {
  37. this.parent = checkNotNull(parent);
  38. this.child = new MBassador(BusConfiguration.Default());
  39. this.propagator = new EventPropagator();
  40. }
  41. /**
  42. * Connects the child event bus to the parent.
  43. * <p>
  44. * After this method is called, all events sent to the child bus will be passed to the parent.
  45. */
  46. public void connect() {
  47. child.subscribe(propagator);
  48. }
  49. /**
  50. * Disconnects the child event bus from the parent.
  51. * <p>
  52. * After this method is called, no further events will be passed to the parent.
  53. */
  54. public void disconnect() {
  55. child.unsubscribe(propagator);
  56. }
  57. /**
  58. * Gets the child bus.
  59. *
  60. * @return The child bus belonging to this manager.
  61. */
  62. public MBassador getChildBus() {
  63. return child;
  64. }
  65. private class EventPropagator {
  66. @Handler
  67. public void handleEvent(final Object object) {
  68. parent.post(object);
  69. }
  70. }
  71. }