view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgVersion.java @ 999:1b1cc811e1b6 Jaipur-2018.1.x

Jaipur-2018.1.x branch created
author pavel.sher
date Thu, 21 Jun 2018 17:37:32 +0200
parents 1168c4c64d49
children
line wrap: on
line source
/*
 * Copyright 2000-2018 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jetbrains.buildServer.buildTriggers.vcs.mercurial;

import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.exception.ParseHgVersionException;
import org.jetbrains.annotations.NotNull;

/**
 * @author dmitry.neverov
 */
public class HgVersion implements Comparable<HgVersion> {

  private static final String PREFIX = "Mercurial Distributed SCM (version ";

  private final int myMajor;
  private final int myMinor;
  private final int myThird;


  public HgVersion(int major, int minor, int third) {
    myMajor = major;
    myMinor = minor;
    myThird = third;
  }


  public static HgVersion parse(@NotNull final String version) throws ParseHgVersionException {
    Parser p = new Parser(version);
    if (!p.skipString(PREFIX))
      throw new ParseHgVersionException(version);
    int major = p.readInt();
    p.skipString(".");
    int minor = p.readInt();
    int third = p.skipString(".") ? p.readInt() : 0;
    return new HgVersion(major, minor, third);
  }


  public boolean isEqualsOrGreaterThan(HgVersion other) {
    return compareTo(other) >= 0;
  }


  public boolean isLessThan(@NotNull HgVersion other) {
    return compareTo(other) < 0;
  }


  @Override
  public String toString() {
    return myMajor + "." + myMinor + "." + myThird;
  }

  @Override
  public boolean equals(Object o) {
    return (o instanceof HgVersion) && this.compareTo((HgVersion) o) == 0;
  }

  @Override
  public int hashCode() {
    int result = myMajor;
    result = 31 * result + myMinor;
    result = 31 * result + myThird;
    return result;
  }

  public int compareTo(HgVersion other) {
    int d = myMajor - other.myMajor;
    if (d != 0)
      return d;

    d = myMinor - other.myMinor;
    if (d != 0)
      return d;

    return myThird - other.myThird;
  }


  private static final class Parser {

    private final String myString;
    private int myIndex = 0;

    Parser(@NotNull String string) {
      myString = string;
    }


    boolean skipString(String str) {
      if (myIndex == myString.length() || myIndex + str.length() > myString.length())
        return false;
      String substr = myString.substring(myIndex, myIndex + str.length());
      if (substr.equals(str)) {
        myIndex = myIndex + str.length();
        return true;
      } else {
        return false;
      }
    }


    int readInt() {
      int result = 0;
      while (myIndex < myString.length() && Character.isDigit(myString.codePointAt(myIndex))) {
        result = result * 10 + Character.digit(myString.codePointAt(myIndex), 10);
        myIndex++;
      }
      return result;
    }
  }
}