View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.gpg;
20  
21  import java.util.Arrays;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  /**
26   * Represents the semver value of GPG.
27   * This is in most cases the first line of <code>gpg --version</code>
28   *
29   *
30   * @author Robert Scholte
31   * @since 3.0.0
32   */
33  public class GpgVersion implements Comparable<GpgVersion> {
34  
35      private static final Pattern VERSION_PATTERN = Pattern.compile("(\\d+\\.)+(\\d+)");
36  
37      private final int[] versionSegments;
38  
39      private GpgVersion(int... versionSegments) {
40          this.versionSegments = versionSegments;
41      }
42  
43      public static GpgVersion parse(String rawVersion) {
44          final Matcher versionMatcher = VERSION_PATTERN.matcher(rawVersion);
45          if (!versionMatcher.find()) {
46              throw new IllegalArgumentException("Can't parse version of " + rawVersion);
47          }
48  
49          final String[] rawVersionSegments = versionMatcher.group(0).split("\\.");
50  
51          final int[] versionSegments = new int[rawVersionSegments.length];
52          for (int index = 0; index < rawVersionSegments.length; index++) {
53              versionSegments[index] = Integer.parseInt(rawVersionSegments[index]);
54          }
55  
56          return new GpgVersion(versionSegments);
57      }
58  
59      @Override
60      public int compareTo(GpgVersion other) {
61          final int[] thisSegments = versionSegments;
62          final int[] otherSegments = other.versionSegments;
63  
64          int minSegments = Math.min(thisSegments.length, otherSegments.length);
65  
66          for (int index = 0; index < minSegments; index++) {
67              int compareValue = Integer.compare(thisSegments[index], otherSegments[index]);
68  
69              if (compareValue != 0) {
70                  return compareValue;
71              }
72          }
73  
74          return (thisSegments.length - otherSegments.length);
75      }
76  
77      /**
78       * Verify if this version is before some other version
79       *
80       * @param other the version to compare with
81       * @return {@code true} is this is less than {@code other}, otherwise {@code false}
82       */
83      public boolean isBefore(GpgVersion other) {
84          return this.compareTo(other) < 0;
85      }
86  
87      /**
88       * Verify if this version is at least some other version
89       *
90       * @param other the version to compare with
91       * @return  {@code true}  is this is greater than or equal to {@code other}, otherwise {@code false}
92       */
93      public boolean isAtLeast(GpgVersion other) {
94          return this.compareTo(other) >= 0;
95      }
96  
97      @Override
98      public String toString() {
99          if (versionSegments.length == 0) {
100             return "";
101         }
102 
103         final StringBuilder versionStringBuilder = new StringBuilder();
104         versionStringBuilder.append(versionSegments[0]);
105 
106         for (int index = 1; index < versionSegments.length; index++) {
107             versionStringBuilder.append('.').append(versionSegments[index]);
108         }
109 
110         return versionStringBuilder.toString();
111     }
112 
113     @Override
114     public boolean equals(final Object other) {
115         if (this == other) {
116             return true;
117         }
118 
119         if (!(other instanceof GpgVersion)) {
120             return false;
121         }
122 
123         final GpgVersion that = (GpgVersion) other;
124 
125         return compareTo(that) == 0;
126     }
127 
128     @Override
129     public int hashCode() {
130         return Arrays.hashCode(versionSegments);
131     }
132 }