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      /**
35       * Represents the System property {@code java.specification.version}
36       */
37      public static final JavaVersion JAVA_SPECIFICATION_VERSION = parse( System.getProperty( "java.specification.version" ) );
38  
39      /**
40       * Represents the System property {@code java.version}
41       */
42      public static final JavaVersion JAVA_VERSION = parse( System.getProperty( "java.version" ) );
43      
44      private static final Pattern startingDigits = Pattern.compile( "(\\d+)(.*)" );
45      
46      private String rawVersion;
47  
48      private JavaVersion( String rawVersion )
49      {
50          this.rawVersion = rawVersion;
51      }
52  
53      /**
54       * Parser only the version-scheme.
55       * 
56       * @param s the version string
57       * @return the version wrapped in a JavadocVersion
58       */
59      public static JavaVersion parse( String s ) 
60      {
61          return new JavaVersion( s );
62      }
63  
64      @Override
65      public int compareTo( JavaVersion other )
66      {
67          String[] thisSegments = this.rawVersion.split( "\\." );
68          String[] otherSegments = other.rawVersion.split( "\\." );
69          
70          int minSegments = Math.min( thisSegments.length, otherSegments.length );
71          
72          for ( int index = 0; index < minSegments; index++ )
73          {
74              Matcher thisMatcher = startingDigits.matcher( thisSegments[index] );
75              
76              int thisValue;
77              
78              if( thisMatcher.find() )
79              {
80                  thisValue = Integer.parseInt( thisMatcher.group( 1 ) );
81              }
82              else
83              {
84                  thisValue = -1;
85              }
86              
87              Matcher otherMatcher = startingDigits.matcher( otherSegments[index] );
88              
89              int otherValue;
90              
91              if( otherMatcher.find() )
92              {
93                  otherValue = Integer.parseInt( otherMatcher.group( 1 ) );
94              }
95              else
96              {
97                  otherValue = -1;
98              }
99              
100             int compareValue = Integer.compare( thisValue, otherValue );
101             
102             if ( compareValue != 0 )
103             {
104                 return compareValue;
105             }
106 
107             compareValue = suffixRate( thisMatcher.group( 2 ) ) - suffixRate( otherMatcher.group( 2 ) );
108             if ( compareValue != 0 )
109             {
110                 return compareValue;
111             }
112             
113             // works for now, but needs improvement
114             compareValue = thisMatcher.group( 2 ).compareTo( otherMatcher.group( 2 ) );
115             
116             if ( compareValue != 0 )
117             {
118                 return compareValue;
119             }
120         }
121         
122         return ( thisSegments.length - otherSegments.length );
123     }
124     
125     private int suffixRate( String suffix ) {
126         if ( "-ea".equals( suffix ) )
127         {
128             return -100;
129         }
130         else if ( "".equals( suffix ) )
131         {
132             return 0;
133         }
134         else 
135         {
136             return 10;
137         }
138     }
139 
140     public boolean isBefore( JavaVersion other )
141     {
142         return this.compareTo( other ) < 0;
143     }
144 
145     public boolean isBefore( String other )
146     {
147         return this.compareTo( parse( other ) ) < 0;
148     }
149 
150     public boolean isAtLeast( JavaVersion other )
151     {
152         return this.compareTo( other ) >= 0;
153     }
154 
155     public boolean isAtLeast( String other )
156     {
157         return this.compareTo( parse( other ) ) >= 0;
158     }
159 
160     @Override
161     public String toString()
162     {
163         return rawVersion;
164     }
165 }