View Javadoc

1   /*
2    * Copyright 2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.issue.util;
17  
18  import org.apache.maven.artifact.versioning.ArtifactVersion;
19  import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
20  import org.apache.maven.project.MavenProject;
21  
22  /**
23   * Utility class to handle versions.
24   */
25  public final class VersionHelper
26  {
27    // ********************************* Fields *********************************
28  
29    // --- constants ------------------------------------------------------------
30  
31    /**
32     * Signals to increment the major version number, setting minor and micro to
33     * zero.
34     * <p>
35     * The value of this constant is {@value}.
36     * </p>
37     */
38    public static final String MAJOR = "MAJOR";
39  
40    /**
41     * Signals to increment the minor version number, setting micro to zero.
42     * <p>
43     * The value of this constant is {@value}.
44     * </p>
45     */
46    public static final String MINOR = "MINOR";
47  
48    /**
49     * Signals to increment the micro version number.
50     * <p>
51     * The value of this constant is {@value}.
52     * </p>
53     */
54    public static final String MICRO = "MICRO";
55  
56    /**
57     * Signals to use the current version (without SNAPSHOT).
58     * <p>
59     * The value of this constant is {@value}.
60     * </p>
61     */
62    public static final String CURRENT = "CURRENT";
63  
64    /**
65     * Signals to use the current version (inclusive if present SNAPSHOT).
66     * <p>
67     * The value of this constant is {@value}.
68     * </p>
69     */
70    public static final String SNAPSHOT = "SNAPSHOT";
71  
72    // --- members --------------------------------------------------------------
73  
74    /**
75     * The Mave project.
76     */
77    private final MavenProject project;
78  
79    // ****************************** Initializer *******************************
80  
81    // ****************************** Constructors ******************************
82  
83    /**
84     * Default constructor.
85     *
86     * @param project the Mave project.
87     */
88    public VersionHelper(final MavenProject project)
89    {
90      this.project = project;
91    }
92  
93    // ****************************** Inner Classes *****************************
94  
95    // ********************************* Methods ********************************
96  
97    // --- init -----------------------------------------------------------------
98  
99    // --- get&set --------------------------------------------------------------
100 
101   // --- business -------------------------------------------------------------
102 
103   /**
104    * Calculates the next version.
105    *
106    * @param version the version to analyze.
107    * @return the version if it is neither {@link #MAJOR}, {@link #MINOR}, or
108    *         {@link #MICRO}.
109    */
110   public String calculateNextVersion(final String version)
111   {
112     final String nextVersion;
113     if (MAJOR.equals(version))
114     {
115       nextVersion = calcMajor();
116     }
117     else if (MINOR.equals(version))
118     {
119       nextVersion = calcMinor();
120     }
121     else if (MICRO.equals(version))
122     {
123       nextVersion = calcMicro();
124     }
125     else if (CURRENT.equals(version))
126     {
127       nextVersion = getVersionStrippedFromSnapshotSuffix(project.getVersion());
128     }
129     else if (SNAPSHOT.equals(version))
130     {
131       nextVersion = project.getVersion();
132     }
133     else
134     {
135       nextVersion = version;
136     }
137 
138     return nextVersion;
139   }
140 
141   /**
142    * Returns the given version without the <code>-SNAPSHOT</code> suffix.
143    *
144    * @param version the version to strip.
145    * @return the stripped version or (if no suffix was present) the
146    *         {@code version}.
147    */
148   public static String getVersionStrippedFromSnapshotSuffix(final String version)
149   {
150     if (version.endsWith("-SNAPSHOT"))
151     {
152       return version.substring(0, version.length() - 9);
153     }
154     return version;
155   }
156 
157   private String calcMajor()
158   {
159     final String nextVersion;
160     final ArtifactVersion artifactVersion = createArtifactVersion();
161     if (artifactVersion == null)
162     {
163       return null;
164     }
165     final int next = artifactVersion.getMajorVersion() + 1;
166     nextVersion = next + ".0.0";
167     return nextVersion;
168   }
169 
170   private String calcMinor()
171   {
172     final String nextVersion;
173     final ArtifactVersion artifactVersion = createArtifactVersion();
174     if (artifactVersion == null)
175     {
176       return null;
177     }
178     final int next = artifactVersion.getMinorVersion() + 1;
179     nextVersion =
180         String.valueOf(artifactVersion.getMajorVersion()) + '.' + next + ".0";
181     return nextVersion;
182   }
183 
184   private String calcMicro()
185   {
186     final String nextVersion;
187     final ArtifactVersion artifactVersion = createArtifactVersion();
188     if (artifactVersion == null)
189     {
190       return null;
191     }
192     final int next = artifactVersion.getIncrementalVersion() + 1;
193     nextVersion =
194         String.valueOf(artifactVersion.getMajorVersion()) + '.'
195             + artifactVersion.getMinorVersion() + '.' + next;
196     return nextVersion;
197   }
198 
199   private ArtifactVersion createArtifactVersion()
200   {
201     final String version = project.getVersion();
202     final ArtifactVersion artifactVersion = new DefaultArtifactVersion(version);
203     if (notRecognized(artifactVersion))
204     {
205       return null;
206     }
207     return artifactVersion;
208   }
209 
210   private static boolean notRecognized(final ArtifactVersion artifactVersion)
211   {
212     return (artifactVersion.getMajorVersion() == 0
213             && artifactVersion.getMinorVersion() == 0 && artifactVersion
214         .getIncrementalVersion() == 0);
215   }
216 
217   // --- object basics --------------------------------------------------------
218 
219 }