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.argeo.osgi.boot.OsgiBootUtils;
8   import org.osgi.framework.Version;
9   
10  /**
11   * A logical linear sequence of versions of a given {@link A2Component}. This is
12   * typically a combination of major and minor version, indicating backward
13   * compatibility.
14   */
15  public class A2Branch implements Comparable<A2Branch> {
16  	private final A2Component component;
17  	private final String id;
18  
19  	final SortedMap<Version, A2Module> modules = Collections.synchronizedSortedMap(new TreeMap<>());
20  
21  	public A2Branch(A2Component component, String id) {
22  		this.component = component;
23  		this.id = id;
24  		component.branches.put(id, this);
25  	}
26  
27  	A2Module getOrAddModule(Version version, Object locator) {
28  		if (modules.containsKey(version)) {
29  			A2Module res = modules.get(version);
30  			if (OsgiBootUtils.isDebug() && !res.getLocator().equals(locator)) {
31  				OsgiBootUtils.debug("Inconsistent locator " + locator + " (registered: " + res.getLocator() + ")");
32  			}
33  			return res;
34  		} else
35  			return new A2Module(this, version, locator);
36  	}
37  
38  	A2Module last() {
39  		return modules.get(modules.lastKey());
40  	}
41  
42  	A2Module first() {
43  		return modules.get(modules.firstKey());
44  	}
45  
46  	A2Component getComponent() {
47  		return component;
48  	}
49  
50  	String getId() {
51  		return id;
52  	}
53  
54  	@Override
55  	public int compareTo(A2Branch o) {
56  		return id.compareTo(id);
57  	}
58  
59  	@Override
60  	public int hashCode() {
61  		return id.hashCode();
62  	}
63  
64  	@Override
65  	public boolean equals(Object obj) {
66  		if (obj instanceof A2Branch) {
67  			A2Branch o = (A2Branch) obj;
68  			return component.equals(o.component) && id.equals(o.id);
69  		} else
70  			return false;
71  	}
72  
73  	@Override
74  	public String toString() {
75  		return getCoordinates();
76  	}
77  
78  	public String getCoordinates() {
79  		return component + ":" + id;
80  	}
81  
82  	static String versionToBranchId(Version version) {
83  		return version.getMajor() + "." + version.getMinor();
84  	}
85  }