Coverage Report - org.vostokframework.domain.loading.LoaderRepository
 
Classes in this File Line Coverage Branch Coverage Complexity
LoaderRepository
36%
17/46
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
 30  
 {
 31  
         import org.as3collections.IIterator;
 32  
         import org.as3collections.IList;
 33  
         import org.as3collections.IMap;
 34  
         import org.as3collections.lists.ReadOnlyArrayList;
 35  
         import org.as3collections.lists.TypedList;
 36  
         import org.as3collections.maps.HashMap;
 37  
         import org.as3collections.maps.TypedMap;
 38  
         import org.as3utils.ReflectionUtil;
 39  
         import org.vostokframework.VostokIdentification;
 40  
         import org.vostokframework.domain.loading.errors.DuplicateLoaderError;
 41  
 
 42  
         /**
 43  
          * description
 44  
          * 
 45  
          * @author Flávio Silva
 46  
          */
 47  
         public class LoaderRepository
 48  
         {
 49  
                 private var _loaderMap:IMap;//<String,ILoader> - where String = ILoader().identification.toString()
 50  
                 
 51  
                 public function get openedConnections():int
 52  
                 {
 53  1
                         var it:IIterator = _loaderMap.iterator();
 54  
                         var loader:ILoader;
 55  
                         var sum:int;
 56  
                         
 57  1
                         while (it.hasNext())
 58  
                         {
 59  1
                                 loader = it.next();
 60  1
                                 sum += loader.openedConnections;
 61  
                         }
 62  
                         
 63  1
                         return sum;
 64  
                 }
 65  
 
 66  
                 /**
 67  
                  * description
 68  
                  */
 69  
                 public function LoaderRepository()
 70  1
                 {
 71  1
                         _loaderMap = new TypedMap(new HashMap(), String, ILoader);
 72  1
                 }
 73  
                 
 74  
                 /**
 75  
                  * description
 76  
                  * 
 77  
                  * @param         loader
 78  
                  * @throws         ArgumentError         if the <code>loader</code> argument is <code>null</code>.
 79  
                  * @throws         org.vostokframework.loadermanagement.errors.DuplicateLoaderError         if already exists an <code>ILoader</code> object stored with the same <code>id</code> of the provided <code>loader</code> argument.
 80  
                  * @return
 81  
                  */
 82  
                 public function add(loader:ILoader): void
 83  
                 {
 84  1
                         if (!loader) throw new ArgumentError("Argument <loader> must not be null.");
 85  
                         
 86  1
                         if (_loaderMap.containsKey(loader.identification.toString()))
 87  
                         {
 88  0
                                 var message:String = "There is already an ILoader object stored with identification:\n";
 89  0
                                 message += "<" + loader.identification + ">\n";
 90  0
                                 message += "Use the method <LoaderRepository().exists()> to check if a ILoader object already exists.\n";
 91  0
                                 message += "For further information please read the documentation section about the ILoader object.";
 92  
                                 
 93  0
                                 throw new DuplicateLoaderError(loader.identification, message);
 94  
                         }
 95  
                         
 96  1
                         _loaderMap.put(loader.identification.toString(), loader);
 97  1
                 }
 98  
                 
 99  
                 /**
 100  
                  * description
 101  
                  * 
 102  
                  * @return
 103  
                   */
 104  
                 public function clear(): void
 105  
                 {
 106  1
                         _loaderMap.clear();
 107  1
                 }
 108  
 
 109  
                 /**
 110  
                  * description
 111  
                  * 
 112  
                  * @param         loaderId    
 113  
                  * @throws         ArgumentError         if the <code>loaderId</code> argument is <code>null</code> or <code>empty</code>.
 114  
                  * @return
 115  
                  */
 116  
                 public function exists(identification:VostokIdentification): Boolean
 117  
                 {
 118  0
                         if (!identification) throw new ArgumentError("Argument <identification> must not be null.");
 119  
                         
 120  0
                         return _loaderMap.containsKey(identification.toString());
 121  
                 }
 122  
 
 123  
                 /**
 124  
                  * description
 125  
                  * 
 126  
                  * @param         loaderId    
 127  
                  * @throws         ArgumentError         if the <code>loaderId</code> argument is <code>null</code> or <code>empty</code>.
 128  
                  * @return
 129  
                  */
 130  
                 public function find(identification:VostokIdentification): ILoader
 131  
                 {
 132  0
                         if (!identification) throw new ArgumentError("Argument <identification> must not be null.");
 133  
                         
 134  0
                         return _loaderMap.getValue(identification.toString());
 135  
                 }
 136  
 
 137  
                 /**
 138  
                  * description
 139  
                  * 
 140  
                  * @return
 141  
                   */
 142  
                 public function findAll(): IList
 143  
                 {
 144  0
                         if (isEmpty()) return null;
 145  0
                         var l:IList = new ReadOnlyArrayList(_loaderMap.getValues().toArray());
 146  
                         
 147  0
                         return l;
 148  
                 }
 149  
                 
 150  
                 /**
 151  
                  * description
 152  
                  * 
 153  
                  * @return
 154  
                   */
 155  
                 /*public function findAllLoading(): IList
 156  
                 {
 157  
                         var list1:IList = new ArrayList(findByStatus(LoaderConnecting.INSTANCE).toArray());
 158  
                         var list2:IList = new ArrayList(findByStatus(LoaderLoading.INSTANCE).toArray());
 159  
                         //TODO: otimizar
 160  
                         var unique:UniqueList = new UniqueList(new ArrayList());
 161  
                         unique.addAll(list1);
 162  
                         unique.addAll(list2);
 163  
                         
 164  
                         return new ReadOnlyArrayList(unique.toArray());
 165  
                 }*/
 166  
                 
 167  
                 /**
 168  
                  * description
 169  
                  * 
 170  
                  * @return
 171  
                   */
 172  
                 /*public function findByStatus(status:LoaderState): IList
 173  
                 {
 174  
                         if (!status) throw new ArgumentError("Argument <status> must not be null.");
 175  
                         
 176  
                         var it:IIterator = _loaderMap.getValues().iterator();
 177  
                         var loader:ILoader;
 178  
                         var list:IList = new ArrayList();
 179  
                         
 180  
                         while (it.hasNext())
 181  
                         {
 182  
                                 loader = it.next();
 183  
                                 if (loader.state.equals(status))
 184  
                                 {
 185  
                                         list.add(loader);
 186  
                                 }
 187  
                         }
 188  
                         
 189  
                         return new ReadOnlyArrayList(list.toArray());
 190  
                 }*/
 191  
                 
 192  
                 public function findParentLoader(childIdentification:VostokIdentification):ILoader
 193  
                 {
 194  0
                         var it:IIterator = _loaderMap.getValues().iterator();
 195  
                         var parentLoader:ILoader;
 196  
                         
 197  0
                         while (it.hasNext())
 198  
                         {
 199  0
                                 parentLoader = it.next();
 200  0
                                 if (parentLoader.containsChild(childIdentification)) return parentLoader;
 201  
                         }
 202  
                         
 203  0
                         return null;
 204  
                 }
 205  
                 
 206  
                 /**
 207  
                  * description
 208  
                  * 
 209  
                  * @return
 210  
                  */
 211  
                 public function isEmpty(): Boolean
 212  
                 {
 213  0
                         return _loaderMap.isEmpty();
 214  
                 }
 215  
 
 216  
                 /**
 217  
                  * description
 218  
                  * 
 219  
                  * @param         loaderId    
 220  
                  * @throws         ArgumentError         if the <code>loaderId</code> argument is <code>null</code> or <code>empty</code>.
 221  
                  * @return
 222  
                  */
 223  
                 public function remove(identification:VostokIdentification): Boolean
 224  
                 {
 225  1
                         if (!identification) throw new ArgumentError("Argument <identification> must not be null.");
 226  
                         
 227  1
                         return _loaderMap.remove(identification.toString()) != null;
 228  
                 }
 229  
                 
 230  
                 /**
 231  
                  * description
 232  
                  * 
 233  
                  * @param         loaders    
 234  
                  * @throws         ArgumentError         if the <code>loaders</code> argument is <code>null</code>.
 235  
                  * @return
 236  
                  */
 237  
                 public function removeAll(loaders:IList): Boolean
 238  
                 {
 239  0
                         if (!loaders) throw new ArgumentError("Argument <loaders> must not be null.");
 240  0
                         if (loaders.isEmpty()) return false;
 241  
                         
 242  0
                         var prevSize:int = size();
 243  0
                         var $loaders:IList = new TypedList(loaders, ILoader);
 244  0
                         var it:IIterator = $loaders.iterator();
 245  
                         var loader:ILoader;
 246  
                         
 247  0
                         while (it.hasNext())
 248  
                         {
 249  0
                                 loader = it.next();
 250  0
                                 remove(loader.identification);
 251  
                         }
 252  
                         
 253  0
                         return size() != prevSize;
 254  
                 }
 255  
                 
 256  
                 /**
 257  
                  * description
 258  
                  * 
 259  
                  * @return
 260  
                  */
 261  
                 public function size(): int
 262  
                 {
 263  0
                         return _loaderMap.size();
 264  
                 }
 265  
                 
 266  
                 /**
 267  
                  * description
 268  
                  * 
 269  
                  * @return
 270  
                  */
 271  
                 public function toString(): String
 272  
                 {
 273  0
                         return "[" + ReflectionUtil.getClassName(this) + "] <" + _loaderMap.getValues() + ">";
 274  
                 }
 275  
                 
 276  
         }
 277  
 
 278  
 }