chat.viska.xmpp.Version.java Source code

Java tutorial

Introduction

Here is the source code for chat.viska.xmpp.Version.java

Source

/*
 * Copyright 2017 Kai-Chung Yan (?)
 *
 * 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 chat.viska.xmpp;

import java.util.Objects;
import org.apache.commons.lang3.Validate;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * Version of the XMPP standard.
 */
public class Version implements Comparable<Version> {

    private int major;
    private int minor;

    /**
     * Constructs with parts specified.
     */
    public Version(int major, int minor) {
        Validate.isTrue(major >= 0);
        Validate.isTrue(minor >= 0);
        this.major = major;
        this.minor = minor;
    }

    /**
     * Constructs with a literal.
     */
    public Version(String version) {
        String[] parts = version.split("\\.");
        major = Integer.parseInt(parts[0]);
        minor = Integer.parseInt(parts[1]);
        Validate.isTrue(major >= 0);
        Validate.isTrue(minor >= 0);
    }

    /**
     * Gets the major version.
     */
    public int getMajor() {
        return major;
    }

    /**
     * Gets the minor version.
     */
    public int getMinor() {
        return minor;
    }

    @Override
    public int compareTo(Version version) {
        final int majorDiff = Integer.compare(major, version.major);
        if (majorDiff != 0) {
            return majorDiff;
        }
        return Integer.compare(minor, version.minor);
    }

    @Override
    public boolean equals(@Nullable final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        final Version version = (Version) obj;
        return major == version.major && minor == version.minor;
    }

    @Override
    public int hashCode() {
        return Objects.hash(major, minor);
    }

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