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