| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AssetPackage |
|
| 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.lists.ArrayList; | |
| 34 | import org.as3collections.lists.ReadOnlyArrayList; | |
| 35 | import org.as3collections.utils.ListUtil; | |
| 36 | import org.as3coreaddendum.system.IDisposable; | |
| 37 | import org.as3coreaddendum.system.IEquatable; | |
| 38 | import org.as3utils.ReflectionUtil; | |
| 39 | import org.vostokframework.VostokIdentification; | |
| 40 | ||
| 41 | /** | |
| 42 | * description | |
| 43 | * | |
| 44 | * @author Flávio Silva | |
| 45 | */ | |
| 46 | public class AssetPackage implements IEquatable, IDisposable | |
| 47 | { | |
| 48 | private var _assets:IList; | |
| 49 | private var _identification:VostokIdentification; | |
| 50 | ||
| 51 | /** | |
| 52 | * description | |
| 53 | */ | |
| 54 | public function get identification(): VostokIdentification { return _identification; } | |
| 55 | ||
| 56 | /** | |
| 57 | * description | |
| 58 | * | |
| 59 | * @param id | |
| 60 | * @param locale | |
| 61 | * @throws ArgumentError if the <code>id</code> argument is <code>null</code> or <code>empty</code>. | |
| 62 | * @throws ArgumentError if the <code>locale</code> argument is <code>null</code> or <code>empty</code>. | |
| 63 | */ | |
| 64 | public function AssetPackage(identification:VostokIdentification) | |
| 65 | 1 | { |
| 66 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
| 67 | ||
| 68 | 1 | _identification = identification; |
| 69 | 1 | _assets = ListUtil.getUniqueTypedList(new ArrayList(), Asset); |
| 70 | 1 | } |
| 71 | ||
| 72 | /** | |
| 73 | * description | |
| 74 | * | |
| 75 | * @param asset | |
| 76 | * @throws ArgumentError if the <code>asset</code> argument is <code>null</code>. | |
| 77 | * @return | |
| 78 | */ | |
| 79 | public function addAsset(asset:Asset): Boolean | |
| 80 | { | |
| 81 | 1 | if (!asset) throw new ArgumentError("Argument <asset> must not be null."); |
| 82 | 1 | if (identification.locale != asset.identification.locale) |
| 83 | { | |
| 84 | 0 | var errorMessage:String = "The <locale> property of Asset and AssetPackage objects must match.\n"; |
| 85 | 0 | errorMessage += "AssetPackage: <" + AssetPackage + ">\n"; |
| 86 | 0 | errorMessage += "Asset: <" + Asset + ">\n"; |
| 87 | 0 | errorMessage += "For further information please read the documentation section about the AssetPackage object."; |
| 88 | 0 | throw new ArgumentError(errorMessage); |
| 89 | } | |
| 90 | ||
| 91 | 1 | return _assets.add(asset); |
| 92 | } | |
| 93 | ||
| 94 | /** | |
| 95 | * description | |
| 96 | * | |
| 97 | * @param assets | |
| 98 | * @throws ArgumentError if the <code>assets</code> argument is <code>null</code>. | |
| 99 | * @throws ArgumentError if the <code>assets</code> argument contains any <code>null</code> element. | |
| 100 | * @return | |
| 101 | */ | |
| 102 | public function addAssets(assets:IList): Boolean | |
| 103 | { | |
| 104 | 1 | if (!assets) throw new ArgumentError("Argument <assets> must not be null."); |
| 105 | ||
| 106 | 1 | var it:IIterator = assets.iterator(); |
| 107 | 1 | while (it.hasNext()) |
| 108 | { | |
| 109 | 1 | if (!it.next()) throw new ArgumentError("Argument <assets> must not contain any null element. A null element was found at index <" + it.pointer() + ">"); |
| 110 | } | |
| 111 | ||
| 112 | 1 | return _assets.addAll(assets); |
| 113 | } | |
| 114 | ||
| 115 | /** | |
| 116 | * description | |
| 117 | * | |
| 118 | * @return | |
| 119 | */ | |
| 120 | public function clear(): void | |
| 121 | { | |
| 122 | 1 | _assets.clear(); |
| 123 | 1 | } |
| 124 | ||
| 125 | /** | |
| 126 | * description | |
| 127 | * | |
| 128 | * @param assetId | |
| 129 | * @throws ArgumentError if the <code>assetId</code> argument is <code>null</code> or <code>empty</code>. | |
| 130 | * @return | |
| 131 | */ | |
| 132 | public function containsAsset(identification:VostokIdentification): Boolean | |
| 133 | { | |
| 134 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
| 135 | ||
| 136 | 1 | if (isEmpty()) return false; |
| 137 | ||
| 138 | 1 | var asset:Asset = getAsset(identification); |
| 139 | 1 | return _assets.contains(asset); |
| 140 | } | |
| 141 | ||
| 142 | public function dispose():void | |
| 143 | { | |
| 144 | 0 | clear(); |
| 145 | 0 | _assets = null; |
| 146 | 0 | } |
| 147 | ||
| 148 | public function equals(other : *): Boolean | |
| 149 | { | |
| 150 | 0 | if (this == other) return true; |
| 151 | 0 | if (!(other is AssetPackage)) return false; |
| 152 | ||
| 153 | 0 | var otherAssetPackage:AssetPackage = other as AssetPackage; |
| 154 | 0 | return identification.equals(otherAssetPackage.identification); |
| 155 | } | |
| 156 | ||
| 157 | /** | |
| 158 | * description | |
| 159 | * | |
| 160 | * @param id | |
| 161 | * @throws ArgumentError if the <code>assetId</code> argument is <code>null</code> or <code>empty</code>. | |
| 162 | * @return | |
| 163 | */ | |
| 164 | public function getAsset(identification:VostokIdentification): Asset | |
| 165 | { | |
| 166 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
| 167 | ||
| 168 | 1 | if (isEmpty()) return null; |
| 169 | ||
| 170 | var asset:Asset; | |
| 171 | 1 | var it:IIterator = _assets.iterator(); |
| 172 | ||
| 173 | 1 | while (it.hasNext()) |
| 174 | { | |
| 175 | 1 | asset = it.next(); |
| 176 | 1 | if (asset.identification.equals(identification)) return asset; |
| 177 | } | |
| 178 | ||
| 179 | 1 | return null; |
| 180 | } | |
| 181 | ||
| 182 | /** | |
| 183 | * description | |
| 184 | * | |
| 185 | * @return | |
| 186 | */ | |
| 187 | public function getAssets(): ReadOnlyArrayList | |
| 188 | { | |
| 189 | 1 | if (isEmpty()) return null; |
| 190 | 1 | return new ReadOnlyArrayList(_assets.toArray()); |
| 191 | } | |
| 192 | ||
| 193 | /** | |
| 194 | * description | |
| 195 | * | |
| 196 | * @return | |
| 197 | */ | |
| 198 | public function isEmpty(): Boolean | |
| 199 | { | |
| 200 | 1 | return _assets.isEmpty(); |
| 201 | } | |
| 202 | ||
| 203 | /** | |
| 204 | * description | |
| 205 | * | |
| 206 | * @param id | |
| 207 | * @throws ArgumentError if the <code>assetId</code> argument is <code>null</code> or <code>empty</code>. | |
| 208 | * @return | |
| 209 | */ | |
| 210 | public function removeAsset(identification:VostokIdentification): Boolean | |
| 211 | { | |
| 212 | 1 | if (!identification) throw new ArgumentError("Argument <identification> must not be null."); |
| 213 | ||
| 214 | 1 | if (isEmpty()) return false; |
| 215 | ||
| 216 | 1 | var asset:Asset = getAsset(identification); |
| 217 | 1 | return _assets.remove(asset); |
| 218 | } | |
| 219 | ||
| 220 | /** | |
| 221 | * description | |
| 222 | * | |
| 223 | * @param assets | |
| 224 | * @throws ArgumentError if the <code>assets</code> argument is <code>null</code>. | |
| 225 | * @return | |
| 226 | */ | |
| 227 | public function removeAssets(assets:IList): Boolean | |
| 228 | { | |
| 229 | 1 | if (!assets) throw new ArgumentError("Argument <assets> must not be null."); |
| 230 | ||
| 231 | 1 | if (isEmpty()) return false; |
| 232 | 1 | return _assets.removeAll(assets); |
| 233 | } | |
| 234 | ||
| 235 | /** | |
| 236 | * description | |
| 237 | * | |
| 238 | * @return | |
| 239 | */ | |
| 240 | public function size(): int | |
| 241 | { | |
| 242 | 1 | return _assets.size(); |
| 243 | } | |
| 244 | ||
| 245 | /** | |
| 246 | * description | |
| 247 | * | |
| 248 | * @return | |
| 249 | */ | |
| 250 | public function toString(): String | |
| 251 | { | |
| 252 | 0 | return "[" + ReflectionUtil.getClassName(this) + " identification <" + identification + ">]"; |
| 253 | } | |
| 254 | ||
| 255 | } | |
| 256 | ||
| 257 | } |