001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal.events;
022:
023: import com.db4o.events.*;
024: import com.db4o.ext.Db4oException;
025: import com.db4o.foundation.*;
026: import com.db4o.internal.ReflectException;
027:
028: /**
029: * @exclude
030: * @sharpen.ignore
031: */
032: public class Event4Impl implements Event4 {
033:
034: private Collection4 _listeners;
035:
036: public Event4Impl() {
037: }
038:
039: public final void addListener(EventListener4 listener) {
040: validateListener(listener);
041:
042: Collection4 listeners = new Collection4();
043: listeners.add(listener);
044: addExistingListenersTo(listeners);
045: _listeners = listeners;
046:
047: onListenerAdded();
048: }
049:
050: private void addExistingListenersTo(Collection4 newListeners) {
051: if (_listeners == null) {
052: return;
053: }
054: Iterator4 i = _listeners.iterator();
055: while (i.moveNext()) {
056: newListeners.add(i.current());
057: }
058:
059: }
060:
061: /**
062: * Might be overriden whenever specific events need
063: * to know when listeners subscribe to the event.
064: */
065: protected void onListenerAdded() {
066: }
067:
068: public final void removeListener(EventListener4 listener) {
069: validateListener(listener);
070:
071: if (null == _listeners) {
072: return;
073: }
074:
075: Collection4 listeners = new Collection4();
076: addExistingListenersTo(listeners);
077: listeners.remove(listener);
078:
079: _listeners = listeners;
080: }
081:
082: public final void trigger(EventArgs args) {
083: if (null == _listeners) {
084: return;
085: }
086: Iterator4 iterator = _listeners.iterator();
087: while (iterator.moveNext()) {
088: EventListener4 listener = (EventListener4) iterator
089: .current();
090: onEvent(listener, this , args);
091: }
092: }
093:
094: private void onEvent(EventListener4 listener, Event4 e,
095: EventArgs args) {
096: try {
097: listener.onEvent(e, args);
098: } catch (Db4oException db4oException) {
099: throw db4oException;
100: } catch (Throwable exc) {
101: throw new ReflectException(exc);
102: }
103: }
104:
105: private void validateListener(EventListener4 listener) {
106: if (null == listener) {
107: throw new ArgumentNullException();
108: }
109: }
110:
111: public boolean hasListeners() {
112: return _listeners != null;
113: }
114: }
|