Coverage Report - org.apache.commons.scxml.NotificationRegistry

Classes in this File Line Coverage Branch Coverage Complexity
NotificationRegistry
88% 
78% 
1.75

 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *     http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.scxml;
 18  
 
 19  
 import java.io.Serializable;
 20  
 import java.util.HashMap;
 21  
 import java.util.HashSet;
 22  
 import java.util.Iterator;
 23  
 import java.util.Map;
 24  
 import java.util.Set;
 25  
 
 26  
 import org.apache.commons.scxml.model.SCXML;
 27  
 import org.apache.commons.scxml.model.Transition;
 28  
 import org.apache.commons.scxml.model.TransitionTarget;
 29  
 
 30  
 /**
 31  
  * The registry where SCXML listeners are recorded for nodes of
 32  
  * interest such as the <code>SCXML</code> root,
 33  
  * <code>TransitionTarget</code>s and <code>Transition</code>s.
 34  
  * The notification registry keeps track of all
 35  
  * <code>SCXMLListener</code>s attached and notifies relevant
 36  
  * listeners of the events that interest them.
 37  
  *
 38  
  */
 39  
 public final class NotificationRegistry implements Serializable {
 40  
 
 41  
     /**
 42  
      * Serial version UID.
 43  
      */
 44  
     private static final long serialVersionUID = 1L;
 45  
 
 46  
     /**
 47  
      * The Map of all listeners keyed by Observable.
 48  
      */
 49  59
     private Map regs = new HashMap();
 50  
 
 51  
     /**
 52  
      * Constructor.
 53  
      */
 54  
     public NotificationRegistry() {
 55  59
         super();
 56  59
     }
 57  
 
 58  
     /**
 59  
      * Register this SCXMLListener for this Observable.
 60  
      *
 61  
      * @param source The observable this listener wants to listen to
 62  
      * @param lst The listener
 63  
      */
 64  
     synchronized void addListener(final Object source,
 65  
             final SCXMLListener lst) {
 66  47
         Set entries = (Set) regs.get(source);
 67  47
         if (entries == null) {
 68  47
             entries = new HashSet();
 69  47
             regs.put(source, entries);
 70  
         }
 71  47
         entries.add(lst);
 72  47
     }
 73  
 
 74  
     /**
 75  
      * Deregister this SCXMLListener for this Observable.
 76  
      *
 77  
      * @param source The observable this listener wants to stop listening to
 78  
      * @param lst The listener
 79  
      */
 80  
     synchronized void removeListener(final Object source,
 81  
             final SCXMLListener lst) {
 82  0
         Set entries = (Set) regs.get(source);
 83  0
         if (entries != null) {
 84  0
             entries.remove(lst);
 85  0
             if (entries.size() == 0) {
 86  0
                 regs.remove(source);
 87  
             }
 88  
         }
 89  0
     }
 90  
 
 91  
     /**
 92  
      * Inform all relevant listeners that a TransitionTarget has been
 93  
      * entered.
 94  
      *
 95  
      * @param observable The Observable
 96  
      * @param state The TransitionTarget that was entered
 97  
      */
 98  
     public void fireOnEntry(final TransitionTarget observable,
 99  
             final TransitionTarget state) {
 100  227
         Object source = observable;
 101  227
         fireOnEntry(source, state);
 102  227
     }
 103  
 
 104  
     /**
 105  
      * Inform all relevant listeners that a TransitionTarget has been
 106  
      * entered.
 107  
      *
 108  
      * @param observable The Observable
 109  
      * @param state The TransitionTarget that was entered
 110  
      */
 111  
     public void fireOnEntry(final SCXML observable,
 112  
             final TransitionTarget state) {
 113  227
         Object source = observable;
 114  227
         fireOnEntry(source, state);
 115  227
     }
 116  
 
 117  
     /**
 118  
      * Inform all relevant listeners that a TransitionTarget has been
 119  
      * entered.
 120  
      *
 121  
      * @param source The Observable
 122  
      * @param state The TransitionTarget that was entered
 123  
      */
 124  
     private synchronized void fireOnEntry(final Object source,
 125  
             final TransitionTarget state) {
 126  454
         Set entries = (Set) regs.get(source);
 127  454
         if (entries != null) {
 128  226
             for (Iterator iter = entries.iterator(); iter.hasNext();) {
 129  226
                 SCXMLListener lst = (SCXMLListener) iter.next();
 130  226
                 lst.onEntry(state);
 131  
             }
 132  
         }
 133  454
     }
 134  
 
 135  
     /**
 136  
      * Inform all relevant listeners that a TransitionTarget has been
 137  
      * exited.
 138  
      *
 139  
      * @param observable The Observable
 140  
      * @param state The TransitionTarget that was exited
 141  
      */
 142  
     public void fireOnExit(final TransitionTarget observable,
 143  
             final TransitionTarget state) {
 144  134
         Object source = observable;
 145  134
         fireOnExit(source, state);
 146  134
     }
 147  
 
 148  
     /**
 149  
      * Inform all relevant listeners that a TransitionTarget has been
 150  
      * exited.
 151  
      *
 152  
      * @param observable The Observable
 153  
      * @param state The TransitionTarget that was exited
 154  
      */
 155  
     public void fireOnExit(final SCXML observable,
 156  
             final TransitionTarget state) {
 157  134
         Object source = observable;
 158  134
         fireOnExit(source, state);
 159  134
     }
 160  
 
 161  
     /**
 162  
      * Inform all relevant listeners that a TransitionTarget has been
 163  
      * exited.
 164  
      *
 165  
      * @param source The Observable
 166  
      * @param state The TransitionTarget that was exited
 167  
      */
 168  
     private synchronized void fireOnExit(final Object source,
 169  
             final TransitionTarget state) {
 170  268
         Set entries = (Set) regs.get(source);
 171  268
         if (entries != null) {
 172  134
             for (Iterator iter = entries.iterator(); iter.hasNext();) {
 173  134
                 SCXMLListener lst = (SCXMLListener) iter.next();
 174  134
                 lst.onExit(state);
 175  
             }
 176  
         }
 177  268
     }
 178  
 
 179  
     /**
 180  
      * Inform all relevant listeners of a transition that has occured.
 181  
      *
 182  
      * @param observable The Observable
 183  
      * @param from The source TransitionTarget
 184  
      * @param to The destination TransitionTarget
 185  
      * @param transition The Transition that was taken
 186  
      */
 187  
     public void fireOnTransition(final Transition observable,
 188  
             final TransitionTarget from, final TransitionTarget to,
 189  
             final Transition transition) {
 190  115
         Object source = observable;
 191  115
         fireOnTransition(source, from, to, transition);
 192  115
     }
 193  
 
 194  
     /**
 195  
      * Inform all relevant listeners of a transition that has occured.
 196  
      *
 197  
      * @param observable The Observable
 198  
      * @param from The source TransitionTarget
 199  
      * @param to The destination TransitionTarget
 200  
      * @param transition The Transition that was taken
 201  
      */
 202  
     public void fireOnTransition(final SCXML observable,
 203  
             final TransitionTarget from, final TransitionTarget to,
 204  
             final Transition transition) {
 205  115
         Object source = observable;
 206  115
         fireOnTransition(source, from, to, transition);
 207  115
     }
 208  
 
 209  
     /**
 210  
      * Inform all relevant listeners of a transition that has occured.
 211  
      *
 212  
      * @param source The Observable
 213  
      * @param from The source TransitionTarget
 214  
      * @param to The destination TransitionTarget
 215  
      * @param transition The Transition that was taken
 216  
      */
 217  
     private synchronized void fireOnTransition(final Object source,
 218  
             final TransitionTarget from, final TransitionTarget to,
 219  
             final Transition transition) {
 220  230
         Set entries = (Set) regs.get(source);
 221  230
         if (entries != null) {
 222  115
             for (Iterator iter = entries.iterator(); iter.hasNext();) {
 223  115
                 SCXMLListener lst = (SCXMLListener) iter.next();
 224  115
                 lst.onTransition(from, to, transition);
 225  
             }
 226  
         }
 227  230
     }
 228  
 
 229  
 }
 230