]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/a2/A2Component.java
Improve initialisation.
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / a2 / A2Component.java
1 package org.argeo.init.a2;
2
3 import java.util.Collections;
4 import java.util.SortedMap;
5 import java.util.TreeMap;
6
7 import org.osgi.framework.Version;
8
9 /**
10 * The logical name of a software package. In OSGi's case this is
11 * <code>Bundle-SymbolicName</code>. This is the equivalent of Maven's artifact
12 * id.
13 */
14 public class A2Component implements Comparable<A2Component> {
15 private final A2Contribution contribution;
16 private final String id;
17
18 final SortedMap<String, A2Branch> branches = Collections.synchronizedSortedMap(new TreeMap<>());
19
20 public A2Component(A2Contribution contribution, String id) {
21 this.contribution = contribution;
22 this.id = id;
23 contribution.components.put(id, this);
24 }
25
26 public Iterable<A2Branch> listBranches(Object filter) {
27 return branches.values();
28 }
29
30 A2Branch getOrAddBranch(String branchId) {
31 if (branches.containsKey(branchId))
32 return branches.get(branchId);
33 else
34 return new A2Branch(this, branchId);
35 }
36
37 A2Module getOrAddModule(Version version, Object locator) {
38 A2Branch branch = getOrAddBranch(A2Branch.versionToBranchId(version));
39 A2Module module = branch.getOrAddModule(version, locator);
40 return module;
41 }
42
43 public A2Branch last() {
44 return branches.get(branches.lastKey());
45 }
46
47 public A2Contribution getContribution() {
48 return contribution;
49 }
50
51 public String getId() {
52 return id;
53 }
54
55 @Override
56 public int compareTo(A2Component o) {
57 return id.compareTo(o.id);
58 }
59
60 @Override
61 public int hashCode() {
62 return id.hashCode();
63 }
64
65 @Override
66 public boolean equals(Object obj) {
67 if (obj instanceof A2Component) {
68 A2Component o = (A2Component) obj;
69 return contribution.equals(o.contribution) && id.equals(o.id);
70 } else
71 return false;
72 }
73
74 @Override
75 public String toString() {
76 return contribution.getId() + ":" + id;
77 }
78
79 void asTree(String prefix, StringBuffer buf) {
80 if (prefix == null)
81 prefix = "";
82 A2Branch lastBranch = last();
83 SortedMap<String, A2Branch> displayMap = new TreeMap<>(Collections.reverseOrder());
84 displayMap.putAll(branches);
85 for (String branchId : displayMap.keySet()) {
86 A2Branch branch = displayMap.get(branchId);
87 if (!lastBranch.equals(branch)) {
88 buf.append('\n');
89 buf.append(prefix);
90 } else {
91 buf.append(" -");
92 }
93 buf.append(prefix);
94 buf.append(branchId);
95 A2Module first = branch.first();
96 A2Module last = branch.last();
97 buf.append(" (").append(last.getVersion());
98 if (!first.equals(last))
99 buf.append(" ... ").append(first.getVersion());
100 buf.append(')');
101 }
102 }
103
104 }