Coverage Report - org.vostokframework.domain.loading.states.queueloader.policies.SpecialHighestLowestQueueLoadingPolicy
 
Classes in this File Line Coverage Branch Coverage Complexity
SpecialHighestLowestQueueLoadingPolicy
100%
48/48
N/A
0
 
 1  
 /*
 2  
  * Licensed under the MIT License
 3  
  * 
 4  
  * Copyright 2010 (c) Flávio Silva, http://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  
 
 30  1
 package org.vostokframework.domain.loading.states.queueloader.policies
 31  
 {
 32  
         import org.as3collections.ICollection;
 33  
         import org.as3collections.IIterator;
 34  
         import org.vostokframework.domain.loading.GlobalLoadingSettings;
 35  
         import org.vostokframework.domain.loading.ILoader;
 36  
         import org.vostokframework.domain.loading.LoadPriority;
 37  
         import org.vostokframework.domain.loading.LoaderRepository;
 38  
         import org.vostokframework.domain.loading.states.queueloader.QueueLoadingStatus;
 39  
 
 40  
         /**
 41  
          * description
 42  
          * 
 43  
          * @author Flávio Silva
 44  
          */
 45  
         public class SpecialHighestLowestQueueLoadingPolicy extends AbstractQueueLoadingPolicy
 46  
         {
 47  
                 
 48  
                 /**
 49  
                  * Constructor, creates a new AssetRepositoryError instance.
 50  
                  * 
 51  
                  * @param message         A string associated with the error object.
 52  
                  */
 53  
                 public function SpecialHighestLowestQueueLoadingPolicy(loaderRepository:LoaderRepository, globalLoadingSettings:GlobalLoadingSettings)
 54  
                 {
 55  1
                         super(loaderRepository, globalLoadingSettings);
 56  1
                 }
 57  
                 
 58  
                 override protected function isNextLoaderEligible(loadingStatus:QueueLoadingStatus):Boolean
 59  
                 {
 60  1
                         var nextLoader:ILoader = loadingStatus.queuedLoaders.peek();
 61  1
                         var priorityNextLoader:LoadPriority = LoadPriority.getByOrdinal(nextLoader.priority);
 62  
                         
 63  1
                         if (priorityNextLoader.equals(LoadPriority.HIGHEST))
 64  
                         {
 65  1
                                 stopAnyNotHighest(loadingStatus);
 66  1
                                 return true;
 67  
                         }
 68  
                         
 69  1
                         if (priorityNextLoader.equals(LoadPriority.LOWEST))
 70  
                         {
 71  1
                                 if (containsOnlyLowest(loadingStatus.loadingLoaders)) return true;
 72  1
                                 return false;
 73  
                         }
 74  
                         
 75  
                         //priorityNextLoader is not LoadPriority.HIGHEST nor LoadPriority.LOWEST
 76  
                         //if it contains any HIGHEST loader loading, permission is denied
 77  1
                         if (containsSomeHighest(loadingStatus.loadingLoaders)) return false;
 78  
                         
 79  
                         //otherwise all LOWEST loading loaders are stopped
 80  
                         //and permission is accepted
 81  1
                         stopAnyLowest(loadingStatus);
 82  1
                         return true;
 83  
                 }
 84  
                 
 85  
                 private function containsSomeHighest(loadingLoaders:ICollection):Boolean
 86  
                 {
 87  1
                         if (loadingLoaders.isEmpty()) return false;
 88  
                         
 89  1
                         var it:IIterator = loadingLoaders.iterator();
 90  
                         var loader:ILoader;
 91  
                         var loaderPriority:LoadPriority;
 92  
                         
 93  1
                         while (it.hasNext())
 94  
                         {
 95  1
                                 loader = it.next();
 96  1
                                 loaderPriority = LoadPriority.getByOrdinal(loader.priority);
 97  1
                                 if (loaderPriority.equals(LoadPriority.HIGHEST)) return true;
 98  
                         }
 99  
                         
 100  1
                         return false;
 101  
                 }
 102  
                 
 103  
                 private function containsOnlyLowest(loadingLoaders:ICollection):Boolean
 104  
                 {
 105  1
                         if (loadingLoaders.isEmpty()) return true;
 106  
                         
 107  1
                         var it:IIterator = loadingLoaders.iterator();
 108  
                         var loader:ILoader;
 109  
                         var loaderPriority:LoadPriority;
 110  
                         
 111  1
                         while (it.hasNext())
 112  
                         {
 113  1
                                 loader = it.next();
 114  1
                                 loaderPriority = LoadPriority.getByOrdinal(loader.priority);
 115  1
                                 if (!loaderPriority.equals(LoadPriority.LOWEST)) return false;
 116  
                         }
 117  
                         
 118  1
                         return true;
 119  
                 }
 120  
                 
 121  
                 private function stopAnyNotHighest(loadingStatus:QueueLoadingStatus):void
 122  
                 {
 123  1
                         if (loadingStatus.loadingLoaders.isEmpty()) return;
 124  
                         
 125  1
                         var it:IIterator = loadingStatus.loadingLoaders.iterator();
 126  
                         var loader:ILoader;
 127  
                         var loaderPriority:LoadPriority;
 128  
                         
 129  1
                         while (it.hasNext())
 130  
                         {
 131  1
                                 loader = it.next();
 132  1
                                 loaderPriority = LoadPriority.getByOrdinal(loader.priority);
 133  
                                 
 134  1
                                 if (!loaderPriority.equals(LoadPriority.HIGHEST))
 135  
                                 {
 136  1
                                         loadingStatus.queuedLoaders.add(loader);
 137  1
                                         it.remove();
 138  
                                         
 139  1
                                         loader.stop();
 140  
                                 }
 141  
                         }
 142  1
                 }
 143  
                 
 144  
                 private function stopAnyLowest(loadingStatus:QueueLoadingStatus):void
 145  
                 {
 146  1
                         if (loadingStatus.loadingLoaders.isEmpty()) return;
 147  
                         
 148  1
                         var it:IIterator = loadingStatus.loadingLoaders.iterator();
 149  
                         var loader:ILoader;
 150  
                         var loaderPriority:LoadPriority;
 151  
                         
 152  1
                         while (it.hasNext())
 153  
                         {
 154  1
                                 loader = it.next();
 155  1
                                 loaderPriority = LoadPriority.getByOrdinal(loader.priority);
 156  
                                 
 157  1
                                 if (loaderPriority.equals(LoadPriority.LOWEST))
 158  
                                 {
 159  1
                                         loadingStatus.queuedLoaders.add(loader);
 160  1
                                         it.remove();
 161  
                                         
 162  1
                                         loader.stop();
 163  
                                 }
 164  
                         }
 165  1
                 }
 166  
                 
 167  
         }
 168  
 
 169  
 }