| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AssetPackageRepository |
|
| 0.0;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.assets |
| 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.maps.ArrayListMap; | |
| 36 | import org.as3collections.maps.TypedMap; | |
| 37 | import org.as3utils.ReflectionUtil; | |
| 38 | import org.vostokframework.VostokIdentification; | |
| 39 | import org.vostokframework.domain.assets.errors.DuplicateAssetPackageError; | |
| 40 | ||
| 41 | /** | |
| 42 | * description | |
| 43 | * | |
| 44 | * @author Flávio Silva | |
| 45 | */ | |
| 46 | public class AssetPackageRepository | |
| 47 | { | |
| 48 | private var _assetPackageMap:IMap;//<VostokIdentification,AssetPackage> | |
| 49 | ||
| 50 | /** | |
| 51 | * description | |
| 52 | */ | |
| 53 | public function AssetPackageRepository() | |
| 54 | 1 | { |
| 55 | //TODO:mudar key para String para melhorar performance | |
| 56 | 1 | _assetPackageMap = new TypedMap(new ArrayListMap(), VostokIdentification, AssetPackage); |
| 57 | 1 | } |
| 58 | ||
| 59 | /** | |
| 60 | * description | |
| 61 | * | |
| 62 | * @param asset assetPackage | |
| 63 | * @throws ArgumentError if the <code>assetPackage</code> argument is <code>null</code>. | |
| 64 | * @throws org.vostokframework.domain.assets.errors.DuplicateAssetPackageError if already exists an <code>AssetPackage</code> object stored with the same <code>id</code> of the provided <code>assetPackage</code> argument. | |
| 65 | * @return | |
| 66 | */ | |
| 67 | public function add(assetPackage:AssetPackage): void | |
| 68 | { | |
| 69 | 1 | if (!assetPackage) throw new ArgumentError("Argument <assetPackage> must not be null."); |
| 70 | ||
| 71 | 1 | if (_assetPackageMap.containsKey(assetPackage.identification)) |
| 72 | { | |
| 73 | 1 | var message:String = "There is already an AssetPackage object stored with identification: "; |
| 74 | 1 | message += "<" + assetPackage.identification + ">\n"; |
| 75 | 1 | message += "Use the method <AssetPackageRepository().exists()> to check if an AssetPackage object already exists.\n"; |
| 76 | 1 | message += "For further information please read the documentation section about the AssetPackage object."; |
| 77 | ||
| 78 | 1 | throw new DuplicateAssetPackageError(assetPackage.identification, message); |
| 79 | } | |
| 80 | ||
| 81 | 1 | _assetPackageMap.put(assetPackage.identification, assetPackage); |
| 82 | 1 | } |
| 83 | ||
| 84 | /** | |
| 85 | * description | |
| 86 | * | |
| 87 | * @return | |
| 88 | */ | |
| 89 | public function clear(): void | |
| 90 | { | |
| 91 | 1 | _assetPackageMap.clear(); |
| 92 | 1 | } |
| 93 | ||
| 94 | /** | |
| 95 | * description | |
| 96 | * | |
| 97 | * @param assetId idPackageAsset | |
| 98 | * @throws ArgumentError if the <code>assetPackageId</code> argument is <code>null</code> or <code>empty</code>. | |
| 99 | * @return | |
| 100 | */ | |
| 101 | public function exists(identification:VostokIdentification): Boolean | |
| 102 | { | |
| 103 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
| 104 | ||
| 105 | 1 | return _assetPackageMap.containsKey(identification); |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * description | |
| 110 | * | |
| 111 | * @param assetPackageId idPackageAsset | |
| 112 | * @param localeId | |
| 113 | * @throws ArgumentError if the <code>assetPackageId</code> argument is <code>null</code> or <code>empty</code>. | |
| 114 | * @return | |
| 115 | */ | |
| 116 | public function find(identification:VostokIdentification): AssetPackage | |
| 117 | { | |
| 118 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
| 119 | ||
| 120 | 1 | return _assetPackageMap.getValue(identification); |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * description | |
| 125 | * | |
| 126 | * @return | |
| 127 | */ | |
| 128 | public function findAll(): IList | |
| 129 | { | |
| 130 | 1 | if (isEmpty()) return null; |
| 131 | 1 | var l:IList = new ReadOnlyArrayList(_assetPackageMap.getValues().toArray()); |
| 132 | ||
| 133 | 1 | return l; |
| 134 | } | |
| 135 | ||
| 136 | /** | |
| 137 | * description | |
| 138 | * | |
| 139 | * @param assetId idPackageAsset | |
| 140 | * @throws ArgumentError if the <code>assetId</code> argument is <code>null</code> or <code>empty</code>. | |
| 141 | * @return | |
| 142 | */ | |
| 143 | public function findAssetPackageByAssetId(identification:VostokIdentification): AssetPackage | |
| 144 | { | |
| 145 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
| 146 | ||
| 147 | 1 | if (isEmpty()) return null; |
| 148 | ||
| 149 | 1 | var itAssetPackages:IIterator = _assetPackageMap.getValues().iterator(); |
| 150 | var itAssets:IIterator; | |
| 151 | var assetPackage:AssetPackage; | |
| 152 | var asset:Asset; | |
| 153 | ||
| 154 | 1 | while (itAssetPackages.hasNext()) |
| 155 | { | |
| 156 | 1 | assetPackage = itAssetPackages.next(); |
| 157 | 1 | if (assetPackage.isEmpty()) continue; |
| 158 | ||
| 159 | 1 | itAssets = assetPackage.getAssets().iterator(); |
| 160 | 1 | while (itAssets.hasNext()) |
| 161 | { | |
| 162 | 1 | asset = itAssets.next(); |
| 163 | 1 | if (asset.identification.equals(identification)) return assetPackage; |
| 164 | } | |
| 165 | } | |
| 166 | ||
| 167 | 0 | return null; |
| 168 | } | |
| 169 | ||
| 170 | /** | |
| 171 | * description | |
| 172 | * | |
| 173 | * @return | |
| 174 | */ | |
| 175 | public function isEmpty(): Boolean | |
| 176 | { | |
| 177 | 1 | return _assetPackageMap.isEmpty(); |
| 178 | } | |
| 179 | ||
| 180 | /** | |
| 181 | * description | |
| 182 | * | |
| 183 | * @param assetPackageId | |
| 184 | * @param localeId | |
| 185 | * @throws ArgumentError if the <code>assetPackageId</code> argument is <code>null</code> or <code>empty</code>. | |
| 186 | * @return | |
| 187 | */ | |
| 188 | public function remove(identification:VostokIdentification): Boolean | |
| 189 | { | |
| 190 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
| 191 | ||
| 192 | 1 | return _assetPackageMap.remove(identification) != null; |
| 193 | } | |
| 194 | ||
| 195 | /** | |
| 196 | * description | |
| 197 | * | |
| 198 | * @return | |
| 199 | */ | |
| 200 | public function size(): int | |
| 201 | { | |
| 202 | 1 | return _assetPackageMap.size(); |
| 203 | } | |
| 204 | ||
| 205 | /** | |
| 206 | * description | |
| 207 | * | |
| 208 | * @return | |
| 209 | */ | |
| 210 | public function toString(): String | |
| 211 | { | |
| 212 | 0 | return "[" + ReflectionUtil.getClassName(this) + "] <" + _assetPackageMap.getValues() + ">"; |
| 213 | } | |
| 214 | ||
| 215 | } | |
| 216 | ||
| 217 | } |