/protocols/smpp/src/main/java/org/mobicents/protocols/smpp/event/AbstractEventDispatcher.java

http://mobicents.googlecode.com/ · Java · 86 lines · 44 code · 12 blank · 30 comment · 2 complexity · 631679d59cd7101e31ab15cdfd61aa81 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.protocols.smpp.event;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.Iterator;
  27. import java.util.List;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. /**
  31. * Parent class for event dispatchers that maintain their observers in a
  32. * <code>java.util.List</code>.
  33. * @version $Id: AbstractEventDispatcher.java 452 2009-01-15 16:56:36Z orank $
  34. */
  35. public abstract class AbstractEventDispatcher implements EventDispatcher {
  36. private static final Logger LOG = LoggerFactory.getLogger(AbstractEventDispatcher.class);
  37. private List<SessionObserver> observers =
  38. new ArrayList<SessionObserver>();
  39. public void addObserver(SessionObserver observer) {
  40. synchronized (observers) {
  41. if (!observers.contains(observer)) {
  42. observers.add(observer);
  43. } else {
  44. LOG.info("Not adding observer because it's already registered");
  45. }
  46. }
  47. }
  48. public void removeObserver(SessionObserver observer) {
  49. synchronized (observers) {
  50. observers.remove(observer);
  51. }
  52. }
  53. public Collection<SessionObserver> getObservers() {
  54. return Collections.unmodifiableCollection(observers);
  55. }
  56. public Iterator<SessionObserver> observerIterator() {
  57. return Collections.unmodifiableList(observers).iterator();
  58. }
  59. public boolean contains(SessionObserver observer) {
  60. return observers.contains(observer);
  61. }
  62. public int size() {
  63. return observers.size();
  64. }
  65. /**
  66. * Get the list of observers as an array.
  67. * @return An array of all registered observers.
  68. */
  69. protected SessionObserver[] getObserverList() {
  70. SessionObserver[] observerList =
  71. observers.toArray(new SessionObserver[0]);
  72. return observerList;
  73. }
  74. }