Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.lang.common.version

File VersionRange.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
30
10
1
90
76
15
0.5
3
10
1.5

Classes

Class Line # Actions
VersionRange 6 30 15
0.958333395.8%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    package io.sunshower.lang.common.version;
2   
3    import io.sunshower.lang.common.Strings;
4    import java.util.function.Predicate;
5   
 
6    public class VersionRange {
7   
 
8  6 toggle public static VersionRange parse(String s) {
9  6 if (Strings.isBlank(s)) {
10  0 throw new IllegalArgumentException("Version must not be null or empty!");
11    }
12  6 final String version = s.trim();
13  6 if (version.charAt(0) == '[' || version.charAt(0) == '(') {
14  5 return parseRange(version);
15    } else {
16  1 return parseFixed(version);
17    }
18    }
19   
20    private final Version lowerBound;
21    private final Version upperBound;
22   
 
23  1 toggle private VersionRange(Version version) {
24  1 this(version, version);
25    }
26   
 
27  2 toggle public boolean contains(Version v) {
28  2 return lowerBound.compareTo(v) >= 0 && v.compareTo(upperBound) <= 0;
29    }
30   
 
31  6 toggle private VersionRange(Version lowerBound, Version upperBound) {
32  6 this.lowerBound = lowerBound;
33  6 this.upperBound = upperBound;
34    }
35   
 
36  1 toggle private static VersionRange parseFixed(String version) {
37  1 return new VersionRange(new Version(true, version));
38    }
39   
 
40  5 toggle private static VersionRange parseRange(String version) {
41  5 final String lowerBound = consumeUntil(version, 1, VersionRange::isWhitespaceOrSeparator, true);
42  5 boolean closedUpperBound = version.charAt(version.length() - 1) == ']';
43  5 boolean closedLowerBound = version.charAt(0) == '[';
44   
45  5 return new VersionRange(
46    new Version(closedLowerBound, lowerBound),
47    new Version(
48    closedUpperBound,
49    consumeUntil(
50    version, version.length() - 2, VersionRange::isWhitespaceOrSeparator, false)));
51    }
52   
 
53  40 toggle private static boolean isWhitespaceOrSeparator(char ch) {
54  40 return Character.isWhitespace(ch)
55    || ch == ','
56    || ch == '['
57    || ch == ']'
58    || ch == '('
59    || ch == ')';
60    }
61   
 
62  10 toggle private static String consumeUntil(
63    String version, int beginIndex, Predicate<Character> p, boolean direction) {
64  10 final StringBuilder b = new StringBuilder();
65  10 int i = beginIndex;
66  10 char ch;
67  10 do {
68  40 ch = version.charAt(i);
69  40 if (!p.test(ch)) {
70  30 b.append(ch);
71    } else {
72  10 break;
73    }
74  30 if (direction) {
75  15 ++i;
76    } else {
77  15 --i;
78    }
79    } while (true);
80  10 return b.toString();
81    }
82   
 
83  4 toggle public Version getLowerBound() {
84  4 return lowerBound;
85    }
86   
 
87  4 toggle public Version getUpperBound() {
88  4 return upperBound;
89    }
90    }