View Javadoc

1   /*
2    * Copyright 2008-2010 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  
17  package de.smartics.maven.issues;
18  
19  import org.apache.maven.artifact.versioning.ArtifactVersion;
20  
21  /**
22   * Enumeration of version types. The version type identifies a specific part of
23   * a version number. The format is <code>major</code>.<code>minor</code>.
24   * <code>micro</code>.
25   *
26   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
27   * @version $Revision:591 $
28   */
29  public enum VersionType
30  {
31    // ****************************** Enumeration *******************************
32  
33    // ********************************* Fields *********************************
34  
35    // --- constants ------------------------------------------------------------
36  
37    /**
38     * The major version type. This is the first number.
39     */
40    MAJOR("major")
41    {
42      /**
43       * {@inheritDoc}
44       * <p>
45       * The major version must match to return the value <code>true</code>.
46       * </p>
47       *
48       * @see de.smartics.maven.issues.VersionType#isSameType(org.apache.maven.artifact.versioning.ArtifactVersion,
49       *      org.apache.maven.artifact.versioning.ArtifactVersion)
50       */
51      @Override
52      public boolean isSameType(final ArtifactVersion releaseVersion,
53          final ArtifactVersion currentVersion)
54      {
55        return releaseVersion.getMajorVersion() == currentVersion
56            .getMajorVersion();
57      }
58    },
59  
60    /**
61     * The minor version type. This is the number in the middle.
62     */
63    MINOR("minor")
64    {
65      /**
66       * {@inheritDoc}
67       * <p>
68       * The major and minor version must match to return the value
69       * <code>true</code>.
70       * </p>
71       *
72       * @see de.smartics.maven.issues.VersionType#isSameType(org.apache.maven.artifact.versioning.ArtifactVersion,
73       *      org.apache.maven.artifact.versioning.ArtifactVersion)
74       */
75      @Override
76      public boolean isSameType(final ArtifactVersion releaseVersion,
77          final ArtifactVersion currentVersion)
78      {
79        return releaseVersion.getMajorVersion() == currentVersion
80            .getMajorVersion()
81               && releaseVersion.getMinorVersion() == currentVersion
82                   .getMinorVersion();
83      }
84    },
85  
86    /**
87     * The micro version type. This it the last number.
88     */
89    MICRO("micro")
90    {
91      /**
92       * {@inheritDoc}
93       * <p>
94       * The major, minor and micro version must match to return the value
95       * <code>true</code>.
96       * </p>
97       *
98       * @see de.smartics.maven.issues.VersionType#isSameType(org.apache.maven.artifact.versioning.ArtifactVersion,
99       *      org.apache.maven.artifact.versioning.ArtifactVersion)
100      */
101     @Override
102     public boolean isSameType(final ArtifactVersion releaseVersion,
103         final ArtifactVersion currentVersion)
104     {
105       return releaseVersion.getMajorVersion() == currentVersion
106           .getMajorVersion()
107              && releaseVersion.getMinorVersion() == currentVersion
108                  .getMinorVersion()
109              && releaseVersion.getIncrementalVersion() == currentVersion
110                  .getIncrementalVersion();
111     }
112   },
113 
114   /**
115    * The all version type. This allows all versions.
116    */
117   ALL("all")
118   {
119     /**
120      * {@inheritDoc}
121      * <p>
122      * The major, minor and micro version must match to return the value
123      * <code>true</code>.
124      * </p>
125      *
126      * @see de.smartics.maven.issues.VersionType#isSameType(org.apache.maven.artifact.versioning.ArtifactVersion,
127      *      org.apache.maven.artifact.versioning.ArtifactVersion)
128      */
129     @Override
130     public boolean isSameType(final ArtifactVersion releaseVersion,
131         final ArtifactVersion currentVersion)
132     {
133       return true;
134     }
135   };
136 
137   // --- members --------------------------------------------------------------
138 
139   /**
140    * The display name of the version.
141    */
142   private final String name;
143 
144   // ****************************** Constructors ******************************
145 
146   /**
147    * Default constructor.
148    *
149    * @param name the display name of the version.
150    */
151   private VersionType(final String name)
152   {
153     this.name = name;
154   }
155 
156   // ********************************* Methods ********************************
157 
158   // --- init -----------------------------------------------------------------
159 
160   // --- get&set --------------------------------------------------------------
161 
162   /**
163    * Returns the display name of the version.
164    *
165    * @return the display name of the version.
166    */
167   public String getName()
168   {
169     return name;
170   }
171 
172   // --- business -------------------------------------------------------------
173 
174   /**
175    * Returns the version type identified by the given name.
176    *
177    * @param name the name of the version type requested.
178    * @return the version type with the given name.
179    * @throws IllegalArgumentException if there is no version type with the given
180    *           name.
181    * @throws NullPointerException if <code>name</code> is <code>null</code>.
182    */
183   public static VersionType fromName(final String name)
184     throws IllegalArgumentException, NullPointerException
185   {
186     for (VersionType type : VersionType.values())
187     {
188       if (name.equals(type.getName()))
189       {
190         return type;
191       }
192     }
193 
194     throw new IllegalArgumentException("Unknown name '" + name
195                                        + "' for a version type.");
196   }
197 
198   /**
199    * Checks if the release version and the current version are of the same type.
200    *
201    * @param releaseVersion the version to compare to.
202    * @param currentVersion the version to check if it is of the same type.
203    * @return <code>true</code> if the two versions are of the same type,
204    *         <code>false</code> otherwise.
205    */
206   public abstract boolean isSameType(ArtifactVersion releaseVersion,
207       ArtifactVersion currentVersion);
208 
209   // --- object basics --------------------------------------------------------
210 
211 }