View Javadoc
1   /*-
2    * #%L
3    * io.earcam.instrumental.module.osgi
4    * %%
5    * Copyright (C) 2018 earcam
6    * %%
7    * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
8    * 
9    * You <b>must</b> choose to accept, in full - any individual or combination of 
10   * the following licenses:
11   * <ul>
12   * 	<li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li>
13   * 	<li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li>
14   * 	<li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li>
15   * 	<li><a href="https://opensource.org/licenses/MIT">MIT</a></li>
16   * </ul>
17   * #L%
18   */
19  package io.earcam.instrumental.module.osgi;
20  
21  import static io.earcam.instrumental.module.manifest.ManifestInfoBuilder.attribute;
22  import static io.earcam.instrumental.module.osgi.BundleManifestHeaders.BUNDLE_MANIFESTVERSION;
23  
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Objects;
29  import java.util.jar.Attributes.Name;
30  import java.util.Map.Entry;
31  
32  import io.earcam.instrumental.module.manifest.AbstractManifestBuilder;
33  
34  class DefaultBundleInfo extends AbstractManifestBuilder<BundleInfoBuilder> implements BundleInfo, BundleInfoBuilder {
35  
36  	private static final String BUNDLE_MANIFESTVERSION_RELEASE_4 = "2";
37  	private final Map<Name, List<Clause>> clauses = new HashMap<>();
38  
39  
40  	@Override
41  	public boolean equals(Object other)
42  	{
43  		return super.equals(other);
44  	}
45  
46  
47  	@Override
48  	public boolean equals(AbstractManifestBuilder<?> that)
49  	{
50  		return that instanceof DefaultBundleInfo
51  				&& same(that)
52  				&& Objects.deepEquals(((DefaultBundleInfo) that).clauses, this.clauses);
53  	}
54  
55  
56  	@Override
57  	public int hashCode()
58  	{
59  		return Objects.hash(super.hashCode(), clauses);
60  	}
61  
62  
63  	@Override
64  	public BundleInfoBuilder headerClause(Name header, Clause clause)
65  	{
66  		clauses.computeIfAbsent(header, h -> new ArrayList<>()).add(clause);
67  		return self();
68  	}
69  
70  
71  	@Override
72  	protected DefaultBundleInfo self()
73  	{
74  		return this;
75  	}
76  
77  
78  	@Override
79  	protected void preBuildHook()
80  	{
81  		if(!mainAttributes.containsKey(BUNDLE_MANIFESTVERSION.header())) {
82  			bundleManifestVersion(BUNDLE_MANIFESTVERSION_RELEASE_4);
83  		}
84  		for(Entry<Name, List<Clause>> e : clauses.entrySet()) {
85  			manifestMain(attribute(e.getKey(), Clause.allToString(e.getValue())));
86  		}
87  	}
88  
89  
90  	@Override
91  	public BundleInfo construct()
92  	{
93  		hook();
94  		return this;
95  	}
96  
97  
98  	@Override
99  	public BundleInfoBuilder deconstruct()
100 	{
101 		unhook();
102 		return this;
103 	}
104 
105 
106 	@Override
107 	public Map<Name, List<Clause>> allHeaderClauses()
108 	{
109 		return clauses;
110 	}
111 }