Coverage Report - org.vostokframework.domain.loading.loaders.VostokLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
VostokLoader
93%
88/94
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.loaders
 30  
 {
 31  
         import org.as3collections.IList;
 32  
         import org.as3collections.lists.ArrayList;
 33  
         import org.as3collections.lists.TypedList;
 34  
         import org.as3coreaddendum.errors.ObjectDisposedError;
 35  
         import org.as3coreaddendum.events.PriorityEvent;
 36  
         import org.as3utils.ReflectionUtil;
 37  
         import org.vostokframework.VostokIdentification;
 38  
         import org.vostokframework.domain.loading.ILoader;
 39  
         import org.vostokframework.domain.loading.ILoaderState;
 40  
         import org.vostokframework.domain.loading.ILoaderStateTransition;
 41  
         import org.vostokframework.domain.loading.LoadPriority;
 42  
 
 43  
         import flash.events.EventDispatcher;
 44  
 
 45  
         /**
 46  
          * description
 47  
          * 
 48  
          * @author Flávio Silva
 49  
          */
 50  
         public class VostokLoader extends EventDispatcher implements ILoaderStateTransition
 51  
         {
 52  
                 /**
 53  
                  * @private
 54  
                  */
 55  
                 private var _disposed:Boolean;
 56  
                 private var _identification:VostokIdentification;
 57  
                 private var _index:int;
 58  
                 private var _priority:LoadPriority;
 59  
                 private var _state:ILoaderState;
 60  
                 private var _stateHistory:IList;//TODO:pensar sobre remover propriedade
 61  
                 
 62  
                 /**
 63  
                  * description
 64  
                  */
 65  
                 public function get identification(): VostokIdentification
 66  
                 {
 67  1
                         validateDisposal();
 68  1
                         return _identification;
 69  
                 }
 70  
                 
 71  
                 /**
 72  
                  * description
 73  
                  */
 74  
                 public function get index(): int
 75  
                 {
 76  1
                         validateDisposal();
 77  1
                         return _index;
 78  
                 }
 79  
                 public function set index(value:int): void
 80  
                 {
 81  1
                         validateDisposal();
 82  1
                         _index = value;
 83  1
                 }
 84  
                 
 85  
                 public function get isLoading():Boolean
 86  
                 {
 87  1
                         validateDisposal();
 88  1
                         return _state.isLoading;
 89  
                 }
 90  
                 
 91  
                 public function get isQueued():Boolean
 92  
                 {
 93  1
                         validateDisposal();
 94  1
                         return _state.isQueued;
 95  
                 }
 96  
                 
 97  
                 public function get isStopped():Boolean
 98  
                 {
 99  1
                         validateDisposal();
 100  1
                         return _state.isStopped;
 101  
                 }
 102  
                 
 103  
                 public function get maxConcurrentConnections():int
 104  
                 {
 105  0
                         validateDisposal();
 106  0
                         return _state.maxConcurrentConnections;
 107  
                 }
 108  
                 
 109  
                 /**
 110  
                  * description
 111  
                  */
 112  
                 public function get priority(): int
 113  
                 {
 114  1
                         validateDisposal();
 115  1
                         return _priority.ordinal;
 116  
                 }
 117  
                 public function set priority(value:int): void
 118  
                 {
 119  1
                         validateDisposal();
 120  
                         
 121  
                         try
 122  
                         {
 123  1
                                 _priority = LoadPriority.getByOrdinal(value);
 124  
                         }
 125  1
                         catch(error:Error)
 126  
                         {
 127  1
                                 var errorMessage:String = "Value must be between 0 and 4. Received: <" + value + ">.\n";
 128  1
                                 errorMessage += "For further information please consult the documentation section about:\n";
 129  1
                                 errorMessage += ReflectionUtil.getClassPath(LoadPriority);
 130  1
                                 throw new ArgumentError(errorMessage);
 131  
                         }
 132  
                         
 133  1
                         dispatchEvent(new PriorityEvent(PriorityEvent.CHANGED, _priority.ordinal));
 134  1
                 }
 135  
                 
 136  
                 /**
 137  
                  * description
 138  
                  */
 139  
                 //public function get state(): ILoaderState { return _state; }
 140  
                 
 141  
                 /**
 142  
                  * description
 143  
                  */
 144  
                 /*public function get stateHistory(): IList
 145  
                 {
 146  
                         validateDisposal();
 147  
                         return new ReadOnlyArrayList(_stateHistory.toArray());
 148  
                 }*/
 149  
                 
 150  
                 public function get openedConnections():int
 151  
                 {
 152  1
                         validateDisposal();
 153  1
                         return _state.openedConnections;
 154  
                 }
 155  
                 
 156  
                 /**
 157  
                  * description
 158  
                  * 
 159  
                  */
 160  
                 public function VostokLoader(identification:VostokIdentification, state:ILoaderState, priority:LoadPriority)
 161  1
                 {
 162  1
                         if (!identification) throw new ArgumentError("Argument <identification> must not be null.");
 163  1
                         if (!state) throw new ArgumentError("Argument <state> must not be null.");
 164  1
                         if (!priority) throw new ArgumentError("Argument <priority> must not be null.");
 165  
                         
 166  1
                         _identification = identification;
 167  1
                         _state = state;
 168  1
                         _priority = priority;
 169  1
                         _stateHistory = new TypedList(new ArrayList(), ILoaderState);
 170  
                         
 171  1
                         _state.setLoader(this);
 172  1
                 }
 173  
                 
 174  
                 /**
 175  
                  * description
 176  
                  * 
 177  
                  * @param loader
 178  
                  */
 179  
                 public function addChild(child:ILoader): void
 180  
                 {
 181  1
                         validateDisposal();
 182  1
                         _state.addChild(child);
 183  1
                 }
 184  
                 
 185  
                 /**
 186  
                  * description
 187  
                  * 
 188  
                  * @param loader
 189  
                  */
 190  
                 public function addChildren(children:IList): void
 191  
                 {
 192  1
                         validateDisposal();
 193  1
                         _state.addChildren(children);
 194  1
                 }
 195  
                 
 196  
                 /**
 197  
                  * description
 198  
                  * 
 199  
                   */
 200  
                 public function cancel(): void
 201  
                 {
 202  1
                         validateDisposal();
 203  1
                         _state.cancel();
 204  1
                 }
 205  
                 
 206  
                 /**
 207  
                  * description
 208  
                  * 
 209  
                  * @param loaderId
 210  
                  */
 211  
                 public function cancelChild(identification:VostokIdentification): void
 212  
                 {
 213  1
                         validateDisposal();
 214  1
                         _state.cancelChild(identification);
 215  1
                 }
 216  
                 
 217  
                 /**
 218  
                  * description
 219  
                  * 
 220  
                  * @param identification
 221  
                  */
 222  
                 public function containsChild(identification:VostokIdentification): Boolean
 223  
                 {
 224  1
                         validateDisposal();
 225  1
                         return _state.containsChild(identification);
 226  
                 }
 227  
                 
 228  
                 /**
 229  
                  * description
 230  
                  * 
 231  
                   */
 232  
                 public function dispose():void
 233  
                 {
 234  1
                         if (_disposed) return;
 235  
                         
 236  1
                         _state.dispose();
 237  1
                         _stateHistory.clear();
 238  
                         
 239  1
                         _stateHistory = null;
 240  1
                         _state = null;
 241  
                         
 242  1
                         _disposed = true;
 243  1
                 }
 244  
                 
 245  
                 public function equals(other : *): Boolean
 246  
                 {
 247  1
                         validateDisposal();
 248  
                         
 249  1
                         if (this == other) return true;
 250  1
                         if (!(other is ILoader)) return false;
 251  
                         
 252  1
                         var otherLoader:ILoader = other as ILoader;
 253  1
                         return _identification.equals(otherLoader.identification);
 254  
                 }
 255  
                 
 256  
                 /**
 257  
                  * description
 258  
                  * 
 259  
                  * @param loaderId
 260  
                  */
 261  
                 public function getChild(identification:VostokIdentification): ILoader
 262  
                 {
 263  1
                         validateDisposal();
 264  1
                         return _state.getChild(identification);
 265  
                 }
 266  
                 
 267  
                 /**
 268  
                  * description
 269  
                  * 
 270  
                  * @param loaderId
 271  
                  */
 272  
                 /*public function getLoaderState(identification:VostokIdentification): ILoaderState
 273  
                 {
 274  
                         validateDisposal();
 275  
                         return _state.getLoaderState(identification);
 276  
                 }*/
 277  
                 
 278  
                 /**
 279  
                  * description
 280  
                  * 
 281  
                  * @param identification
 282  
                  */
 283  
                 public function getParent(identification:VostokIdentification): ILoader
 284  
                 {
 285  1
                         validateDisposal();
 286  1
                         return _state.getParent(identification);
 287  
                 }
 288  
                 
 289  
                 /**
 290  
                  * description
 291  
                  * 
 292  
                   */
 293  
                 public function load(): void
 294  
                 {
 295  1
                         validateDisposal();
 296  1
                         _state.load();
 297  1
                 }
 298  
 
 299  
                 /**
 300  
                  * description
 301  
                  * 
 302  
                  * @param loaderId
 303  
                  */
 304  
                 public function removeChild(identification:VostokIdentification): void
 305  
                 {
 306  1
                         validateDisposal();
 307  1
                         _state.removeChild(identification);
 308  1
                 }
 309  
                 
 310  
                 /**
 311  
                  * description
 312  
                  * 
 313  
                  * @param loaderId
 314  
                  */
 315  
                 public function resumeChild(identification:VostokIdentification): void
 316  
                 {
 317  1
                         validateDisposal();
 318  1
                         _state.resumeChild(identification);
 319  1
                 }
 320  
                 
 321  
                 /**
 322  
                  * description
 323  
                  */
 324  
                 public function setMaxConcurrentConnections(value:int):void
 325  
                 {
 326  0
                         validateDisposal();
 327  0
                         _state.setMaxConcurrentConnections(value);
 328  0
                 }
 329  
                 
 330  
                 /**
 331  
                  * @private
 332  
                  */
 333  
                 public function setState(state:ILoaderState):void
 334  
                 {
 335  1
                         validateDisposal();
 336  
                         
 337  1
                         _state = state;
 338  1
                         _stateHistory.add(_state);
 339  1
                 }
 340  
 
 341  
                 /**
 342  
                  * description
 343  
                  */
 344  
                 public function stop(): void
 345  
                 {
 346  1
                         validateDisposal();
 347  1
                         _state.stop();
 348  1
                 }
 349  
                 
 350  
                 /**
 351  
                  * description
 352  
                  * 
 353  
                  * @param loaderId
 354  
                  */
 355  
                 public function stopChild(identification:VostokIdentification): void
 356  
                 {
 357  1
                         validateDisposal();
 358  1
                         _state.stopChild(identification);
 359  1
                 }
 360  
                 
 361  
                 override public function toString():String
 362  
                 {
 363  0
                         return "[ILoader " + identification + "]";
 364  
                 }
 365  
                 
 366  
                 /**
 367  
                  * @private
 368  
                  */
 369  
                 private function validateDisposal():void
 370  
                 {
 371  1
                         if (_disposed) throw new ObjectDisposedError("This object was disposed, therefore no more operations can be performed.");
 372  1
                 }
 373  
         }
 374  
 
 375  
 }