All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cryptomorin.xseries.reflection.VersionHandle Maven / Gradle / Ivy

There is a newer version: 12.1.0
Show newest version
package com.cryptomorin.xseries.reflection;

import java.util.concurrent.Callable;

public final class VersionHandle {
    private int version, patch;
    private T handle;
    // private RuntimeException errors;

    VersionHandle(int version, T handle) {
        this(version, 0, handle);
    }

    VersionHandle(int version, int patch, T handle) {
        if (XReflection.supports(version, patch)) {
            this.version = version;
            this.patch = patch;
            this.handle = handle;
        }
    }

    public VersionHandle(int version, int patch, Callable handle) {
        if (XReflection.supports(version, patch)) {
            this.version = version;
            this.patch = patch;

            try {
                this.handle = handle.call();
            } catch (Exception ignored) {
            }
        }
    }

    public VersionHandle(int version, Callable handle) {
        this(version, 0, handle);
    }

    public VersionHandle v(int version, T handle) {
        return v(version, 0, handle);
    }

    private boolean checkVersion(int version, int patch) {
        if (version == this.version && patch == this.patch)
            throw new IllegalArgumentException("Cannot have duplicate version handles for version: " + version + '.' + patch);
        return version > this.version && patch >= this.patch && XReflection.supports(version, patch);
    }

    public VersionHandle v(int version, int patch, Callable handle) {
        if (!checkVersion(version, patch)) return this;

        try {
            this.handle = handle.call();
        } catch (Exception ignored) {
        }

        this.version = version;
        this.patch = patch;
        return this;
    }

    public VersionHandle v(int version, int patch, T handle) {
        if (checkVersion(version, patch)) {
            this.version = version;
            this.patch = patch;
            this.handle = handle;
        }
        return this;
    }

    /**
     * If none of the previous version checks matched, it'll return this object.
     */
    public T orElse(T handle) {
        return this.version == 0 ? handle : this.handle;
    }

    public T orElse(Callable handle) {
        if (this.version == 0) {
            try {
                return handle.call();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return this.handle;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy