refactoring: extracted MavenSnapshotMetadata from

(Local+Remote)SnapshotMetadata
This commit is contained in:
Hervé Boutemy 2013-01-19 13:36:20 +01:00
parent 37d3166d00
commit ff370850cd
4 changed files with 118 additions and 133 deletions

View File

@ -21,7 +21,6 @@ package org.apache.maven.repository.internal;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -35,66 +34,38 @@ import org.sonatype.aether.artifact.Artifact;
* @author Benjamin Bentmann * @author Benjamin Bentmann
*/ */
final class LocalSnapshotMetadata final class LocalSnapshotMetadata
extends MavenMetadata extends MavenSnapshotMetadata
{ {
private final Collection<Artifact> artifacts = new ArrayList<Artifact>();
private final boolean legacyFormat;
public LocalSnapshotMetadata( Artifact artifact, boolean legacyFormat ) public LocalSnapshotMetadata( Artifact artifact, boolean legacyFormat )
{ {
super( createMetadata( artifact, legacyFormat ), null ); super( createLocalMetadata( artifact, legacyFormat ), null, legacyFormat );
this.legacyFormat = legacyFormat;
} }
public LocalSnapshotMetadata( Metadata metadata, File file, boolean legacyFormat ) public LocalSnapshotMetadata( Metadata metadata, File file, boolean legacyFormat )
{ {
super( metadata, file ); super( metadata, file, legacyFormat );
this.legacyFormat = legacyFormat;
} }
private static Metadata createMetadata( Artifact artifact, boolean legacyFormat ) private static Metadata createLocalMetadata( Artifact artifact, boolean legacyFormat )
{ {
Metadata metadata = createRepositoryMetadata( artifact, legacyFormat );
Snapshot snapshot = new Snapshot(); Snapshot snapshot = new Snapshot();
snapshot.setLocalCopy( true ); snapshot.setLocalCopy( true );
Versioning versioning = new Versioning(); Versioning versioning = new Versioning();
versioning.setSnapshot( snapshot ); versioning.setSnapshot( snapshot );
Metadata metadata = new Metadata();
metadata.setVersioning( versioning ); metadata.setVersioning( versioning );
metadata.setGroupId( artifact.getGroupId() );
metadata.setArtifactId( artifact.getArtifactId() );
metadata.setVersion( artifact.getBaseVersion() );
if ( !legacyFormat )
{
metadata.setModelVersion( "1.1.0" );
}
return metadata; return metadata;
} }
public void bind( Artifact artifact )
{
artifacts.add( artifact );
}
public MavenMetadata setFile( File file ) public MavenMetadata setFile( File file )
{ {
return new LocalSnapshotMetadata( metadata, file, legacyFormat ); return new LocalSnapshotMetadata( metadata, file, legacyFormat );
} }
public Object getKey()
{
return getGroupId() + ':' + getArtifactId() + ':' + getVersion();
}
public static Object getKey( Artifact artifact )
{
return artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion();
}
@Override @Override
protected void merge( Metadata recessive ) protected void merge( Metadata recessive )
{ {
@ -135,29 +106,4 @@ final class LocalSnapshotMetadata
artifacts.clear(); artifacts.clear();
} }
private String getKey( String classifier, String extension )
{
return classifier + ':' + extension;
}
public String getGroupId()
{
return metadata.getGroupId();
}
public String getArtifactId()
{
return metadata.getArtifactId();
}
public String getVersion()
{
return metadata.getVersion();
}
public Nature getNature()
{
return Nature.SNAPSHOT;
}
} }

View File

@ -0,0 +1,100 @@
package org.apache.maven.repository.internal;
/*
* 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.File;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.maven.artifact.repository.metadata.Metadata;
import org.sonatype.aether.artifact.Artifact;
/**
* @author Hervé Boutemy
*/
abstract class MavenSnapshotMetadata
extends MavenMetadata
{
static final String SNAPSHOT = "SNAPSHOT";
protected final Collection<Artifact> artifacts = new ArrayList<Artifact>();
protected final boolean legacyFormat;
protected MavenSnapshotMetadata( Metadata metadata, File file, boolean legacyFormat )
{
super( metadata, file );
this.legacyFormat = legacyFormat;
}
protected static Metadata createRepositoryMetadata( Artifact artifact, boolean legacyFormat )
{
Metadata metadata = new Metadata();
if ( !legacyFormat )
{
metadata.setModelVersion( "1.1.0" );
}
metadata.setGroupId( artifact.getGroupId() );
metadata.setArtifactId( artifact.getArtifactId() );
metadata.setVersion( artifact.getBaseVersion() );
return metadata;
}
public void bind( Artifact artifact )
{
artifacts.add( artifact );
}
public Object getKey()
{
return getGroupId() + ':' + getArtifactId() + ':' + getVersion();
}
public static Object getKey( Artifact artifact )
{
return artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion();
}
protected String getKey( String classifier, String extension )
{
return classifier + ':' + extension;
}
public String getGroupId()
{
return metadata.getGroupId();
}
public String getArtifactId()
{
return metadata.getArtifactId();
}
public String getVersion()
{
return metadata.getVersion();
}
public Nature getNature()
{
return Nature.SNAPSHOT;
}
}

View File

