org.commonjava.emb.version.autobox.AutoboxableVersion Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2011 Red Hat, Inc.
*
* Licensed 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.
*/
package org.commonjava.emb.version.autobox;
import static org.commonjava.emb.version.autobox.AutoboxableVersionScheme.MAX_REBUILD_NUMBER;
import static org.commonjava.emb.version.autobox.VersionPartSeparatorType.DASH;
import static org.commonjava.emb.version.autobox.VersionPartType.INT;
import static org.commonjava.emb.version.autobox.VersionPartType.REBUILD;
import org.sonatype.aether.version.InvalidVersionSpecificationException;
import org.sonatype.aether.version.Version;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public final class AutoboxableVersion
implements Version
{
private final VersionPart[] parts;
private final boolean isSnapshot;
private final List qualifiers;
private final String rebuildIndicator;
public AutoboxableVersion( final boolean isSnapshot, final String rebuildIndicator, final List qualifiers,
final VersionPart... parts )
{
this.isSnapshot = isSnapshot;
this.rebuildIndicator = rebuildIndicator;
this.qualifiers = qualifiers;
this.parts = parts;
}
@Override
public int compareTo( final Version o )
{
if ( this == o )
{
return 0;
}
final AutoboxableVersion ov;
if ( o instanceof AutoboxableVersion )
{
ov = (AutoboxableVersion) o;
}
else
{
try
{
ov = AutoboxingParser.parseVersion( o.toString(), rebuildIndicator, qualifiers );
}
catch ( final InvalidVersionSpecificationException e )
{
throw new IllegalArgumentException( "toString() method of " + o.getClass().getName()
+ " does not render the version string faithfully! Cannot make a comparison!" );
}
}
final VersionPart[] otherParts = ov.parts;
final int sharedLen = parts.length > otherParts.length ? otherParts.length : parts.length;
for ( int i = 0; i < sharedLen; i++ )
{
final int comp = parts[i].compareTo( otherParts[i] );
if ( comp != 0 )
{
return comp;
}
}
if ( isSnapshot && ov.isSnapshot )
{
if ( parts.length == otherParts.length )
{
return 0;
}
/*
* Looking at parts[len-1].getType() == LOCAL_SNAPSHOT would be a form of double-checking I think...and I'm
* not 100% sure we need it.
*
* isSnapshot being equal and parts.length being different, the longer parts.length should sort LOWER.
* Otherwise, parts.length should not be equal, or else we should never get this far without a result.
*/
return parts.length < otherParts.length ? 1 : -1;
}
if ( parts.length > otherParts.length )
{
if ( isSnapshot )
{
return -1;
}
else
{
for ( int i = otherParts.length; i < parts.length; i++ )
{
if ( VersionPartType.compare( INT, parts[i].getType() ) > 0 )
{
return -1;
}
}
}
return 1;
}
else if ( parts.length < otherParts.length )
{
if ( ov.isSnapshot )
{
return 1;
}
else
{
for ( int i = parts.length; i < otherParts.length; i++ )
{
if ( VersionPartType.compare( INT, otherParts[i].getType() ) > 0 )
{
return 1;
}
}
}
return -1;
}
return 0;
}
@Override
public String toString()
{
final StringBuilder builder = new StringBuilder();
for ( final VersionPart part : parts )
{
builder.append( part );
}
return builder.toString();
}
public VersionPart[] getParts()
{
// defensive copy, so this class remains immutable.
return Arrays.asList( parts ).toArray( new VersionPart[] {} );
}
public boolean isSnapshot()
{
return isSnapshot;
}
public AutoboxableVersion createAutoboxUpperBoundVersion()
{
return createRebuildVersion( MAX_REBUILD_NUMBER );
}
public AutoboxableVersion createRebuildVersion( final int rebuildNumber )
{
final String rebuild = Integer.toString( rebuildNumber );
return createRebuildVersion( rebuild );
}
public AutoboxableVersion createRebuildVersion( final String rebuildNumber )
{
final List newParts = new ArrayList();
if ( isRebuild() )
{
if ( parts[parts.length - 1].getRawPart().equals( rebuildNumber ) )
{
return this;
}
for ( int i = 0; i < parts.length - 2; i++ )
{
newParts.add( parts[i] );
}
}
else
{
newParts.addAll( Arrays.asList( parts ) );
}
newParts.add( new VersionPart( REBUILD, rebuildIndicator, DASH, '-', qualifiers ) );
newParts.add( new VersionPart( INT, rebuildNumber, DASH, '-', qualifiers ) );
return new AutoboxableVersion( isSnapshot, rebuildIndicator, qualifiers,
newParts.toArray( new VersionPart[] {} ) );
}
public boolean isRebuild()
{
return parts.length > 2 && parts[parts.length - 2].getType() == REBUILD;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy