1 package org.apache.archiva.repository.content.base;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.repository.ManagedRepositoryContent;
23 import org.apache.archiva.repository.content.Project;
24 import org.apache.archiva.repository.content.Version;
25 import org.apache.archiva.repository.content.base.builder.VersionOptBuilder;
26 import org.apache.archiva.repository.content.base.builder.WithAssetBuilder;
27 import org.apache.archiva.repository.content.base.builder.WithNamespaceBuilder;
28 import org.apache.archiva.repository.content.base.builder.WithProjectBuilder;
29 import org.apache.archiva.repository.content.base.builder.WithVersionBuilder;
30 import org.apache.archiva.repository.storage.StorageAsset;
31 import org.apache.commons.lang3.StringUtils;
33 import java.util.Arrays;
34 import java.util.List;
35 import java.util.regex.PatternSyntaxException;
38 * Immutable version instance.
40 * You have to use the builder method to create instances of this version object.
42 * The project and the version string are required attributes of this instance additional to the base
43 * attributes repository and asset.
45 * Two instances are equal, if the project and the version match in addition to the base attributes repository and asset.
47 public class ArchivaVersion extends BaseContentItem implements Version
50 private String version;
51 private Project project;
52 private String separatorExpression = "\\.";
53 private List<String> versionSegments;
55 private ArchivaVersion( )
61 * Creates a new builder for creating new version instances. You have to provide the required
62 * attributes before the build() method can be called.
64 * @param storageAsset the storage asset
65 * @return the builder for creating new version instances
67 public static WithProjectBuilder withAsset( StorageAsset storageAsset )
69 return new Builder( ).withAsset( storageAsset );
72 public static WithAssetBuilder<WithProjectBuilder> withRepository( ManagedRepositoryContent repository )
74 return new ArchivaVersion.Builder( ).withRepository( repository );
78 public List<String> getVersionSegments( )
80 return versionSegments;
84 public String getVersion( )
90 public Project getProject( )
97 public boolean equals( Object o )
99 if ( this == o ) return true;
100 if ( o == null || getClass( ) != o.getClass( ) ) return false;
102 ArchivaVersion that = (ArchivaVersion) o;
103 if (!repository.equals( that.repository )) return false;
104 if ( !version.equals( that.version ) ) return false;
105 return project.equals( that.project );
109 public int hashCode( )
111 int result = super.hashCode( );
112 result = 31 * result + version.hashCode( );
113 result = 31 * result + project.hashCode( );
118 public String toString( )
120 return "ArchivaVersion{ "+version+", project="+project.toString()+"}";
123 private static final class Builder extends ContentItemBuilder<ArchivaVersion, VersionOptBuilder, WithProjectBuilder>
124 implements WithProjectBuilder, WithVersionBuilder, VersionOptBuilder
129 super( new ArchivaVersion( ) );
133 protected WithProjectBuilder getNextBuilder( )
139 protected VersionOptBuilder getOptBuilder( )
145 public VersionOptBuilder withVersion( String version )
147 if ( StringUtils.isEmpty( version ) )
149 throw new IllegalArgumentException( "Version parameter must not be empty or null." );
151 item.version = version;
152 updateVersionSegments( );
157 private void updateVersionSegments( )
159 item.versionSegments = Arrays.asList( item.version.split( item.separatorExpression ) );
163 public WithVersionBuilder withProject( Project project )
165 if ( project == null )
167 throw new IllegalArgumentException( "Project may not be null" );
169 item.project = project;
170 super.setRepository( project.getRepository( ) );
175 public ArchivaVersion build( )
182 public VersionOptBuilder withSeparatorExpression( String expression )
184 if ( StringUtils.isEmpty( expression ) )
186 throw new IllegalArgumentException( "Separator expression may not be null or empty" );
188 this.item.separatorExpression = expression;
191 updateVersionSegments( );
193 catch ( PatternSyntaxException e )
195 throw new IllegalArgumentException( "Bad separator expression " + expression + ": " + e.getMessage( ), e );