Coverage Report - org.vostokframework.domain.loading.states.queueloader.LoadingQueueLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
LoadingQueueLoader
100%
89/89
N/A
0
 
 1  
 /*
 2  
  * Licensed under the MIT License
 3  
  * 
 4  
  * Copyright 2011 (c) Flávio Silva, flsilva.com
 5  
  *
 6  
  * Permission is hereby granted, free of charge, to any person
 7  
  * obtaining a copy of this software and associated documentation
 8  
  * files (the "Software"), to deal in the Software without
 9  
  * restriction, including without limitation the rights to use,
 10  
  * copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  
  * copies of the Software, and to permit persons to whom the
 12  
  * Software is furnished to do so, subject to the following
 13  
  * conditions:
 14  
  *
 15  
  * The above copyright notice and this permission notice shall be
 16  
  * included in all copies or substantial portions of the Software.
 17  
  *
 18  
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 19  
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 20  
  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 21  
  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 22  
  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 23  
  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 24  
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 25  
  * OTHER DEALINGS IN THE SOFTWARE.
 26  
  * 
 27  
  * http://www.opensource.org/licenses/mit-license.php
 28  
  */
 29  1
 package org.vostokframework.domain.loading.states.queueloader
 30  
 {
 31  
         import org.as3collections.IIterator;
 32  
         import org.vostokframework.domain.loading.ILoader;
 33  
         import org.vostokframework.domain.loading.ILoaderStateTransition;
 34  
         import org.vostokframework.domain.loading.events.LoaderErrorEvent;
 35  
         import org.vostokframework.domain.loading.events.LoaderEvent;
 36  
         import org.vostokframework.domain.loading.states.queueloader.IQueueLoadingPolicy;
 37  
 
 38  
         /**
 39  
          * description
 40  
          * 
 41  
          * @author Flávio Silva
 42  
          */
 43  
         public class LoadingQueueLoader extends QueueLoaderState
 44  
         {
 45  
                 
 46  
                 /**
 47  
                  * @private
 48  
                  */
 49  
                 private var _openEventDispatched:Boolean;
 50  
                 
 51  
                 override public function get isLoading():Boolean { return true; }
 52  
                 
 53  
                 override public function get openedConnections():int
 54  
                 {
 55  1
                         validateDisposal();
 56  
                         
 57  1
                         var it:IIterator = loadingStatus.allLoaders.iterator();
 58  
                         var child:ILoader;
 59  
                         var sum:int;
 60  
                         
 61  1
                         while (it.hasNext())
 62  
                         {
 63  1
                                 child = it.next();
 64  1
                                 sum += child.openedConnections;
 65  
                         }
 66  
                         
 67  1
                         return sum;
 68  
                 }
 69  
                 //TODO:note: explain that this Composite implementation
 70  
                 //does not have the Failed state
 71  
                 //if all loader fail it will become Complete
 72  
                 //also explain that this objeto starts its logic as soon as it is instanciated
 73  
                 //therefore if its instantiated with an empty queue of loaders
 74  
                 //it will dispatches LoaderEvent.COMPLETE
 75  
                 //and make state transition to CompleteQueueLoader
 76  
                 /**
 77  
                  * description
 78  
                  * 
 79  
                  * @param name
 80  
                  * @param ordinal
 81  
                  */
 82  
                 public function LoadingQueueLoader(loader:ILoaderStateTransition, loadingStatus:QueueLoadingStatus, policy:IQueueLoadingPolicy, maxConcurrentConnections:int)
 83  
                 {
 84  1
                         super(loadingStatus, policy, maxConcurrentConnections);
 85  
                         
 86  1
                         if (loadingStatus.queuedLoaders.isEmpty())
 87  
                         {
 88  1
                                 var errorMessage:String = "Argument <loadingStatus> must not have an empty queue (loadingStatus.queuedLoaders).";
 89  1
                                 errorMessage += " Queue must have at least one element.";
 90  
                                 
 91  1
                                 throw new ArgumentError(errorMessage);
 92  
                         }
 93  
                         
 94  1
                         setLoader(loader);
 95  1
                         addChildrenListeners();
 96  1
                         processLoading();
 97  1
                 }
 98  
                 
 99  
                 override public function load():void
 100  
                 {
 101  
                         //do nothing
 102  1
                 }
 103  
                 
 104  
                 override public function stop():void
 105  
                 {
 106  1
                         super.stop();
 107  1
                         _openEventDispatched = false;
 108  1
                 }
 109  
                 
 110  
                 /**
 111  
                  * @private
 112  
                  */
 113  
                 override protected function doDispose():void
 114  
                 {
 115  1
                         removeChildrenListeners();
 116  1
                 }
 117  
                 
 118  
                 /**
 119  
                  * @private
 120  
                  */
 121  
                 override protected function childAdded(child:ILoader): void
 122  
                 {
 123  1
                         addChildListeners(child);
 124  1
                         processLoading();
 125  1
                 }
 126  
                 
 127  
                 /**
 128  
                  * @private
 129  
                  */
 130  
                 override protected function childRemoved(child:ILoader): void
 131  
                 {
 132  1
                         removeChildListeners(child);
 133  1
                         processLoading();
 134  1
                 }
 135  
                 
 136  
                 /**
 137  
                  * @private
 138  
                  */
 139  
                 override protected function childResumed(child:ILoader): void
 140  
                 {
 141  1
                         child = null;//just to avoid compiler warnings
 142  1
                         processLoading();
 143  1
                 }
 144  
                 
 145  
                 /**
 146  
                  * @private
 147  
                  */
 148  
                 override protected function childStopped(child:ILoader): void
 149  
                 {
 150  1
                         child = null;//just to avoid compiler warnings
 151  1
                         processLoading();
 152  1
                 }
 153  
                 
 154  
                 /**
 155  
                  * @private
 156  
                  */
 157  
                 override protected function maxConcurrentConnectionsChanged(): void
 158  
                 {
 159  1
                         processLoading();
 160  1
                 }
 161  
                 
 162  
                 /**
 163  
                  * @private
 164  
                  */
 165  
                 override protected function priorityChildChanged(child:ILoader): void
 166  
                 {
 167  1
                         child = null;//just to avoid compiler warnings
 168  1
                         processLoading();
 169  1
                 }
 170  
                 
 171  
                 /**
 172  
                  * @private
 173  
                  */
 174  
                 private function addChildrenListeners():void
 175  
                 {
 176  1
                         if (loadingStatus.allLoaders.isEmpty()) return;
 177  
                         
 178  1
                         var it:IIterator = loadingStatus.allLoaders.iterator();
 179  
                         var child:ILoader;
 180  
                         
 181  1
                         while (it.hasNext())
 182  
                         {
 183  1
                                 child = it.next();
 184  1
                                 addChildListeners(child);
 185  
                         }
 186  1
                 }
 187  
                 
 188  
                 private function addChildListeners(child:ILoader):void
 189  
                 {
 190  1
                         validateDisposal();
 191  
                         
 192  1
                         child.addEventListener(LoaderEvent.COMPLETE, childCompleteHandler, false, 0, true);
 193  1
                         child.addEventListener(LoaderErrorEvent.FAILED, childFailedHandler, false, 0, true);
 194  1
                         child.addEventListener(LoaderEvent.OPEN, childOpenHandler, false, 0, true);
 195  1
                 }
 196  
                 
 197  
                 /**
 198  
                  * @private
 199  
                  */
 200  
                 private function isLoadingComplete():Boolean
 201  
                 {
 202  1
                         validateDisposal();
 203  
                         
 204  1
                         return loadingStatus.loadingLoaders.isEmpty() &&
 205  1
                                 loadingStatus.queuedLoaders.isEmpty() &&
 206  1
                                 loadingStatus.stoppedLoaders.isEmpty();
 207  
                 }
 208  
                 
 209  
                 /**
 210  
                  * @private
 211  
                  */
 212  
                 private function loadingComplete(): void
 213  
                 {
 214  1
                         removeChildrenListeners();
 215  
                         
 216  1
                         loader.setState(new CompleteQueueLoader(loader, loadingStatus, policy, maxConcurrentConnections));
 217  1
                         loader.dispatchEvent(new LoaderEvent(LoaderEvent.COMPLETE));
 218  1
                         dispose();
 219  1
                 }
 220  
                 
 221  
                 /**
 222  
                  * @private
 223  
                  */
 224  
                 private function processLoading(): void
 225  
                 {
 226  1
                         validateDisposal();
 227  
                         
 228  1
                         if (isLoadingComplete())
 229  
                         {
 230  1
                                 loadingComplete();
 231  1
                                 return;
 232  
                         }
 233  
                         
 234  1
                         if (loadingStatus.queuedLoaders.isEmpty()) return;
 235  
                         
 236  1
                         policy.process(loadingStatus, maxConcurrentConnections);
 237  1
                 }
 238  
                 
 239  
                 private function removeChildrenListeners():void
 240  
                 {
 241  1
                         validateDisposal();
 242  
                         
 243  1
                         var it:IIterator = loadingStatus.allLoaders.iterator();
 244  
                         var child:ILoader;
 245  
                         
 246  1
                         while (it.hasNext())
 247  
                         {
 248  1
                                 child = it.next();
 249  1
                                 removeChildListeners(child);
 250  
                         }
 251  1
                 }
 252  
                 
 253  
                 private function removeChildListeners(child:ILoader):void
 254  
                 {
 255  1
                         validateDisposal();
 256  
                         
 257  1
                         child.removeEventListener(LoaderEvent.COMPLETE, childCompleteHandler, false);
 258  1
                         child.removeEventListener(LoaderErrorEvent.FAILED, childFailedHandler, false);
 259  1
                         child.removeEventListener(LoaderEvent.OPEN, childOpenHandler, false);
 260  1
                 }
 261  
                 
 262  
                 ////////////////////////////////////////////////////////////////////////
 263  
                 //////////////////////////// CHILD LISTENERS ///////////////////////////
 264  
                 ////////////////////////////////////////////////////////////////////////
 265  
                 
 266  
                 private function childCompleteHandler(event:LoaderEvent):void
 267  
                 {
 268  1
                         loadingStatus.completeLoaders.add(event.target);
 269  1
                         loadingStatus.loadingLoaders.remove(event.target);
 270  
                         
 271  1
                         processLoading();
 272  1
                 }
 273  
                 
 274  
                 private function childFailedHandler(event:LoaderErrorEvent):void
 275  
                 {
 276  1
                         loadingStatus.failedLoaders.add(event.target);
 277  1
                         loadingStatus.loadingLoaders.remove(event.target);
 278  
                         
 279  1
                         processLoading();
 280  1
                 }
 281  
                 
 282  
                 private function childOpenHandler(event:LoaderEvent):void
 283  
                 {
 284  1
                         validateDisposal();
 285  
                         
 286  1
                         if (!_openEventDispatched) loader.dispatchEvent(new LoaderEvent(LoaderEvent.OPEN, null, event.latency));
 287  1
                         _openEventDispatched = true;
 288  1
                 }
 289  
         }
 290  
 
 291  
 }