Home / Class/ ApiVersion Class — spring-boot Architecture

ApiVersion Class — spring-boot Architecture

Architecture documentation for the ApiVersion class in ApiVersion.java from the spring-boot codebase.

Entity Profile

Source Code

buildpack/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ApiVersion.java lines 34–150

public final class ApiVersion implements Comparable<ApiVersion> {

	private static final Pattern PATTERN = Pattern.compile("^v?(\\d+)\\.(\\d*)$");

	private static final Comparator<ApiVersion> COMPARATOR = Comparator.comparing(ApiVersion::getMajor)
		.thenComparing(ApiVersion::getMinor);

	private final int major;

	private final int minor;

	private ApiVersion(int major, int minor) {
		this.major = major;
		this.minor = minor;
	}

	/**
	 * Return the major version number.
	 * @return the major version
	 */
	int getMajor() {
		return this.major;
	}

	/**
	 * Return the minor version number.
	 * @return the minor version
	 */
	int getMinor() {
		return this.minor;
	}

	/**
	 * Returns if this API version supports the given version. A {@code 0.x} matches only
	 * the same version number. A 1.x or higher release matches when the versions have the
	 * same major version and a minor that is equal or greater.
	 * @param other the version to check against
	 * @return if the specified API version is supported
	 */
	public boolean supports(ApiVersion other) {
		if (equals(other)) {
			return true;
		}
		if (this.major == 0 || this.major != other.major) {
			return false;
		}
		return this.minor >= other.minor;
	}

	/**
	 * Returns if this API version supports any of the given versions.
	 * @param others the versions to check against
	 * @return if any of the specified API versions are supported
	 * @see #supports(ApiVersion)
	 */
	public boolean supportsAny(ApiVersion... others) {
		for (ApiVersion other : others) {
			if (supports(other)) {
				return true;
			}
		}
		return false;
	}

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

	@Override
	public int hashCode() {
		return this.major * 31 + this.minor;
	}

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

	/**
	 * Factory method to parse a string into an {@link ApiVersion} instance.
	 * @param value the value to parse.
	 * @return the corresponding {@link ApiVersion}
	 * @throws IllegalArgumentException if the value could not be parsed
	 */
	public static ApiVersion parse(String value) {
		Assert.hasText(value, "'value' must not be empty");
		Matcher matcher = PATTERN.matcher(value);
		Assert.isTrue(matcher.matches(),
				() -> "'value' [%s] must contain a well formed version number".formatted(value));
		try {
			int major = Integer.parseInt(matcher.group(1));
			int minor = Integer.parseInt(matcher.group(2));
			return new ApiVersion(major, minor);
		}
		catch (NumberFormatException ex) {
			throw new IllegalArgumentException("'value' must contain a well formed version number [" + value + "]", ex);
		}
	}

	public static ApiVersion of(int major, int minor) {
		return new ApiVersion(major, minor);
	}

	@Override
	public int compareTo(ApiVersion other) {
		return COMPARATOR.compare(this, other);
	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free