/*
 * Copyright 2014-2020 the original author or authors.
 *
 * 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 de.codecentric.boot.admin.server.domain.values;

import java.io.Serializable;
import java.util.Map;
import java.util.Scanner;

import javax.annotation.Nullable;

import org.springframework.util.Assert;

@lombok.Data
public final class BuildVersion implements Serializable, Comparable<BuildVersion> {

	private final String value;

	private BuildVersion(String value) {
		Assert.hasText(value, "'value' must not be empty");
		this.value = value;
	}

	public static BuildVersion valueOf(String s) {
		return new BuildVersion(s);
	}

	@Nullable
	public static BuildVersion from(Map<String, ?> map) {
		if (map.isEmpty()) {
			return null;
		}

		Object build = map.get("build");
		if (build instanceof Map) {
			Object version = ((Map<?, ?>) build).get("version");
			if (version instanceof String) {
				return valueOf((String) version);
			}
		}

		Object version = map.get("build.version");
		if (version instanceof String) {
			return valueOf((String) version);
		}

		version = map.get("version");
		if (version instanceof String) {
			return valueOf((String) version);
		}
		return null;
	}

	public String getValue() {
		return this.value;
	}

	@Override
	public String toString() {
		return this.value;
	}

	@Override
	public int compareTo(BuildVersion other) {
		try (Scanner s1 = new Scanner(this.value); Scanner s2 = new Scanner(other.value)) {
			s1.useDelimiter("[.\\-+]");
			s2.useDelimiter("[.\\-+]");

			while (s1.hasNext() && s2.hasNext()) {
				int c;
				if (s1.hasNextInt() && s2.hasNextInt()) {
					c = Integer.compare(s1.nextInt(), s2.nextInt());
				}
				else {
					c = s1.next().compareTo(s2.next());
				}
				if (c != 0) {
					return c;
				}
			}

			if (s1.hasNext()) {
				return 1;
			}
			else if (s2.hasNext()) {
				return -1;
			}
		}
		return 0;
	}

}

@@ -816,22 +816,27 @@
 rk.util.
-Assert
+StringUtils
 ;%0A%0A@lomb
@@ -928,16 +928,75 @@
 ion%3E %7B%0A%0A
+%09private static final String DEFAULT_VERSION = %22UNKNOWN%22;%0A%0A
 %09private
@@ -1061,60 +1061,87 @@
 %7B%0A%09%09
-Assert.hasText(value, %22'value' must not be empty%22);%0A
+if(!StringUtils.hasText(value)) %7B%0A%09%09%09this.value = DEFAULT_VERSION;%0A%09%09%7D else %7B%0A%09
 %09%09th
@@ -1154,24 +1154,28 @@
 ue = value;%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09public 

Optimize the issue of default version for BuildVersion Constructor . (#2060)