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