-
Notifications
You must be signed in to change notification settings - Fork 265
/
UpdateScope.java
524 lines (478 loc) · 21.3 KB
/
UpdateScope.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package org.codehaus.mojo.versions.api;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.codehaus.mojo.versions.ordering.VersionComparator;
/**
* Scopes of version updates.
*
* @author Stephen Connolly
* todo: convert this class to a Java 1.5 enum once we move to Java 1.5
* @since 1.0-beta-1
*/
public abstract class UpdateScope
implements Comparable, Serializable
{
/**
* Versions which are less than an incremental update.
*
* @since 1.0-beta-1
*/
public static final UpdateScope SUBINCREMENTAL = new UpdateScope( "SUBINCREMENTAL", 0 )
{
/** {@inheritDoc} */
public ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
VersionComparator versionComparator = versionDetails.getVersionComparator();
return versionComparator.getSegmentCount( currentVersion ) < 3 ? null
: versionDetails.getOldestVersion( currentVersion,
versionComparator.incrementSegment( currentVersion, 2 ),
includeSnapshots, false, false );
}
/** {@inheritDoc} */
public ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
VersionComparator versionComparator = versionDetails.getVersionComparator();
return versionComparator.getSegmentCount( currentVersion ) < 3 ? null
: versionDetails.getNewestVersion( currentVersion,
versionComparator.incrementSegment( currentVersion, 2 ),
includeSnapshots, false, false );
}
/** {@inheritDoc} */
public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
VersionComparator versionComparator = versionDetails.getVersionComparator();
return versionComparator.getSegmentCount( currentVersion ) < 3 ? null
: versionDetails.getVersions( currentVersion,
versionComparator.incrementSegment( currentVersion, 2 ),
includeSnapshots, false, false );
}
};
/**
* Incremental version updates, that is the third segment of the version number, for example <code>1.0.0.15</code>
* to <code>1.0.1.0</code>.
*
* @since 1.0-beta-1
*/
public static final UpdateScope INCREMENTAL = new UpdateScope( "INCREMENTAL", 1 )
{
/** {@inheritDoc} */
public ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
VersionComparator versionComparator = versionDetails.getVersionComparator();
return versionComparator.getSegmentCount( currentVersion ) < 3 ? null
: versionDetails.getOldestVersion( versionComparator.incrementSegment( currentVersion, 2 ),
versionComparator.incrementSegment( currentVersion, 1 ),
includeSnapshots, true, false );
}
/** {@inheritDoc} */
public ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
VersionComparator versionComparator = versionDetails.getVersionComparator();
return versionComparator.getSegmentCount( currentVersion ) < 3 ? null
: versionDetails.getNewestVersion( versionComparator.incrementSegment( currentVersion, 2 ),
versionComparator.incrementSegment( currentVersion, 1 ),
includeSnapshots, true, false );
}
/** {@inheritDoc} */
public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
VersionComparator versionComparator = versionDetails.getVersionComparator();
return versionComparator.getSegmentCount( currentVersion ) < 3 ? null
: versionDetails.getVersions( versionComparator.incrementSegment( currentVersion, 2 ),
versionComparator.incrementSegment( currentVersion, 1 ),
includeSnapshots, true, false );
}
};
/**
* Minor version updates, that is the second segment of the version number, for example <code>1.0.0.15</code> to
* <code>1.1.0.0</code>.
*
* @since 1.0-beta-1
*/
public static final UpdateScope MINOR = new UpdateScope( "MINOR", 2 )
{
/** {@inheritDoc} */
public ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
VersionComparator versionComparator = versionDetails.getVersionComparator();
return versionComparator.getSegmentCount( currentVersion ) < 2 ? null
: versionDetails.getOldestVersion( versionComparator.incrementSegment( currentVersion, 1 ),
versionComparator.incrementSegment( currentVersion, 0 ),
includeSnapshots, true, false );
}
/** {@inheritDoc} */
public ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
VersionComparator versionComparator = versionDetails.getVersionComparator();
return versionComparator.getSegmentCount( currentVersion ) < 2 ? null
: versionDetails.getNewestVersion( versionComparator.incrementSegment( currentVersion, 1 ),
versionComparator.incrementSegment( currentVersion, 0 ),
includeSnapshots, true, false );
}
/** {@inheritDoc} */
public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
VersionComparator versionComparator = versionDetails.getVersionComparator();
return versionComparator.getSegmentCount( currentVersion ) < 2 ? null
: versionDetails.getVersions( versionComparator.incrementSegment( currentVersion, 1 ),
versionComparator.incrementSegment( currentVersion, 0 ),
includeSnapshots, true, false );
}
};
/**
* Major version updates, that is the first segment of the version number, for example <code>1.0.0.15</code> to
* <code>2.0.0.0</code>.
*
* @since 1.0-beta-1
*/
public static final UpdateScope MAJOR = new UpdateScope( "MAJOR", 3 )
{
/** {@inheritDoc} */
public ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
VersionComparator versionComparator = versionDetails.getVersionComparator();
return versionComparator.getSegmentCount( currentVersion ) < 1 ? null
: versionDetails.getOldestVersion( versionComparator.incrementSegment( currentVersion, 0 ),
null, includeSnapshots, true, false );
}
/** {@inheritDoc} */
public ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
VersionComparator versionComparator = versionDetails.getVersionComparator();
return versionComparator.getSegmentCount( currentVersion ) < 1 ? null
: versionDetails.getNewestVersion( versionComparator.incrementSegment( currentVersion, 0 ),
null, includeSnapshots, true, false );
}
/** {@inheritDoc} */
public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
VersionComparator versionComparator = versionDetails.getVersionComparator();
return versionComparator.getSegmentCount( currentVersion ) < 1 ? null
: versionDetails.getVersions( versionComparator.incrementSegment( currentVersion, 0 ), null,
includeSnapshots, true, false );
}
};
/**
* Any version updates.
*
* @since 1.0-beta-1
*/
public static final UpdateScope ANY = new UpdateScope( "ANY", 4 )
{
/** {@inheritDoc} */
public ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
return versionDetails.getOldestVersion( currentVersion, null, includeSnapshots, false, false );
}
/** {@inheritDoc} */
public ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
return versionDetails.getNewestVersion( currentVersion, null, includeSnapshots, false, false );
}
/** {@inheritDoc} */
public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots )
{
return versionDetails.getVersions( currentVersion, null, includeSnapshots, false, false );
}
};
/**
* Returns the next version after the specified current version within this scope.
*
* @param versionDetails The versions to select from.
* @param currentVersion The current version.
* @param includeSnapshots Whether to include snapshots.
* @return The next version within this scope or <code>null</code> if there is no version within this scope.
*/
public abstract ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots );
/**
* Returns the newest version after the specified current version within this scope.
*
* @param versionDetails The versions to select from.
* @param currentVersion The current version.
* @param includeSnapshots Whether to include snapshots.
* @return The newest version within this scope or <code>null</code> if there is no version within this scope.
*/
public abstract ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots );
/**
* Returns all versions newer than the specified current version within this scope.
*
* @param versionDetails The versions to select from.
* @param currentVersion The current version.
* @param includeSnapshots Whether to include snapshots.
* @return All newer versions within this scope.
*/
public abstract ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactVersion currentVersion,
boolean includeSnapshots );
/**
* The name of this constant, as declared in the declaration. Most programmers should use the {@link #toString}
* method rather than accessing this field.
*/
private final String name;
/**
* Returns the name of this enum constant, exactly as declared in its enum declaration.
* <p>
* <b>Most programmers should use the {@link #toString} method in preference to this one, as the toString method may
* return a more user-friendly name.</b> This method is designed primarily for use in specialized situations where
* correctness depends on getting the exact name, which will not vary from release to release.
* </p>
*
* @return the name of this enum constant
*/
public final String name()
{
return name;
}
/**
* The ordinal of this enumeration constant (its position in the enum declaration, where the initial constant is
* assigned an ordinal of zero).
* <p>
* Most programmers will have no use for this field.
* </p>
*/
private final int ordinal;
/**
* Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial
* constant is assigned an ordinal of zero).
* <p>
* Most programmers will have no use for this method.
* </p>
*
* @return the ordinal of this enumeration constant
*/
public final int ordinal()
{
return ordinal;
}
/**
* Sole constructor. Programmers cannot invoke this constructor.
*
* @param name - The name of this enum constant, which is the identifier used to declare it.
* @param ordinal - The ordinal of this enumeration constant (its position in the enum declaration, where the
* initial constant is assigned an ordinal of zero).
*/
private UpdateScope( String name, int ordinal )
{
this.name = name;
this.ordinal = ordinal;
}
/**
* {@inheritDoc}
*/
public String toString()
{
return name;
}
/**
* {@inheritDoc}
*/
public boolean equals( Object o )
{
return this == o;
}
/**
* {@inheritDoc}
*/
public int hashCode()
{
return super.hashCode();
}
/**
* Throws CloneNotSupportedException. This guarantees that levels are never cloned, which is necessary to preserve
* their "singleton" status.
*
* @return (neverreturns)
*/
protected final Object clone()
throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
}
/**
* Compares this enum with the specified object for order. Returns a negative integer, zero, or a positive integer
* as this object is less than, equal to, or greater than the specified object.
* <p>
* Enum constants are only comparable to other enum constants of the same enum type. The natural order implemented
* by this method is the order in which the constants are declared.
* </p>
*/
public final int compareTo( Object o )
{
UpdateScope other = (UpdateScope) o;
UpdateScope self = this;
if ( self.getClass() != other.getClass() )
{
throw new ClassCastException();
}
return self.ordinal - other.ordinal;
}
/**
* Returns the Class object corresponding to this enum constant's enum type. Two enum constants e1 and e2 are of the
* same enum type if and only if e1.getDeclaringClass() == e2.getDeclaringClass(). (The value returned by this
* method may differ from the one returned by the {@link Object#getClass} method for enum constants with
* constant-specific class bodies.)
*
* @return the Class object corresponding to this enum constant's enum type
*/
public final Class getDeclaringClass()
{
return getClass();
}
/**
* Returns the enum constant of the specified enum type with the specified name. The name must match exactly an
* identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
*
* @param name the name of the constant to return
* @return the enum constant of the specified enum type with the specified name
* @throws IllegalArgumentException if the specified enum type has no constant with the specified name, or the
* specified class object does not represent an enum type
* @throws NullPointerException if <code>name</code> is null
*/
public static UpdateScope valueOf( String name )
{
UpdateScope result = (UpdateScope) levelConstants.get( name );
if ( result != null )
{
return result;
}
if ( name == null )
{
throw new NullPointerException( "Name is null" );
}
throw new IllegalArgumentException( "No enum const " + UpdateScope.class.getName() + "." + name );
}
public static UpdateScope[] values()
{
return new UpdateScope[] { SUBINCREMENTAL, INCREMENTAL, MINOR, MAJOR, ANY };
}
/**
* Classifies the type of update.
*
* @param comparator The version comparator to use for classifying.
* @param from The first version.
* @param to The second version.
* @return The update classification.
*/
public static UpdateScope classifyUpdate( VersionComparator comparator, ArtifactVersion from, ArtifactVersion to )
{
if ( comparator.compare( from, to ) >= 0 )
{
throw new IllegalArgumentException( "From version must be less than to version" );
}
// the trick here is that incrementing from twice and to once, should give the same version
int matchSegment = 0;
for ( int segment =
Math.min( comparator.getSegmentCount( from ), comparator.getSegmentCount( to ) ); segment > 0; segment-- )
{
ArtifactVersion f = comparator.incrementSegment( from, segment - 1 );
f = comparator.incrementSegment( f, segment - 1 );
ArtifactVersion t = comparator.incrementSegment( to, segment - 1 );
if ( f.toString().equals( t.toString() ) )
{
matchSegment = segment;
break;
}
}
switch ( matchSegment )
{
case 0:
return MAJOR;
case 1:
return MINOR;
case 2:
return INCREMENTAL;
default:
return SUBINCREMENTAL;
}
}
private static final Map levelConstants;
static
{
Map map = new HashMap( 5 );
map.put( SUBINCREMENTAL.name(), SUBINCREMENTAL );
map.put( INCREMENTAL.name(), INCREMENTAL );
map.put( MINOR.name(), MINOR );
map.put( MAJOR.name(), MAJOR );
map.put( ANY.name(), ANY );
levelConstants = map;
}
/**
* enum classes cannot have finalize methods.
*/
protected final void finalize()
throws Throwable
{
super.finalize();
}
/**
* We need to ensure that the singleton is used when deserializing.
*
* @return The correct singleton instance.
* @throws java.io.ObjectStreamException when things go wrong.
*/
private Object readResolve()
throws ObjectStreamException
{
if ( ordinal == SUBINCREMENTAL.ordinal )
{
return SUBINCREMENTAL;
}
if ( ordinal == INCREMENTAL.ordinal )
{
return INCREMENTAL;
}
if ( ordinal == MINOR.ordinal )
{
return MINOR;
}
if ( ordinal == MAJOR.ordinal )
{
return MAJOR;
}
if ( ordinal == ANY.ordinal )
{
return ANY;
}
throw new StreamCorruptedException();
}
}