This project has retired. For details please refer to its Attic page.
ArchivaArtifact xref
View Javadoc
1   package org.apache.archiva.model;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import org.apache.archiva.common.utils.VersionUtil;
23  import org.apache.commons.lang3.StringUtils;
24  
25  /**
26   * ArchivaArtifact - Mutable artifact object.
27   *
28   *
29   */
30  public class ArchivaArtifact
31  {
32      private ArchivaArtifactModel model;
33  
34      private ArchivaArtifactPlatformDetails platformDetails;
35  
36      private String baseVersion;
37  
38      private static final int PRIME = 31;
39  
40      public ArchivaArtifact( String groupId, String artifactId, String version,
41                              String classifier, String type, String repositoryId )
42      {
43          if ( empty( groupId ) )
44          {
45              throw new IllegalArgumentException( "Unable to create ArchivaArtifact with empty groupId ["
46                  + Keys.toKey( groupId, artifactId, version, classifier, type ) + "]" );
47          }
48  
49          if ( empty( artifactId ) )
50          {
51              throw new IllegalArgumentException( "Unable to create ArchivaArtifact with empty artifactId ["
52                  + Keys.toKey( groupId, artifactId, version, classifier, type ) + "]" );
53          }
54  
55          if ( empty( version ) )
56          {
57              throw new IllegalArgumentException( "Unable to create ArchivaArtifact with empty version ["
58                  + Keys.toKey( groupId, artifactId, version, classifier, type ) + "]" );
59          }
60  
61          if ( empty( type ) )
62          {
63              throw new IllegalArgumentException( "Unable to create ArchivaArtifact with empty type ["
64                  + Keys.toKey( groupId, artifactId, version, classifier, type ) + "]" );
65          }
66  
67          if ( empty( repositoryId ) )
68          {
69              throw new IllegalArgumentException( "Unable to create ArchivaArtifact with empty repositoryId ["
70                  + Keys.toKey( groupId, artifactId, version, classifier, type ) + "]" );
71          }
72  
73          model = new ArchivaArtifactModel();
74  
75          model.setGroupId( groupId );
76          model.setArtifactId( artifactId );
77          model.setVersion( version );
78          model.setClassifier( StringUtils.defaultString( classifier ) );
79          model.setType( type );
80          model.setSnapshot( VersionUtil.isSnapshot( version ) );
81          model.setRepositoryId(repositoryId);
82          
83          this.baseVersion = VersionUtil.getBaseVersion( version );
84      }
85  
86      public ArchivaArtifact( ArchivaArtifactModel artifactModel )
87      {
88          this.model = artifactModel;
89          model.setSnapshot( VersionUtil.isSnapshot( model.getVersion() ) );
90          this.baseVersion = VersionUtil.getBaseVersion( model.getVersion() );
91      }
92      
93      public ArchivaArtifact( ArtifactReference ref, String repositoryId )
94      {
95          this( ref.getGroupId(), ref.getArtifactId(), ref.getVersion(), ref.getClassifier(), ref.getType(), repositoryId );
96      }
97  
98      public ArchivaArtifactModel getModel()
99      {
100         return model;
101     }
102 
103     public String getGroupId()
104     {
105         return model.getGroupId();
106     }
107 
108     public String getArtifactId()
109     {
110         return model.getArtifactId();
111     }
112 
113     public String getVersion()
114     {
115         return model.getVersion();
116     }
117 
118     public String getBaseVersion()
119     {
120         return baseVersion;
121     }
122 
123     public boolean isSnapshot()
124     {
125         return model.isSnapshot();
126     }
127 
128     public String getClassifier()
129     {
130         return model.getClassifier();
131     }
132 
133     public String getType()
134     {
135         return model.getType();
136     }
137 
138     public boolean hasClassifier()
139     {
140         return StringUtils.isNotEmpty( model.getClassifier() );
141     }
142 
143     public String getRepositoryId()
144     {
145         return model.getRepositoryId();
146     }
147 
148     @Override
149     public int hashCode()
150     {
151 
152         int result = 1;
153         if ( model != null )
154         {
155             result = PRIME * result + ( ( model.getGroupId() == null ) ? 0 : model.getGroupId().hashCode() );
156             result = PRIME * result + ( ( model.getArtifactId() == null ) ? 0 : model.getArtifactId().hashCode() );
157             result = PRIME * result + ( ( model.getVersion() == null ) ? 0 : model.getVersion().hashCode() );
158             result = PRIME * result + ( ( model.getClassifier() == null ) ? 0 : model.getClassifier().hashCode() );
159             result = PRIME * result + ( ( model.getType() == null ) ? 0 : model.getType().hashCode() );
160         }
161         return result;
162     }
163 
164     @Override
165     public boolean equals( Object obj )
166     {
167         if ( this == obj )
168         {
169             return true;
170         }
171 
172         if ( obj == null )
173         {
174             return false;
175         }
176 
177         if ( getClass() != obj.getClass() )
178         {
179             return false;
180         }
181 
182         final ArchivaArtifact/../org/apache/archiva/model/ArchivaArtifact.html#ArchivaArtifact">ArchivaArtifact other = (ArchivaArtifact) obj;
183 
184         if ( model == null )
185         {
186             if ( other.model != null )
187             {
188                 return false;
189             }
190         }
191         if ( !model.equals( other.model ) )
192         {
193             return false;
194         }
195 
196         return true;
197     }
198 
199     @Override
200     public String toString()
201     {
202         StringBuilder sb = new StringBuilder();
203         if ( model.getGroupId() != null )
204         {
205             sb.append( model.getGroupId() );
206             sb.append( ":" );
207         }
208         appendArtifactTypeClassifierString( sb );
209         sb.append( ":" );
210         if ( model.getVersion() != null )
211         {
212             sb.append( model.getVersion() );
213         }
214 
215         return sb.toString();
216     }
217 
218     private void appendArtifactTypeClassifierString( StringBuilder sb )
219     {
220         sb.append( model.getArtifactId() );
221         sb.append( ":" );
222         sb.append( getType() );
223         if ( hasClassifier() )
224         {
225             sb.append( ":" );
226             sb.append( getClassifier() );
227         }
228     }
229 
230     private boolean empty( String value )
231     {
232         return ( value == null ) || ( value.trim().length() < 1 );
233     }
234 
235     public ArchivaArtifactPlatformDetails getPlatformDetails()
236     {
237         return platformDetails;
238     }
239 
240     public void setPlatformDetails( ArchivaArtifactPlatformDetails platformDetails )
241     {
242         this.platformDetails = platformDetails;
243     }
244 }