EMMA Coverage Report (generated Tue Apr 17 08:51:20 BST 2007)
[all classes][org.jtoolkit.essence.data.impl]

COVERAGE SUMMARY FOR SOURCE FILE [ListenerSetImpl.java]

nameclass, %method, %block, %line, %
ListenerSetImpl.java100% (1/1)64%  (7/11)39%  (102/259)45%  (23.8/53)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ListenerSetImpl100% (1/1)64%  (7/11)39%  (102/259)45%  (23.8/53)
add (Event$Listener): void 0%   (0/1)0%   (0/34)0%   (0/6)
getThreadName (): String 0%   (0/1)0%   (0/3)0%   (0/1)
remove (Event$Listener): void 0%   (0/1)0%   (0/21)0%   (0/5)
toString (): String 0%   (0/1)0%   (0/17)0%   (0/1)
onCallback (Event): void 100% (1/1)35%  (25/72)37%  (5.2/14)
addCallback (Callback): void 100% (1/1)40%  (20/50)46%  (5.5/12)
isEmpty (): boolean 100% (1/1)78%  (18/23)67%  (2/3)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
ListenerSetImpl (): void 100% (1/1)100% (14/14)100% (5/5)
notifyReset (boolean): void 100% (1/1)100% (10/10)100% (3/3)
resetListenerArr (): void 100% (1/1)100% (11/11)100% (2/2)

1package org.jtoolkit.essence.data.impl;
2 
3import org.apache.commons.logging.Log;
4import org.apache.commons.logging.LogFactory;
5import org.jetbrains.annotations.NotNull;
6import org.jtoolkit.essence.concurrency.Callback;
7import org.jtoolkit.essence.concurrency.impl.CallbackSetImpl;
8import org.jtoolkit.essence.data.Event;
9 
10import java.util.Arrays;
11import java.util.LinkedHashSet;
12import java.util.Set;
13 
14/*
15   Copyright 2006 Peter Lawrey
16 
17   Licensed under the Apache License, Version 2.0 (the "License");
18   you may not use this file except in compliance with the License.
19   You may obtain a copy of the License at
20 
21       http://www.apache.org/licenses/LICENSE-2.0
22 
23   Unless required by applicable law or agreed to in writing, software
24   distributed under the License is distributed on an &quot;AS IS&quot; BASIS,
25   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26   See the License for the specific language governing permissions and
27   limitations under the License.
28*/
29 
30/**
31 * @author Peter Lawrey
32 */
33public class ListenerSetImpl extends CallbackSetImpl<Event> implements Event.ListenerSet {
34    private static final Log LOG = LogFactory.getLog(ListenerSetImpl.class);
35    private final Set<Event.Listener> listeners = new LinkedHashSet<Event.Listener>();
36    @SuppressWarnings({"FieldAccessedSynchronizedAndUnsynchronized"})
37    private Event.Listener[] listenerArr;
38    @SuppressWarnings({"FieldAccessedSynchronizedAndUnsynchronized"})
39    private boolean online = false;
40 
41    public ListenerSetImpl() {
42        super(CallbackMode.THREADED);
43        resetListenerArr();
44    }
45 
46    public void addCallback(@NotNull Callback<Event> callback) {
47        synchronized (callbacks) {
48            try {
49                callback.onCallback(new Event.StatusEvent(online));
50            } catch (IllegalStateException e) {
51                dropCallback(callback, e);
52                return;
53            } catch (Exception e) {
54                LOG.warn(getThreadName() + ": Dropping callback " + callback, e);
55                return;
56            }
57            super.addCallback(callback);
58        }
59    }
60 
61    private static String getThreadName() {
62        return Thread.currentThread().getName();
63    }
64 
65    public void add(@NotNull Event.Listener l) {
66        synchronized (listeners) {
67            if (listeners.add(callbackMode == CallbackMode.SIMPLE ? l : new ThreadedListener(l)))
68                resetListenerArr();
69        }
70        l.onStatusEvent(online);
71    }
72 
73    public boolean isEmpty() {
74        synchronized (listeners) {
75            return super.isEmpty() && listenerArr.length == 0;
76        }
77    }
78 
79    @SuppressWarnings({"CastToIncompatibleInterface"})
80    public void onCallback(@NotNull Event event) {
81        super.onCallback(event);
82        Event.Listener[] listenerArr;
83        synchronized (listeners) {
84            listenerArr = this.listenerArr;
85        }
86        for (Event.Listener l : listenerArr)
87            try {
88                event.notifyListener(l);
89            } catch (IllegalStateException e) {
90                Callback callback;
91                if (l instanceof ThreadedListener)
92                    callback = (Callback) ((ThreadedListener) l).getUnderlying();
93                else
94                    callback = (Callback) l;
95                if (LOG.isDebugEnabled()) LOG.debug(getThreadName() + ": Dropping callback " + callback, e);
96                super.removeCallback(callback);
97            }
98    }
99 
100    public void notifyReset(boolean online2) {
101        online = online2;
102        onCallback(new Event.StatusEvent(online2));
103    }
104 
105    public void remove(@NotNull Event.Listener l) {
106        synchronized (listeners) {
107            if (listeners.remove(l))
108                resetListenerArr();
109        }
110    }
111 
112    private void resetListenerArr() {
113        listenerArr = listeners.toArray(new Event.Listener[listeners.size()]);
114    }
115 
116    public String toString() {
117        return "ListenerSet callbacks=" + Arrays.asList(getCallbackArray()) + ", listeners=" + Arrays.asList(listenerArr);
118    }
119}

[all classes][org.jtoolkit.essence.data.impl]
EMMA 2.0.5312 (C) Vladimir Roubtsov