1 package org.eclipse.aether.util.version;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.Set;
27
28 import org.eclipse.aether.version.Version;
29 import org.eclipse.aether.version.VersionRange;
30
31
32
33
34 final class UnionVersionRange
35 implements VersionRange
36 {
37
38 private final Set<VersionRange> ranges;
39
40 private final Bound lowerBound;
41
42 private final Bound upperBound;
43
44 public static VersionRange from( VersionRange... ranges )
45 {
46 if ( ranges == null )
47 {
48 return from( Collections.<VersionRange>emptySet() );
49 }
50 return from( Arrays.asList( ranges ) );
51 }
52
53 public static VersionRange from( Collection<? extends VersionRange> ranges )
54 {
55 if ( ranges != null && ranges.size() == 1 )
56 {
57 return ranges.iterator().next();
58 }
59 return new UnionVersionRange( ranges );
60 }
61
62 private UnionVersionRange( Collection<? extends VersionRange> ranges )
63 {
64 if ( ranges == null || ranges.isEmpty() )
65 {
66 this.ranges = Collections.emptySet();
67 lowerBound = null;
68 upperBound = null;
69 }
70 else
71 {
72 this.ranges = new HashSet<>( ranges );
73 Bound lowerBound = null, upperBound = null;
74 for ( VersionRange range : this.ranges )
75 {
76 Bound lb = range.getLowerBound();
77 if ( lb == null )
78 {
79 lowerBound = null;
80 break;
81 }
82 else if ( lowerBound == null )
83 {
84 lowerBound = lb;
85 }
86 else
87 {
88 int c = lb.getVersion().compareTo( lowerBound.getVersion() );
89 if ( c < 0 || ( c == 0 && !lowerBound.isInclusive() ) )
90 {
91 lowerBound = lb;
92 }
93 }
94 }
95 for ( VersionRange range : this.ranges )
96 {
97 Bound ub = range.getUpperBound();
98 if ( ub == null )
99 {
100 upperBound = null;
101 break;
102 }
103 else if ( upperBound == null )
104 {
105 upperBound = ub;
106 }
107 else
108 {
109 int c = ub.getVersion().compareTo( upperBound.getVersion() );
110 if ( c > 0 || ( c == 0 && !upperBound.isInclusive() ) )
111 {
112 upperBound = ub;
113 }
114 }
115 }
116 this.lowerBound = lowerBound;
117 this.upperBound = upperBound;
118 }
119 }
120
121 public boolean containsVersion( Version version )
122 {
123 for ( VersionRange range : ranges )
124 {
125 if ( range.containsVersion( version ) )
126 {
127 return true;
128 }
129 }
130 return false;
131 }
132
133 public Bound getLowerBound()
134 {
135 return lowerBound;
136 }
137
138 public Bound getUpperBound()
139 {
140 return upperBound;
141 }
142
143 @Override
144 public boolean equals( Object obj )
145 {
146 if ( obj == this )
147 {
148 return true;
149 }
150 else if ( obj == null || !getClass().equals( obj.getClass() ) )
151 {
152 return false;
153 }
154
155 UnionVersionRange that = (UnionVersionRange) obj;
156
157 return ranges.equals( that.ranges );
158 }
159
160 @SuppressWarnings( "checkstyle:magicnumber" )
161 @Override
162 public int hashCode()
163 {
164 return 97 * ranges.hashCode();
165 }
166
167 @Override
168 public String toString()
169 {
170 StringBuilder buffer = new StringBuilder( 128 );
171 for ( VersionRange range : ranges )
172 {
173 if ( buffer.length() > 0 )
174 {
175 buffer.append( ", " );
176 }
177 buffer.append( range );
178 }
179 return buffer.toString();
180 }
181
182 }