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