Coverage Report - org.apache.maven.wagon.events.TransferEventSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
TransferEventSupport
100 %
38/38
92 %
13/14
1,778
 
 1  
 package org.apache.maven.wagon.events;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 
 26  
 /**
 27  
  * The class allows registration and removal of event listeners of type
 28  
  * TransferListener and dispatch of those events to those listeners
 29  
  *
 30  
  * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
 31  
  * @version $Id: TransferEventSupport.java 682051 2008-08-02 21:29:38Z hboutemy $
 32  
  */
 33  50
 public final class TransferEventSupport
 34  
 {
 35  
 
 36  
     /**
 37  
      * registered listeners
 38  
      */
 39  50
     private final List listeners = new ArrayList();
 40  
 
 41  
     /**
 42  
      * Adds the listener to the collection of listeners
 43  
      * who will be notified when any transfer event occurs
 44  
      * in this <code>Wagon</code> object.
 45  
      * <br/>
 46  
      * If listener is <code>null</code>, no exception is thrown and no action is performed
 47  
      *
 48  
      * @param listener the transfer listener
 49  
      * @see #removeTransferListener(org.apache.maven.wagon.events.TransferListener)
 50  
      * @see TransferListener
 51  
      */
 52  
     public synchronized void addTransferListener( final TransferListener listener )
 53  
     {
 54  40
         if ( listener != null )
 55  
         {
 56  40
             listeners.add( listener );
 57  
         }
 58  40
     }
 59  
 
 60  
     /**
 61  
      * Removes the transfer listener from the collection of listeners so
 62  
      * it no longer receives transfer events.
 63  
      * <br/>
 64  
      * If listener is <code>null</code> or specified listener was not added
 65  
      * to this <code>TransferEventSupport</code> object
 66  
      * no exception is thrown and no action is performed
 67  
      *
 68  
      * @param listener the transfer listener
 69  
      * @see #addTransferListener(TransferListener)
 70  
      */
 71  
     public synchronized void removeTransferListener( final TransferListener listener )
 72  
     {
 73  3
         listeners.remove( listener );
 74  3
     }
 75  
 
 76  
     /**
 77  
      * Returns whether the specified instance of transfer
 78  
      * listener was added to the collection of listeners
 79  
      * who will be notified when an transfer event occurs
 80  
      *
 81  
      * @param listener the transfer listener
 82  
      * @return <code>true<code>
 83  
      *         if given listener was added to the collection of listeners
 84  
      *         <code>false</code> otherwise
 85  
      * @see org.apache.maven.wagon.events.TransferEvent
 86  
      * @see #addTransferListener(TransferListener)
 87  
      */
 88  
     public synchronized boolean hasTransferListener( final TransferListener listener )
 89  
     {
 90  9
         return listeners.contains( listener );
 91  
     }
 92  
 
 93  
 
 94  
     /**
 95  
      * Dispatches the given <code>TransferEvent</code>
 96  
      * to all registered listeners (calls method {@link TransferListener#transferStarted(TransferEvent)} on all of
 97  
      * them}. The Event should be of type {@link TransferEvent#TRANSFER_COMPLETED}
 98  
      *
 99  
      * @param transferEvent the TransferEvent which will be dispatched to listeners
 100  
      */
 101  
     public synchronized void fireTransferStarted( final TransferEvent transferEvent )
 102  
     {
 103  13
         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
 104  
         {
 105  7
             final TransferListener listener = (TransferListener) iter.next();
 106  7
             listener.transferStarted( transferEvent );
 107  7
         }
 108  13
     }
 109  
 
 110  
     /**
 111  
      * Dispatches the given <code>TransferEvent</code>
 112  
      * to all registered listeners (calls method {@link TransferListener#transferProgress(TransferEvent, byte[], int)}
 113  
      * on all of them). The Event should be of type {@link TransferEvent#TRANSFER_PROGRESS}.
 114  
      *
 115  
      * @param transferEvent the TransferEvent which will be dispatched to listeners
 116  
      * @param buffer        the buffer containing the additional content
 117  
      * @param length        the length of the content in the buffer
 118  
      */
 119  
     public synchronized void fireTransferProgress( final TransferEvent transferEvent, byte[] buffer, int length )
 120  
     {
 121  13
         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
 122  
         {
 123  9
             final TransferListener listener = (TransferListener) iter.next();
 124  9
             listener.transferProgress( transferEvent, buffer, length );
 125  
 
 126  9
         }
 127  13
     }
 128  
 
 129  
     /**
 130  
      * Dispatches the given <code>TransferEvent</code>
 131  
      * to all registered listeners (calls method {@link TransferListener#transferCompleted(TransferEvent)} on all of
 132  
      * them}. The Event should be of type {@link TransferEvent#TRANSFER_COMPLETED}
 133  
      *
 134  
      * @param transferEvent the TransferEvent which will be dispatched to listeners
 135  
      */
 136  
     public synchronized void fireTransferCompleted( final TransferEvent transferEvent )
 137  
     {
 138  10
         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
 139  
         {
 140  4
             final TransferListener listener = (TransferListener) iter.next();
 141  4
             listener.transferCompleted( transferEvent );
 142  
 
 143  4
         }
 144  10
     }
 145  
 
 146  
     /**
 147  
      * Dispatches the given <code>TransferEvent</code>
 148  
      * to all registered listeners (calls method {@link TransferListener#transferError(TransferEvent)}  on all of them.
 149  
      * The Event should be of type {@link TransferEvent#TRANSFER_ERROR} and it is expected that
 150  
      * {@link TransferEvent#getException()} } method will return not null value
 151  
      *
 152  
      * @param transferEvent the TransferEvent which will be dispatched to listeners
 153  
      */
 154  
     public synchronized void fireTransferError( final TransferEvent transferEvent )
 155  
     {
 156  11
         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
 157  
         {
 158  11
             final TransferListener listener = (TransferListener) iter.next();
 159  11
             listener.transferError( transferEvent );
 160  
 
 161  11
         }
 162  11
     }
 163  
 
 164  
     /**
 165  
      * Dispatches the given debug message
 166  
      * to all registered listeners (calls method {@link TransferListener#debug(String)} on all of them.
 167  
      *
 168  
      * @param message the debug message which will be dispatched to listeners
 169  
      */
 170  
     public synchronized void fireDebug( final String message )
 171  
     {
 172  
 
 173  9
         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
 174  
         {
 175  8
             final TransferListener listener = (TransferListener) iter.next();
 176  8
             listener.debug( message );
 177  
 
 178  8
         }
 179  9
     }
 180  
 
 181  
     /**
 182  
      * Dispatches the given <code>TransferEvent</code>
 183  
      * to all registered listeners (calls method {@link TransferListener#transferInitiated(TransferEvent)} on all of
 184  
      * them. The Event should be of type {@link TransferEvent#TRANSFER_INITIATED}.
 185  
      *
 186  
      * @param transferEvent the TransferEvent which will be dispatched to listeners
 187  
      */
 188  
     public synchronized void fireTransferInitiated( final TransferEvent transferEvent )
 189  
     {
 190  23
         for ( Iterator iter = listeners.iterator(); iter.hasNext(); )
 191  
         {
 192  11
             final TransferListener listener = (TransferListener) iter.next();
 193  11
             listener.transferInitiated( transferEvent );
 194  11
         }
 195  23
     }
 196  
 }