com.programmablefun.Version.java Source code

Java tutorial

Introduction

Here is the source code for com.programmablefun.Version.java

Source

/*
 * Copyright (c) 2017 Andreas Signer <asigner@gmail.com>
 *
 * This file is part of programmablefun.
 *
 * programmablefun is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * programmablefun is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with programmablefun.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.programmablefun;

import com.google.common.base.Preconditions;

import java.util.Comparator;

public class Version implements Comparable {
    private final int major;
    private final int minor;
    private final int patch;

    public Version(int major, int minor, int patch) {
        this.major = major;
        this.minor = minor;
        this.patch = patch;
    }

    public Version(String version) {
        String parts[] = version.split("\\.");
        Preconditions.checkState(parts.length == 3);
        this.major = Integer.parseInt(parts[0]);
        this.minor = Integer.parseInt(parts[1]);
        this.patch = Integer.parseInt(parts[2]);
    }

    public String toString() {
        return major + "." + minor + "." + patch;
    }

    @Override
    public int compareTo(Object o) {
        if (o.getClass() != this.getClass()) {
            return -1;
        }
        Version other = (Version) o;
        int delta = this.major - other.major;
        if (delta == 0) {
            delta = this.minor - other.major;
        }
        if (delta == 0) {
            delta = this.patch - other.patch;
        }
        return delta;
    }
}