View Javadoc
1   package org.codehaus.plexus.languages.java.version;
2   
3   import java.util.regex.Matcher;
4   import java.util.regex.Pattern;
5   
6   /*
7    * Licensed to the Apache Software Foundation (ASF) under one
8    * or more contributor license agreements.  See the NOTICE file
9    * distributed with this work for additional information
10   * regarding copyright ownership.  The ASF licenses this file
11   * to you under the Apache License, Version 2.0 (the
12   * "License"); you may not use this file except in compliance
13   * with the License.  You may obtain a copy of the License at
14   *
15   *   http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing,
18   * software distributed under the License is distributed on an
19   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20   * KIND, either express or implied.  See the License for the
21   * specific language governing permissions and limitations
22   * under the License.
23   */
24  
25  /**
26   * @author Robert Scholte
27   * @since 1.0.0
28   * 
29   * @see <a href="http://www.oracle.com/technetwork/java/javase/namechange-140185.html">http://www.oracle.com/technetwork/java/javase/namechange-140185.html</a>
30   * @see <a href="http://openjdk.java.net/jeps/223">http://openjdk.java.net/jeps/223</a>
31   */
32  public class JavaVersion implements Comparable<JavaVersion>
33  {
34      private static final Pattern startingDigits = Pattern.compile( "(\\d+)(.*)" );
35      
36      private String rawVersion;
37  
38      private JavaVersion( String rawVersion )
39      {
40          this.rawVersion = rawVersion;
41      }
42  
43      /**
44       * Parser only the version-scheme.
45       * 
46       * @param s the version string
47       * @return the version wrapped in a JavadocVersion
48       */
49      public static JavaVersion parse( String s ) 
50      {
51          return new JavaVersion( s );
52      }
53  
54      @Override
55      public int compareTo( JavaVersion other )
56      {
57          String[] thisSegments = this.rawVersion.split( "\\." );
58          String[] otherSegments = other.rawVersion.split( "\\." );
59          
60          int minSegments = Math.min( thisSegments.length, otherSegments.length );
61          
62          for ( int index = 0; index < minSegments; index++ )
63          {
64              Matcher thisMatcher = startingDigits.matcher( thisSegments[index] );
65              
66              int thisValue;
67              
68              if( thisMatcher.find() )
69              {
70                  thisValue = Integer.parseInt( thisMatcher.group( 1 ) );
71              }
72              else
73              {
74                  thisValue = -1;
75              }
76              
77              Matcher otherMatcher = startingDigits.matcher( otherSegments[index] );
78              
79              int otherValue;
80              
81              if( otherMatcher.find() )
82              {
83                  otherValue = Integer.parseInt( otherMatcher.group( 1 ) );
84              }
85              else
86              {
87                  otherValue = -1;
88              }
89              
90              int compareValue = Integer.compare( thisValue, otherValue );
91              
92              if ( compareValue != 0 )
93              {
94                  return compareValue;
95              }
96  
97              compareValue = suffixRate( thisMatcher.group( 2 ) ) - suffixRate( otherMatcher.group( 2 ) );
98              if ( compareValue != 0 )
99              {
100                 return compareValue;
101             }
102             
103             // works for now, but needs improvement
104             compareValue = thisMatcher.group( 2 ).compareTo( otherMatcher.group( 2 ) );
105             
106             if ( compareValue != 0 )
107             {
108                 return compareValue;
109             }
110         }
111         
112         return ( thisSegments.length - otherSegments.length );
113     }
114     
115     private int suffixRate( String suffix ) {
116         if ( "-ea".equals( suffix ) )
117         {
118             return -100;
119         }
120         else if ( "".equals( suffix ) )
121         {
122             return 0;
123         }
124         else 
125         {
126             return 10;
127         }
128     }
129 
130     @Override
131     public String toString()
132     {
133         return rawVersion;
134     }
135 }