blob: a355326a0082d7c5269db0254f37cbe61aef4937 [file] [log] [blame]
Stuart McCulloch26e7a5a2011-10-17 10:31:43 +00001package aQute.lib.deployer.obr;
2
3import java.util.Collections;
4import java.util.LinkedList;
5import java.util.List;
6
7/**
8 * @immutable
9 * @author Neil Bartlett
10 */
11public class Resource {
12
13 private final String id;
14 private final String presentationName;
15 private final String symbolicName;
16 private final String baseUrl;
17 private final String url;
18 private final String version;
19 private final List<Capability> capabilities;
20 private final List<Require> requires;
21
22 private Resource(String id, String presentationName, String symbolicName, String baseUrl, String url, String version, List<Capability> capabilities, List<Require> requires) {
23 this.id = id;
24 this.presentationName = presentationName;
25 this.symbolicName = symbolicName;
26 this.baseUrl = baseUrl;
27 this.url = url;
28 this.version = version;
29
30 this.capabilities = capabilities;
31 this.requires = requires;
32 }
33
34 public static class Builder {
35 private String id;
36 private String presentationName;
37 private String symbolicName;
38 private String baseUrl;
39 private String url;
40 private String version;
41 private final List<Capability> capabilities = new LinkedList<Capability>();
42 private final List<Require> requires = new LinkedList<Require>();
43
44 public Builder setId(String id) {
45 this.id = id;
46 return this;
47 }
48 public Builder setPresentationName(String presentationName) {
49 this.presentationName = presentationName;
50 return this;
51 }
52 public Builder setSymbolicName(String symbolicName) {
53 this.symbolicName = symbolicName;
54 return this;
55 }
56 public Builder setBaseUrl(String baseUrl) {
57 this.baseUrl = baseUrl;
58 return this;
59 }
60 public Builder setUrl(String url) {
61 this.url = url;
62 return this;
63 }
64 public Builder setVersion(String version) {
65 this.version = version;
66 return this;
67 }
68 public Builder addCapability(Capability capability) {
69 this.capabilities.add(capability);
70 return this;
71 }
72 public Builder addCapability(Capability.Builder capabilityBuilder) {
73 this.capabilities.add(capabilityBuilder.build());
74 return this;
75 }
76 public Builder addRequire(Require require) {
77 this.requires.add(require);
78 return this;
79 }
80
81 public Resource build() {
82 if (id == null) throw new IllegalStateException("'id' field is not initialised");
83 if (symbolicName == null) throw new IllegalStateException("'symbolicName' field is not initialised");
84 if (url == null) throw new IllegalStateException("'url' field is not initialised");
85
86 return new Resource(id, presentationName, symbolicName, baseUrl, url, version, Collections.unmodifiableList(capabilities), Collections.unmodifiableList(requires));
87 }
88 }
89
90 public String getId() {
91 return id;
92 }
93
94 public String getPresentationName() {
95 return presentationName;
96 }
97
98 public String getSymbolicName() {
99 return symbolicName;
100 }
101
102 public String getBaseUrl() {
103 return baseUrl;
104 }
105
106 public String getUrl() {
107 return url;
108 }
109
110 public String getVersion() {
111 return version;
112 }
113
114 public List<Capability> getCapabilities() {
115 return capabilities;
116 }
117
118 public Capability findPackageCapability(String pkgName) {
119 for (Capability capability : capabilities) {
120 if (CapabilityType.PACKAGE.getTypeName().equals(capability.getName())) {
121 List<Property> props = capability.getProperties();
122 for (Property prop : props) {
123 if (Property.PACKAGE.equals(prop.getName())) {
124 if (pkgName.equals(prop.getValue()))
125 return capability;
126 else
127 break;
128 }
129 }
130 }
131 }
132 return null;
133 }
134
135
136
137 public List<Require> getRequires() {
138 return requires;
139 }
140
141 public Require findRequire(String name) {
142 for (Require require : requires) {
143 if (name.equals(require.getName()))
144 return require;
145 }
146 return null;
147 }
148
149 public Require findPackageRequire(String usesPkgName) {
150 String matchString = String.format("(package=%s)", usesPkgName);
151
152 for (Require require : requires) {
153 if (CapabilityType.PACKAGE.getTypeName().equals(require.getName())) {
154 String filter = require.getFilter();
155 if (filter.indexOf(matchString) > -1)
156 return require;
157 }
158 }
159 return null;
160 }
161
162 @Override
163 public String toString() {
164 StringBuilder builder = new StringBuilder();
165 builder.append("Resource [id=").append(id)
166 .append(", presentationName=").append(presentationName)
167 .append(", symbolicName=").append(symbolicName)
168 .append(", baseUrl=").append(baseUrl)
169 .append(", url=").append(url).append(", version=")
170 .append(version).append(", capabilities=").append(capabilities)
171 .append("]");
172 return builder.toString();
173 }
174
175 @Override
176 public int hashCode() {
177 final int prime = 31;
178 int result = 1;
179 result = prime * result + ((baseUrl == null) ? 0 : baseUrl.hashCode());
180 result = prime * result
181 + ((capabilities == null) ? 0 : capabilities.hashCode());
182 result = prime * result + ((id == null) ? 0 : id.hashCode());
183 result = prime
184 * result
185 + ((presentationName == null) ? 0 : presentationName.hashCode());
186 result = prime * result
187 + ((symbolicName == null) ? 0 : symbolicName.hashCode());
188 result = prime * result + ((url == null) ? 0 : url.hashCode());
189 result = prime * result + ((version == null) ? 0 : version.hashCode());
190 return result;
191 }
192
193 @Override
194 public boolean equals(Object obj) {
195 if (this == obj)
196 return true;
197 if (obj == null)
198 return false;
199 if (getClass() != obj.getClass())
200 return false;
201 Resource other = (Resource) obj;
202 if (baseUrl == null) {
203 if (other.baseUrl != null)
204 return false;
205 } else if (!baseUrl.equals(other.baseUrl))
206 return false;
207 if (capabilities == null) {
208 if (other.capabilities != null)
209 return false;
210 } else if (!capabilities.equals(other.capabilities))
211 return false;
212 if (id == null) {
213 if (other.id != null)
214 return false;
215 } else if (!id.equals(other.id))
216 return false;
217 if (presentationName == null) {
218 if (other.presentationName != null)
219 return false;
220 } else if (!presentationName.equals(other.presentationName))
221 return false;
222 if (symbolicName == null) {
223 if (other.symbolicName != null)
224 return false;
225 } else if (!symbolicName.equals(other.symbolicName))
226 return false;
227 if (url == null) {
228 if (other.url != null)
229 return false;
230 } else if (!url.equals(other.url))
231 return false;
232 if (version == null) {
233 if (other.version != null)
234 return false;
235 } else if (!version.equals(other.version))
236 return false;
237 return true;
238 }
239
240}