1 package com.atlassian.marketplace.client.model;
2
3 import com.atlassian.fugue.Option;
4 import com.atlassian.fugue.Pair;
5 import com.atlassian.marketplace.client.api.ApplicationKey;
6 import com.atlassian.marketplace.client.api.HostingType;
7
8 import com.google.common.base.Predicate;
9
10 import static com.atlassian.fugue.Option.none;
11 import static com.atlassian.fugue.Option.some;
12 import static com.atlassian.fugue.Pair.pair;
13
14
15
16
17
18 public final class VersionCompatibility
19 {
20 ApplicationKey application;
21 CompatibilityHosting hosting;
22
23 VersionCompatibility(ApplicationKey application, CompatibilityHosting hosting)
24 {
25 this.application = application;
26 this.hosting = hosting;
27 }
28
29 static final class CompatibilityHosting
30 {
31 Option<CompatibilityHostingBounds> server;
32 Option<Boolean> cloud;
33
34 CompatibilityHosting(Option<CompatibilityHostingBounds> server, Option<Boolean> cloud)
35 {
36 this.server = server;
37 this.cloud = cloud;
38 }
39
40 boolean isServerCompatible()
41 {
42 return server.isDefined();
43 }
44
45 boolean isCloudCompatible()
46 {
47 return cloud.getOrElse(false);
48 }
49 }
50
51 static final class CompatibilityHostingBounds
52 {
53 VersionPoint min;
54 VersionPoint max;
55
56 CompatibilityHostingBounds(VersionPoint min, VersionPoint max)
57 {
58 this.min = min;
59 this.max = max;
60 }
61 }
62
63 static final class VersionPoint
64 {
65 int build;
66 Option<String> version;
67
68 VersionPoint(int build, Option<String> version)
69 {
70 this.build = build;
71 this.version = version;
72 }
73 }
74
75
76
77
78 public ApplicationKey getApplication()
79 {
80 return application;
81 }
82
83
84
85
86 public boolean isCloudCompatible()
87 {
88 return hosting.isCloudCompatible();
89 }
90
91
92
93
94 public boolean isServerCompatible()
95 {
96 return hosting.isServerCompatible();
97 }
98
99
100
101
102
103 public Option<Pair<Integer, Integer>> getServerBuildRange()
104 {
105 for (CompatibilityHostingBounds b: hosting.server)
106 {
107 return some(pair(b.min.build, b.max.build));
108 }
109 return none();
110 }
111
112 public boolean isInServerBuildRange(int build)
113 {
114 for (CompatibilityHostingBounds b: hosting.server)
115 {
116 return build >= b.min.build && build <= b.max.build;
117 }
118 return false;
119 }
120
121 public Option<Integer> getServerMinBuild()
122 {
123 for (CompatibilityHostingBounds b: hosting.server)
124 {
125 return some(b.min.build);
126 }
127 return none();
128 }
129
130 public Option<Integer> getServerMaxBuild()
131 {
132 for (CompatibilityHostingBounds b: hosting.server)
133 {
134 return some(b.max.build);
135 }
136 return none();
137 }
138
139 public boolean isCompatibleWith(Predicate<ApplicationKey> applicationCriteria,
140 HostingType hostingType, int build)
141 {
142 return applicationCriteria.apply(this.getApplication()) &&
143 ((hostingType == HostingType.CLOUD && hosting.isCloudCompatible()) ||
144 (hostingType != HostingType.CLOUD && hosting.isServerCompatible() &&
145 isInServerBuildRange(build)));
146 }
147
148 public static Predicate<VersionCompatibility> compatibleWith(final Predicate<ApplicationKey> applicationCriteria,
149 final HostingType hostingType, final int build)
150 {
151 return new Predicate<VersionCompatibility>()
152 {
153 @Override
154 public boolean apply(VersionCompatibility c)
155 {
156 return c.isCompatibleWith(applicationCriteria, hostingType, build);
157 }
158 };
159 }
160 }