]> git.argeo.org Git - lgpl/argeo-commons.git/blob - a2/A2Component.java
Prepare next development cycle
[lgpl/argeo-commons.git] / a2 / A2Component.java
1 package org.argeo.osgi.boot.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 class A2Component implements Comparable<A2Component> {
10 private final A2Contribution contribution;
11 private final String id;
12
13 final SortedMap<String, A2Branch> branches = Collections.synchronizedSortedMap(new TreeMap<>());
14
15 public A2Component(A2Contribution contribution, String id) {
16 this.contribution = contribution;
17 this.id = id;
18 contribution.components.put(id, this);
19 }
20
21 A2Branch getOrAddBranch(String branchId) {
22 if (branches.containsKey(branchId))
23 return branches.get(branchId);
24 else
25 return new A2Branch(this, branchId);
26 }
27
28 A2Module getOrAddModule(Version version, Object locator) {
29 A2Branch branch = getOrAddBranch(A2Branch.versionToBranchId(version));
30 A2Module module = branch.getOrAddModule(version, locator);
31 return module;
32 }
33
34 A2Branch last() {
35 return branches.get(branches.lastKey());
36 }
37
38 A2Contribution getContribution() {
39 return contribution;
40 }
41
42 String getId() {
43 return id;
44 }
45
46 @Override
47 public int compareTo(A2Component o) {
48 return id.compareTo(o.id);
49 }
50
51 @Override
52 public int hashCode() {
53 return id.hashCode();
54 }
55
56 @Override
57 public boolean equals(Object obj) {
58 if (obj instanceof A2Component) {
59 A2Component o = (A2Component) obj;
60 return contribution.equals(o.contribution) && id.equals(o.id);
61 } else
62 return false;
63 }
64
65 @Override
66 public String toString() {
67 return contribution.getId() + ":" + id;
68 }
69
70 void asTree(String prefix, StringBuffer buf) {
71 if (prefix == null)
72 prefix = "";
73 A2Branch lastBranch = last();
74 SortedMap<String, A2Branch> displayMap = new TreeMap<>(Collections.reverseOrder());
75 displayMap.putAll(branches);
76 for (String branchId : displayMap.keySet()) {
77 A2Branch branch = displayMap.get(branchId);
78 if (!lastBranch.equals(branch)) {
79 buf.append('\n');
80 buf.append(prefix);
81 } else {
82 buf.append(" -");
83 }
84 buf.append(prefix);
85 buf.append(branchId);
86 A2Module first = branch.first();
87 A2Module last = branch.last();
88 buf.append(" (").append(last.getVersion());
89 if (!first.equals(last))
90 buf.append(" ... ").append(first.getVersion());
91 buf.append(')');
92 }
93 }
94
95 }