Stuart McCulloch | 3fdcd85 | 2011-10-17 10:31:43 +0000 | [diff] [blame] | 1 | package aQute.bnd.component; |
| 2 | |
| 3 | import org.osgi.service.component.annotations.*; |
| 4 | |
| 5 | import aQute.lib.osgi.*; |
| 6 | import aQute.lib.tag.*; |
| 7 | import aQute.libg.version.*; |
| 8 | |
| 9 | class ReferenceDef { |
| 10 | Version version; |
| 11 | String name; |
| 12 | String interfce; |
| 13 | ReferenceCardinality cardinality; |
| 14 | ReferencePolicy policy; |
| 15 | String target; |
| 16 | String bind; |
| 17 | String unbind; |
| 18 | String modified; |
| 19 | |
| 20 | public void prepare(Analyzer analyzer) { |
| 21 | if (name == null) |
| 22 | analyzer.error("No name for a reference"); |
| 23 | |
| 24 | if (version == null) |
| 25 | version = AnnotationReader.V1_1; |
| 26 | |
| 27 | } |
| 28 | |
| 29 | public Tag getTag() { |
| 30 | Tag ref = new Tag("reference"); |
| 31 | ref.addAttribute("name", name); |
| 32 | if (cardinality != null) |
| 33 | ref.addAttribute("cardinality", p(cardinality)); |
| 34 | if (policy != null) |
| 35 | ref.addAttribute("policy", p(policy)); |
| 36 | |
| 37 | if (interfce != null) |
| 38 | ref.addAttribute("interface", interfce); |
| 39 | |
| 40 | if (target != null) |
| 41 | ref.addAttribute("target", target); |
| 42 | |
| 43 | if (bind != null) |
| 44 | ref.addAttribute("bind", bind); |
| 45 | |
| 46 | if (unbind != null) |
| 47 | ref.addAttribute("unbind", unbind); |
| 48 | |
| 49 | if (modified != null) { |
| 50 | ref.addAttribute("modified", modified); |
| 51 | version = max(version, AnnotationReader.V1_2); |
| 52 | } |
| 53 | |
| 54 | return ref; |
| 55 | } |
| 56 | |
| 57 | private String p(ReferencePolicy policy) { |
| 58 | switch(policy) { |
| 59 | case DYNAMIC: |
| 60 | return "dynamic"; |
| 61 | |
| 62 | case STATIC: |
| 63 | return "static"; |
| 64 | } |
| 65 | return policy.toString(); |
| 66 | } |
| 67 | |
| 68 | private String p(ReferenceCardinality crd) { |
| 69 | switch (crd) { |
| 70 | case AT_LEAST_ONE: |
| 71 | return "1..n"; |
| 72 | |
| 73 | case MANDATORY: |
| 74 | return "1..1"; |
| 75 | |
| 76 | case MULTIPLE: |
| 77 | return "0..n"; |
| 78 | |
| 79 | case OPTIONAL: |
| 80 | return "0..1"; |
| 81 | |
| 82 | } |
| 83 | return crd.toString(); |
| 84 | } |
| 85 | |
| 86 | private <T extends Comparable<T>> T max(T a, T b) { |
| 87 | int n = a.compareTo(b); |
| 88 | if (n >= 0) |
| 89 | return a; |
| 90 | else |
| 91 | return b; |
| 92 | } |
| 93 | |
| 94 | public String toString() { |
| 95 | return name; |
| 96 | } |
| 97 | |
| 98 | } |