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