Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame^] | 1 | package aQute.lib.osgi.resource; |
| 2 | |
| 3 | import java.util.LinkedList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import org.osgi.resource.Capability; |
| 7 | import org.osgi.resource.Requirement; |
| 8 | import org.osgi.resource.Resource; |
| 9 | |
| 10 | public class ResourceBuilder { |
| 11 | |
| 12 | private final ResourceImpl resource = new ResourceImpl(); |
| 13 | private final List<Capability> capabilities = new LinkedList<Capability>(); |
| 14 | private final List<Requirement> requirements = new LinkedList<Requirement>(); |
| 15 | |
| 16 | private boolean built = false; |
| 17 | |
| 18 | public ResourceBuilder addCapability(CapReqBuilder builder) { |
| 19 | if (built) |
| 20 | throw new IllegalStateException("Resource already built"); |
| 21 | |
| 22 | Capability cap = builder.setResource(resource).buildCapability(); |
| 23 | capabilities.add(cap); |
| 24 | |
| 25 | return this; |
| 26 | } |
| 27 | |
| 28 | public ResourceBuilder addRequirement(CapReqBuilder builder) { |
| 29 | if (built) |
| 30 | throw new IllegalStateException("Resource already built"); |
| 31 | |
| 32 | Requirement req = builder.setResource(resource).buildRequirement(); |
| 33 | requirements.add(req); |
| 34 | |
| 35 | return this; |
| 36 | } |
| 37 | |
| 38 | public Resource build() { |
| 39 | if (built) |
| 40 | throw new IllegalStateException("Resource already built"); |
| 41 | built = true; |
| 42 | |
| 43 | resource.setCapabilities(capabilities); |
| 44 | resource.setRequirements(requirements); |
| 45 | return resource; |
| 46 | } |
| 47 | |
| 48 | } |