OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library pub_semver.src.version_constraint; | 5 library pub_semver.src.version_constraint; |
6 | 6 |
7 import 'patterns.dart'; | 7 import 'patterns.dart'; |
8 import 'version.dart'; | 8 import 'version.dart'; |
9 import 'version_range.dart'; | 9 import 'version_range.dart'; |
| 10 import 'version_union.dart'; |
10 | 11 |
11 /// A [VersionConstraint] is a predicate that can determine whether a given | 12 /// A [VersionConstraint] is a predicate that can determine whether a given |
12 /// version is valid or not. | 13 /// version is valid or not. |
13 /// | 14 /// |
14 /// For example, a ">= 2.0.0" constraint allows any version that is "2.0.0" or | 15 /// For example, a ">= 2.0.0" constraint allows any version that is "2.0.0" or |
15 /// greater. Version objects themselves implement this to match a specific | 16 /// greater. Version objects themselves implement this to match a specific |
16 /// version. | 17 /// version. |
17 abstract class VersionConstraint { | 18 abstract class VersionConstraint { |
18 /// A [VersionConstraint] that allows all versions. | 19 /// A [VersionConstraint] that allows all versions. |
19 static VersionConstraint any = new VersionRange(); | 20 static VersionConstraint any = new VersionRange(); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 /// all versions. | 162 /// all versions. |
162 factory VersionConstraint.intersection( | 163 factory VersionConstraint.intersection( |
163 Iterable<VersionConstraint> constraints) { | 164 Iterable<VersionConstraint> constraints) { |
164 var constraint = new VersionRange(); | 165 var constraint = new VersionRange(); |
165 for (var other in constraints) { | 166 for (var other in constraints) { |
166 constraint = constraint.intersect(other); | 167 constraint = constraint.intersect(other); |
167 } | 168 } |
168 return constraint; | 169 return constraint; |
169 } | 170 } |
170 | 171 |
| 172 /// Creates a new version constraint that is the union of [constraints]. |
| 173 /// |
| 174 /// It allows any versions that any of those constraints allows. If |
| 175 /// [constraints] is empty, this returns a constraint that allows no versions. |
| 176 factory VersionConstraint.unionOf( |
| 177 Iterable<VersionConstraint> constraints) => |
| 178 VersionUnion.create(constraints); |
| 179 |
171 /// Returns `true` if this constraint allows no versions. | 180 /// Returns `true` if this constraint allows no versions. |
172 bool get isEmpty; | 181 bool get isEmpty; |
173 | 182 |
174 /// Returns `true` if this constraint allows all versions. | 183 /// Returns `true` if this constraint allows all versions. |
175 bool get isAny; | 184 bool get isAny; |
176 | 185 |
177 /// Returns `true` if this constraint allows [version]. | 186 /// Returns `true` if this constraint allows [version]. |
178 bool allows(Version version); | 187 bool allows(Version version); |
179 | 188 |
| 189 /// Returns `true` if this constraint allows all the versions that [other] |
| 190 /// allows. |
| 191 bool allowsAll(VersionConstraint other); |
| 192 |
| 193 /// Returns `true` if this constraint allows any of the versions that [other] |
| 194 /// allows. |
| 195 bool allowsAny(VersionConstraint other); |
| 196 |
180 /// Creates a new [VersionConstraint] that only allows [Version]s allowed by | 197 /// Creates a new [VersionConstraint] that only allows [Version]s allowed by |
181 /// both this and [other]. | 198 /// both this and [other]. |
182 VersionConstraint intersect(VersionConstraint other); | 199 VersionConstraint intersect(VersionConstraint other); |
| 200 |
| 201 /// Creates a new [VersionConstraint] that allows [Versions]s allowed by |
| 202 /// either this or [other]. |
| 203 VersionConstraint union(VersionConstraint other); |
183 } | 204 } |
184 | 205 |
185 class _EmptyVersion implements VersionConstraint { | 206 class _EmptyVersion implements VersionConstraint { |
186 const _EmptyVersion(); | 207 const _EmptyVersion(); |
187 | 208 |
188 bool get isEmpty => true; | 209 bool get isEmpty => true; |
189 bool get isAny => false; | 210 bool get isAny => false; |
190 bool allows(Version other) => false; | 211 bool allows(Version other) => false; |
| 212 bool allowsAll(Version other) => other.isEmpty; |
| 213 bool allowsAny(Version other) => false; |
191 VersionConstraint intersect(VersionConstraint other) => this; | 214 VersionConstraint intersect(VersionConstraint other) => this; |
| 215 VersionConstraint union(VersionConstraint other) => other; |
192 String toString() => '<empty>'; | 216 String toString() => '<empty>'; |
193 } | 217 } |
194 | 218 |
195 class _CompatibleWithVersionRange extends VersionRange { | 219 class _CompatibleWithVersionRange extends VersionRange { |
196 _CompatibleWithVersionRange(Version version) : super( | 220 _CompatibleWithVersionRange(Version version) : super( |
197 min: version, includeMin: true, | 221 min: version, includeMin: true, |
198 max: version.nextBreaking, includeMax: false); | 222 max: version.nextBreaking, includeMax: false); |
199 | 223 |
200 String toString() => '^$min'; | 224 String toString() => '^$min'; |
201 } | 225 } |
OLD | NEW |