PageRenderTime 45ms CodeModel.GetById 29ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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
23package org.mobicents.protocols.smpp.event;
24
25import java.util.ArrayList;
26import java.util.Collection;
27import java.util.Collections;
28import java.util.Iterator;
29import java.util.List;
30
31import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
34/**
35 * Parent class for event dispatchers that maintain their observers in a
36 * <code>java.util.List</code>.
37 * @version $Id: AbstractEventDispatcher.java 452 2009-01-15 16:56:36Z orank $
38 */
39public abstract class AbstractEventDispatcher implements EventDispatcher {
40    private static final Logger LOG = LoggerFactory.getLogger(AbstractEventDispatcher.class);
41    
42    private List<SessionObserver> observers =
43        new ArrayList<SessionObserver>();
44
45    public void addObserver(SessionObserver observer) {
46        synchronized (observers) {
47            if (!observers.contains(observer)) {
48                observers.add(observer);
49            } else {
50                LOG.info("Not adding observer because it's already registered");
51            }
52        }
53    }
54
55    public void removeObserver(SessionObserver observer) {
56        synchronized (observers) {
57            observers.remove(observer);
58        }
59    }
60
61    public Collection<SessionObserver> getObservers() {
62        return Collections.unmodifiableCollection(observers);
63    }
64    
65    public Iterator<SessionObserver> observerIterator() {
66        return Collections.unmodifiableList(observers).iterator();
67    }
68
69    public boolean contains(SessionObserver observer) {
70        return observers.contains(observer);
71    }
72
73    public int size() {
74        return observers.size();
75    }
76
77    /**
78     * Get the list of observers as an array.
79     * @return An array of all registered observers.
80     */
81    protected SessionObserver[] getObserverList() {
82        SessionObserver[] observerList =
83            observers.toArray(new SessionObserver[0]);
84        return observerList;
85    }
86}