]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.init/src/org/argeo/init/a2/A2Branch.java
Improve initialisation.
[lgpl/argeo-commons.git] / org.argeo.init / src / org / argeo / init / a2 / A2Branch.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.argeo.init.osgi.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 public Iterable<A2Module> listModules(Object filter) {
28 return modules.values();
29 }
30
31 A2Module getOrAddModule(Version version, Object locator) {
32 if (modules.containsKey(version)) {
33 A2Module res = modules.get(version);
34 if (OsgiBootUtils.isDebug() && !res.getLocator().equals(locator)) {
35 OsgiBootUtils.debug("Inconsistent locator " + locator + " (registered: " + res.getLocator() + ")");
36 }
37 return res;
38 } else
39 return new A2Module(this, version, locator);
40 }
41
42 public A2Module last() {
43 return modules.get(modules.lastKey());
44 }
45
46 public A2Module first() {
47 return modules.get(modules.firstKey());
48 }
49
50 public A2Component getComponent() {
51 return component;
52 }
53
54 public String getId() {
55 return id;
56 }
57
58 @Override
59 public int compareTo(A2Branch o) {
60 return id.compareTo(id);
61 }
62
63 @Override
64 public int hashCode() {
65 return id.hashCode();
66 }
67
68 @Override
69 public boolean equals(Object obj) {
70 if (obj instanceof A2Branch) {
71 A2Branch o = (A2Branch) obj;
72 return component.equals(o.component) && id.equals(o.id);
73 } else
74 return false;
75 }
76
77 @Override
78 public String toString() {
79 return getCoordinates();
80 }
81
82 public String getCoordinates() {
83 return component + ":" + id;
84 }
85
86 static String versionToBranchId(Version version) {
87 return version.getMajor() + "." + version.getMinor();
88 }
89 }