XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEServerManager

【Java】我的世界XFE服务器管理器

公开
关注 0 Fork 0 Star 0
UTF-8
package com.xfestudio.xfeservermanager.api.util;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/** Internal helpers used by API records to enforce immutability and invariants. */
public final class ApiChecks {
    private ApiChecks() {
    }

    public static String notBlank(String value, String name) {
        Objects.requireNonNull(value, name);
        String trimmed = value.trim();
        if (trimmed.isEmpty()) {
            throw new IllegalArgumentException(name + " must not be blank");
        }
        return trimmed;
    }

    public static String lowerIdentifier(String value, String name) {
        return notBlank(value, name).toLowerCase(Locale.ROOT);
    }

    public static <T> List<T> immutableList(Collection<? extends T> values, String name) {
        Objects.requireNonNull(values, name);
        return List.copyOf(values);
    }

    public static <T> Set<T> immutableSet(Collection<? extends T> values, String name) {
        Objects.requireNonNull(values, name);
        return Set.copyOf(new LinkedHashSet<>(values));
    }

    public static Set<String> immutableLowercaseSet(Collection<String> values, String name) {
        Objects.requireNonNull(values, name);
        LinkedHashSet<String> result = new LinkedHashSet<>();
        for (String value : values) {
            result.add(lowerIdentifier(value, name + " element"));
        }
        return Set.copyOf(result);
    }

    public static <K, V> Map<K, V> immutableMap(Map<? extends K, ? extends V> values, String name) {
        Objects.requireNonNull(values, name);
        return Map.copyOf(new LinkedHashMap<>(values));
    }
}