@ -23,7 +23,6 @@ import java.io.File;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
@ -39,46 +38,19 @@ import org.sonatype.aether.artifact.Artifact;
* @author Benjamin Bentmann * @author Benjamin Bentmann
*/ */
final class RemoteSnapshotMetadata final class RemoteSnapshotMetadata
extends MavenMetadata extends MavenSnapshotMetadata
{ {
private static final String SNAPSHOT = "SNAPSHOT";
private final Collection<Artifact> artifacts = new ArrayList<Artifact>();
private final Map<String, SnapshotVersion> versions = new LinkedHashMap<String, SnapshotVersion>(); private final Map<String, SnapshotVersion> versions = new LinkedHashMap<String, SnapshotVersion>();
private final boolean legacyFormat;
public RemoteSnapshotMetadata( Artifact artifact, boolean legacyFormat ) public RemoteSnapshotMetadata( Artifact artifact, boolean legacyFormat )
{ {
super( createMetadata( artifact, legacyFormat ), null ); super( createRepositoryMetadata( artifact, legacyFormat ), null, legacyFormat );
this.legacyFormat = legacyFormat;
} }
private RemoteSnapshotMetadata( Metadata metadata, File file, boolean legacyFormat ) private RemoteSnapshotMetadata( Metadata metadata, File file, boolean legacyFormat )
{ {
super( metadata, file ); super( metadata, file, legacyFormat );
this.legacyFormat = legacyFormat;
}
private static Metadata createMetadata( Artifact artifact, boolean legacyFormat )
{
Metadata metadata = new Metadata();
if ( !legacyFormat )
{
metadata.setModelVersion( "1.1.0" );
}
metadata.setGroupId( artifact.getGroupId() );
metadata.setArtifactId( artifact.getArtifactId() );
metadata.setVersion( artifact.getBaseVersion() );
return metadata;
}
public void bind( Artifact artifact )
{
artifacts.add( artifact );
} }
public MavenMetadata setFile( File file ) public MavenMetadata setFile( File file )
@ -86,16 +58,6 @@ final class RemoteSnapshotMetadata
return new RemoteSnapshotMetadata( metadata, file, legacyFormat ); return new RemoteSnapshotMetadata( metadata, file, legacyFormat );
} }
public Object getKey()
{
return getGroupId() + ':' + getArtifactId() + ':' + getVersion();
}
public static Object getKey( Artifact artifact )
{
return artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion();
}
public String getExpandedVersion( Artifact artifact ) public String getExpandedVersion( Artifact artifact )
{ {
String key = getKey( artifact.getClassifier(), artifact.getExtension() ); String key = getKey( artifact.getClassifier(), artifact.getExtension() );
@ -136,7 +98,7 @@ final class RemoteSnapshotMetadata
if ( version.endsWith( SNAPSHOT ) ) if ( version.endsWith( SNAPSHOT ) )
{ {
String qualifier = snapshot.getTimestamp() + "-" + snapshot.getBuildNumber(); String qualifier = snapshot.getTimestamp() + '-' + snapshot.getBuildNumber();
version = version.substring( 0, version.length() - SNAPSHOT.length() ) + qualifier; version = version.substring( 0, version.length() - SNAPSHOT.length() ) + qualifier;
} }
@ -145,6 +107,7 @@ final class RemoteSnapshotMetadata
sv.setExtension( artifact.getExtension() ); sv.setExtension( artifact.getExtension() );
sv.setVersion( version ); sv.setVersion( version );
sv.setUpdated( lastUpdated ); sv.setUpdated( lastUpdated );
versions.put( getKey( sv.getClassifier(), sv.getExtension() ), sv ); versions.put( getKey( sv.getClassifier(), sv.getExtension() ), sv );
} }
@ -169,11 +132,6 @@ final class RemoteSnapshotMetadata
} }
} }
private String getKey( String classifier, String extension )
{
return classifier + ':' + extension;
}
private static int getBuildNumber( Metadata metadata ) private static int getBuildNumber( Metadata metadata )
{ {
int number = 0; int number = 0;
@ -191,24 +149,4 @@ final class RemoteSnapshotMetadata
return number; return number;
} }
public String getGroupId()
{
return metadata.getGroupId();
}
public String getArtifactId()
{
return metadata.getArtifactId();
}
public String getVersion()
{
return metadata.getVersion();
}
public Nature getNature()
{
return Nature.SNAPSHOT;
}
} }

View File

@ -40,18 +40,22 @@ final class VersionsMetadata
public VersionsMetadata( Artifact artifact ) public VersionsMetadata( Artifact artifact )
{ {
super( createMetadata( artifact ), null ); super( createRepositoryMetadata( artifact ), null );
this.artifact = artifact; this.artifact = artifact;
} }
public VersionsMetadata( Artifact artifact, File file ) public VersionsMetadata( Artifact artifact, File file )
{ {
super( createMetadata( artifact ), file ); super( createRepositoryMetadata( artifact ), file );
this.artifact = artifact; this.artifact = artifact;
} }
private static Metadata createMetadata( Artifact artifact ) private static Metadata createRepositoryMetadata( Artifact artifact )
{ {
Metadata metadata = new Metadata();
metadata.setGroupId( artifact.getGroupId() );
metadata.setArtifactId( artifact.getArtifactId() );
Versioning versioning = new Versioning(); Versioning versioning = new Versioning();
versioning.addVersion( artifact.getBaseVersion() ); versioning.addVersion( artifact.getBaseVersion() );
if ( !artifact.isSnapshot() ) if ( !artifact.isSnapshot() )
@ -63,10 +67,7 @@ final class VersionsMetadata
versioning.setLatest( artifact.getBaseVersion() ); versioning.setLatest( artifact.getBaseVersion() );
} }
Metadata metadata = new Metadata();
metadata.setVersioning( versioning ); metadata.setVersioning( versioning );
metadata.setGroupId( artifact.getGroupId() );
metadata.setArtifactId( artifact.getArtifactId() );
return metadata; return metadata;
} }