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 = upperBound = null;
68 }
69 else
70 {
71 this.ranges = new HashSet<VersionRange>( ranges );
72 Bound lowerBound = null, upperBound = null;
73 for ( VersionRange range : this.ranges )
74 {
75 Bound lb = range.getLowerBound();
76 if ( lb == null )
77 {
78 lowerBound = null;
79 break;
80 }
81 else if ( lowerBound == null )
82 {
83 lowerBound = lb;
84 }
85 else
86 {
87 int c = lb.getVersion().compareTo( lowerBound.getVersion() );
88 if ( c < 0 || ( c == 0 && !lowerBound.isInclusive() ) )
89 {
90 lowerBound = lb;
91 }
92 }
93 }
94 for ( VersionRange range : this.ranges )
95 {
96 Bound ub = range.getUpperBound();
97 if ( ub == null )
98 {
99 upperBound = null;
100 break;
101 }
102 else if ( upperBound == null )
103 {
104 upperBound = ub;
105 }
106 else
107 {
108 int c = ub.getVersion().compareTo( upperBound.getVersion() );
109 if ( c > 0 || ( c == 0 && !upperBound.isInclusive() ) )
110 {
111 upperBound = ub;
112 }
113 }
114 }
115 this.lowerBound = lowerBound;
116 this.upperBound = upperBound;
117 }
118 }
119
120 public boolean containsVersion( Version version )
121 {
122 for ( VersionRange range : ranges )
123 {
124 if ( range.containsVersion( version ) )
125 {
126 return true;
127 }
128 }
129 return false;
130 }
131
132 public Bound getLowerBound()
133 {
134 return lowerBound;
135 }
136
137 public Bound getUpperBound()
138 {
139 return upperBound;
140 }
141
142 @Override
143 public boolean equals( Object obj )
144 {
145 if ( obj == this )
146 {
147 return true;
148 }
149 else if ( obj == null || !getClass().equals( obj.getClass() ) )
150 {
151 return false;
152 }
153
154 UnionVersionRange that = (UnionVersionRange) obj;
155
156 return ranges.equals( that.ranges );
157 }
158
159 @Override
160 public int hashCode()
161 {
162 int hash = 97 * ranges.hashCode();
163 return hash;
164 }
165
166 @Override
167 public String toString()
168 {
169 StringBuilder buffer = new StringBuilder( 128 );
170 for ( VersionRange range : ranges )
171 {
172 if ( buffer.length() > 0 )
173 {
174 buffer.append( ", " );
175 }
176 buffer.append( range );
177 }
178 return buffer.toString();
179 }
180
181 }