View Javadoc
1   package org.apache.maven.plugins.javadoc;
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.plexus.util.StringUtils;
23  
24  /**
25   * Once the plugin requires Java9, this class can be replaced with java.lang.Runtime.Version
26   * <p>
27   * <strong>Note: </strong> Ensure the methods match, although parse+compareTo+toString should be enough.
28   * </p>
29   * 
30   * 
31   * @author Robert Scholte
32   * @since 3.0.0
33   */
34  public class JavadocVersion implements Comparable<JavadocVersion>
35  {
36      private String rawVersion;
37  
38      private JavadocVersion( String rawVersion )
39      {
40          if ( StringUtils.isEmpty( rawVersion ) )
41          {
42              throw new IllegalArgumentException( "The rawVersion could not be null." );
43          }
44          this.rawVersion = rawVersion;
45      }
46  
47      /**
48       * Parser only the version-scheme.
49       * 
50       * @param s the version string
51       * @return the version wrapped in a JavadocVersion
52       */
53      static JavadocVersion parse( String s ) 
54      {
55          return new JavadocVersion( s );
56      }
57  
58      @Override
59      public int compareTo( JavadocVersion other )
60      {
61          String[] thisSegments = this.rawVersion.split( "\\." );
62          String[] otherSegments = other.rawVersion.split( "\\." );
63          
64          int minSegments = Math.min( thisSegments.length, otherSegments.length );
65          
66          for ( int index = 0; index < minSegments; index++ )
67          {
68              int thisValue = Integer.parseInt( thisSegments[index] );
69              int otherValue = Integer.parseInt( otherSegments[index] );
70              
71              int compareValue = Integer.compare( thisValue, otherValue );
72              
73              if ( compareValue != 0 )
74              {
75                  return compareValue;
76              }
77          }
78          
79          return ( thisSegments.length - otherSegments.length );
80      }
81  
82      @Override
83      public String toString()
84      {
85          return rawVersion;
86      }
87  }