Coverage Report - org.vostokframework.application.cache.CachedAssetDataRepository
 
Classes in this File Line Coverage Branch Coverage Complexity
CachedAssetDataRepository
53%
14/26
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.application.cache
 30  
 {
 31  
         import org.as3collections.IList;
 32  
         import org.as3collections.IMap;
 33  
         import org.as3collections.lists.ReadOnlyArrayList;
 34  
         import org.as3collections.maps.HashMap;
 35  
         import org.as3collections.maps.TypedMap;
 36  
         import org.as3utils.ReflectionUtil;
 37  
         import org.vostokframework.VostokIdentification;
 38  
         import org.vostokframework.application.cache.errors.AssetDataAlreadyCachedError;
 39  
 
 40  
         /**
 41  
          * description
 42  
          * 
 43  
          * @author Flávio Silva
 44  
          */
 45  
         public class CachedAssetDataRepository
 46  
         {
 47  
                 private var _cache:IMap;//<VostokIdentification,LoadedAssetReport >
 48  
 
 49  
                 /**
 50  
                  * description
 51  
                  */
 52  
                 public function CachedAssetDataRepository()
 53  1
                 {
 54  1
                         _cache = new TypedMap(new HashMap(), VostokIdentification, CachedAssetData);
 55  1
                 }
 56  
                 
 57  
                 /**
 58  
                  * description
 59  
                  * 
 60  
                  * @throws         ArgumentError         if the <code>report</code> argument is <code>null</code>.
 61  
                  * @return
 62  
                  */
 63  
                 public function add(report:CachedAssetData): void
 64  
                 {
 65  1
                         if (!report) throw new ArgumentError("Argument <report> must not be null.");
 66  
                         
 67  1
                         if (_cache.containsKey(report.identification))
 68  
                         {
 69  0
                                 var message:String = "There is already a LoadedAssetReport object stored with VostokIdentification:\n";
 70  0
                                 message += "<" + report.identification + ">\n";
 71  0
                                 message += "Use the method <LoadedAssetRepository().exists()> to check if a LoadedAssetReport object already exists.\n";
 72  
                                 
 73  0
                                 throw new AssetDataAlreadyCachedError(report.identification, message);
 74  
                         }
 75  
                         
 76  1
                         _cache.put(report.identification, report);
 77  1
                 }
 78  
                 
 79  
                 /**
 80  
                  * description
 81  
                  * 
 82  
                  * @return
 83  
                   */
 84  
                 public function clear(): void
 85  
                 {
 86  1
                         _cache.clear();
 87  1
                 }
 88  
 
 89  
                 /**
 90  
                  * description
 91  
                  * 
 92  
                  * @param         assetId    
 93  
                  * @throws         ArgumentError         if the <code>identification</code> argument is <code>null</code>.
 94  
                  * @return
 95  
                  */
 96  
                 public function exists(identification:VostokIdentification): Boolean
 97  
                 {
 98  1
                         if (!identification) throw new ArgumentError("Argument <identification> must not be null.");
 99  
                         
 100  1
                         return _cache.containsKey(identification);
 101  
                 }
 102  
 
 103  
                 /**
 104  
                  * description
 105  
                  * 
 106  
                  * @param         identification    
 107  
                  * @throws         ArgumentError         if the <code>identification</code> argument is <code>null</code>.
 108  
                  * @return
 109  
                  */
 110  
                 public function find(identification:VostokIdentification): CachedAssetData
 111  
                 {
 112  1
                         if (!identification) throw new ArgumentError("Argument <identification> must not be null.");
 113  
                         
 114  1
                         return _cache.getValue(identification);
 115  
                 }
 116  
 
 117  
                 /**
 118  
                  * description
 119  
                  * 
 120  
                  * @return
 121  
                   */
 122  
                 public function findAll(): IList
 123  
                 {
 124  0
                         if (isEmpty()) return null;
 125  0
                         var l:IList = new ReadOnlyArrayList(_cache.getValues().toArray());
 126  
                         
 127  0
                         return l;
 128  
                 }
 129  
                 
 130  
                 /**
 131  
                  * description
 132  
                  * 
 133  
                  * @return
 134  
                  */
 135  
                 public function isEmpty(): Boolean
 136  
                 {
 137  0
                         return _cache.isEmpty();
 138  
                 }
 139  
 
 140  
                 /**
 141  
                  * description
 142  
                  * 
 143  
                  * @param         assetId    
 144  
                  * @throws         ArgumentError         if the <code>assetId</code> argument is <code>null</code> or <code>empty</code>.
 145  
                  * @return
 146  
                  */
 147  
                 public function remove(identification:VostokIdentification): Boolean
 148  
                 {
 149  0
                         if (!identification) throw new ArgumentError("Argument <identification> must not be null.");
 150  
                         
 151  0
                         return _cache.remove(identification) != null;
 152  
                 }
 153  
                 
 154  
                 /**
 155  
                  * description
 156  
                  * 
 157  
                  * @return
 158  
                  */
 159  
                 public function size(): int
 160  
                 {
 161  0
                         return _cache.size();
 162  
                 }
 163  
                 
 164  
                 /**
 165  
                  * description
 166  
                  * 
 167  
                  * @return
 168  
                  */
 169  
                 public function toString(): String
 170  
                 {
 171  0
                         return "[" + ReflectionUtil.getClassName(this) + "] <" + _cache.getValues() + ">";
 172  
                 }
 173  
 
 174  
         }
 175  
 
 176  
 }