]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/api/a2/A2Component.java
Clarify ACR API
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / api / a2 / A2Component.java
1 package org.argeo.api.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 A2Branch a2Branch = new A2Branch(this, branchId);
33 branches.put(branchId, a2Branch);
34 }
35 return branches.get(branchId);
36 }
37
38 A2Module getOrAddModule(Version version, Object locator) {
39 A2Branch branch = getOrAddBranch(A2Branch.versionToBranchId(version));
40 A2Module module = branch.getOrAddModule(version, locator);
41 return module;
42 }
43
44 public A2Branch last() {
45 return branches.get(branches.lastKey());
46 }
47
48 public A2Contribution getContribution() {
49 return contribution;
50 }
51
52 public String getId() {
53 return id;
54 }
55
56 @Override
57 public int compareTo(A2Component o) {
58 return id.compareTo(o.id);
59 }
60
61 @Override
62 public int hashCode() {
63 return id.hashCode();
64 }
65
66 @Override
67 public boolean equals(Object obj) {
68 if (obj instanceof A2Component) {
69 A2Component o = (A2Component) obj;
70 return contribution.equals(o.contribution) && id.equals(o.id);
71 } else
72 return false;
73 }
74
75 @Override
76 public String toString() {
77 return contribution.getId() + ":" + id;
78 }
79
80 void asTree(String prefix, StringBuffer buf) {
81 if (prefix == null)
82 prefix = "";
83 A2Branch lastBranch = last();
84 SortedMap<String, A2Branch> displayMap = new TreeMap<>(Collections.reverseOrder());
85 displayMap.putAll(branches);
86 for (String branchId : displayMap.keySet()) {
87 A2Branch branch = displayMap.get(branchId);
88 if (!lastBranch.equals(branch)) {
89 buf.append('\n');
90 buf.append(prefix);
91 } else {
92 buf.append(" -");
93 }
94 buf.append(prefix);
95 buf.append(branchId);
96 A2Module first = branch.first();
97 A2Module last = branch.last();
98 buf.append(" (").append(last.getVersion());
99 if (!first.equals(last))
100 buf.append(" ... ").append(first.getVersion());
101 buf.append(')');
102 }
103 }
104
105 }