View Javadoc
1   package org.codehaus.mojo.jaxb2.shared.version;
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.codehaus.mojo.jaxb2.shared.Validate;
23  
24  /**
25   * Trivial holder class for dependency information, as found within a dependencies.properties file.
26   *
27   * @author <a href="mailto:lj@jguru.se">Lennart J&ouml;relid</a>, jGuru Europe AB
28   * @since 2.0
29   */
30  public class DependencyInfo implements Comparable<DependencyInfo> {
31  
32      // Internal state
33      private static final String GROUP_ARTIFACT_SEPARATOR = "/";
34      private String groupId;
35      private String artifactId;
36      private String version;
37      private String scope = "compile";
38      private String type = "jar";
39  
40      public DependencyInfo(final String groupId, final String artifactId, final String version) {
41          this.groupId = groupId;
42          this.artifactId = artifactId;
43          this.version = version;
44      }
45  
46      /**
47       * @return The GroupId of this DependencyInfo.
48       */
49      public String getGroupId() {
50          return groupId;
51      }
52  
53      /**
54       * @return The ArtifactId of this DependencyInfo.
55       */
56      public String getArtifactId() {
57          return artifactId;
58      }
59  
60      /**
61       * @return The Maven version of this DependencyInfo.
62       */
63      public String getVersion() {
64          return version;
65      }
66  
67      /**
68       * @return The type of this DependencyInfo.
69       */
70      public String getType() {
71          return type;
72      }
73  
74      /**
75       * @return The scope of this DependencyInfo.
76       */
77      public String getScope() {
78          return scope;
79      }
80  
81      /**
82       * Assigns the type of this DependencyInfo.
83       *
84       * @param type The non-empty type of this DependencyInfo.
85       */
86      public void setType(final String type) {
87  
88          // Check sanity
89          Validate.notEmpty(type, "type");
90  
91          // Assign internal state
92          this.type = type;
93      }
94  
95      /**
96       * Assigns the scope of this DependencyInfo.
97       *
98       * @param scope The non-empty scope of this DependencyInfo.
99       */
100     public void setScope(final String scope) {
101 
102         // Check sanity
103         Validate.notEmpty(scope, "scope");
104 
105         // Assign internal state.
106         this.scope = scope;
107     }
108 
109     /**
110      * @return A key for use within a SortedSet where this DependencyInfo should be sorted.
111      */
112     public String getGroupArtifactKey() {
113         return getGroupId() + GROUP_ARTIFACT_SEPARATOR + getArtifactId();
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public int hashCode() {
121         return groupId.hashCode() + artifactId.hashCode() + version.hashCode() + type.hashCode();
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     @Override
128     public String toString() {
129         return groupId + GROUP_ARTIFACT_SEPARATOR
130                 + artifactId + GROUP_ARTIFACT_SEPARATOR
131                 + version + GROUP_ARTIFACT_SEPARATOR
132                 + scope + GROUP_ARTIFACT_SEPARATOR
133                 + type;
134     }
135 
136     /**
137      * {@inheritDoc}
138      */
139     @Override
140     public int compareTo(final DependencyInfo that) {
141 
142         // Return quick.
143         if (that == this) {
144             return 0;
145         }
146         if (that == null) {
147             return -1;
148         }
149 
150         // Compare internal stater
151         int toReturn = this.getGroupId().compareTo(that.getGroupId());
152         if (toReturn == 0) {
153             toReturn = this.getArtifactId().compareTo(that.getArtifactId());
154         }
155         if (toReturn == 0) {
156             toReturn = this.getVersion().compareTo(that.getVersion());
157         }
158         if (toReturn == 0) {
159             toReturn = this.getType().compareTo(that.getType());
160         }
161 
162         // All done.
163         return toReturn;
164     }
165 }