View Javadoc
1   package org.apache.maven.plugins.gpg;
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 java.util.Arrays;
23  import java.util.regex.Matcher;
24  import java.util.regex.Pattern;
25  
26  /**
27   * Represents the semver value of GPG.
28   * This is in most cases the first line of <code>gpg --version</code>
29   * 
30   *
31   * @author Robert Scholte
32   * @since 3.0.0
33   */
34  public class GpgVersion implements Comparable<GpgVersion>
35  {
36  
37      private static final Pattern VERSION_PATTERN = Pattern.compile( "(\\d+\\.)+(\\d+)" );
38  
39      private final int[] versionSegments;
40  
41      private GpgVersion( int... versionSegments )
42      {
43          this.versionSegments = versionSegments;
44      }
45  
46      public static GpgVersion parse( String rawVersion )
47      {
48          final Matcher versionMatcher = VERSION_PATTERN.matcher( rawVersion );
49          if ( !versionMatcher.find() )
50          {
51              throw new IllegalArgumentException( "Can't parse version of " + rawVersion );
52          }
53  
54          final String[] rawVersionSegments = versionMatcher.group( 0 ).split( "\\." );
55  
56          final int[] versionSegments = new int[rawVersionSegments.length];
57          for ( int index = 0; index < rawVersionSegments.length; index++ )
58          {
59              versionSegments[index] = Integer.parseInt( rawVersionSegments[index] );
60          }
61  
62          return new GpgVersion( versionSegments );
63      }
64  
65      @Override
66      public int compareTo( GpgVersion other )
67      {
68          final int[] thisSegments = versionSegments;
69          final int[] otherSegments = other.versionSegments;
70  
71          int minSegments = Math.min( thisSegments.length, otherSegments.length );
72  
73          for ( int index = 0; index < minSegments; index++ )
74          {
75              int compareValue = Integer.compare( thisSegments[index], otherSegments[index] );
76  
77              if ( compareValue != 0 )
78              {
79                  return compareValue;
80              }
81          }
82  
83          return ( thisSegments.length - otherSegments.length );
84      }
85  
86      /**
87       * Verify if this version is before some other version
88       *
89       * @param other the version to compare with
90       * @return {@code true} is this is less than {@code other}, otherwise {@code false}
91       */
92      public boolean isBefore( GpgVersion other )
93      {
94          return this.compareTo( other ) < 0;
95      }
96  
97      /**
98       * Verify if this version is at least some other version
99       *
100      * @param other the version to compare with
101      * @return  {@code true}  is this is greater than or equal to {@code other}, otherwise {@code false}
102      */
103     public boolean isAtLeast( GpgVersion other )
104     {
105         return this.compareTo( other ) >= 0;
106     }
107 
108     @Override
109     public String toString()
110     {
111         if ( versionSegments.length == 0 )
112         {
113             return "";
114         }
115 
116         final StringBuilder versionStringBuilder = new StringBuilder();
117         versionStringBuilder.append( versionSegments[0] );
118 
119         for ( int index = 1; index < versionSegments.length; index++ )
120         {
121             versionStringBuilder.append( '.' ).append( versionSegments[index] );
122         }
123 
124         return versionStringBuilder.toString();
125     }
126 
127     @Override
128     public boolean equals( final Object other )
129     {
130         if ( this == other )
131         {
132             return true;
133         }
134 
135         if ( !( other instanceof GpgVersion ) )
136         {
137             return false;
138         }
139 
140         final GpgVersion that = (GpgVersion) other;
141 
142         return compareTo( that ) == 0;
143     }
144 
145     @Override
146     public int hashCode()
147     {
148         return Arrays.hashCode( versionSegments );
149     }
150 
151 }