View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.data.pipeline.mysql.ingest.client;
19  
20  import lombok.Getter;
21  import lombok.extern.slf4j.Slf4j;
22  
23  import java.util.regex.Matcher;
24  import java.util.regex.Pattern;
25  
26  /**
27   * Server version.
28   */
29  @Getter
30  @Slf4j
31  public final class ServerVersion {
32      
33      private static final Pattern VERSION_PATTERN = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+).*");
34      
35      private final int major;
36      
37      private final int minor;
38      
39      private final int series;
40      
41      public ServerVersion(final String version) {
42          Matcher matcher = VERSION_PATTERN.matcher(version);
43          if (matcher.matches()) {
44              major = Short.parseShort(matcher.group(1));
45              minor = Short.parseShort(matcher.group(2));
46              series = Short.parseShort(matcher.group(3));
47          } else {
48              log.info("Could not match MySQL server version {}", version);
49              major = 0;
50              minor = 0;
51              series = 0;
52          }
53      }
54      
55      /**
56       * Greater than or equal to current version.
57       *
58       * @param major the major
59       * @param minor the minor
60       * @param series the series
61       * @return the boolean
62       */
63      public boolean greaterThanOrEqualTo(final int major, final int minor, final int series) {
64          if (this.major < major) {
65              return false;
66          }
67          if (this.major > major) {
68              return true;
69          }
70          if (this.minor < minor) {
71              return false;
72          }
73          if (this.minor > minor) {
74              return true;
75          }
76          return this.series >= series;
77      }
78  }