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

commons.box.app.I18nLabel Maven / Gradle / Ivy

The newest version!
package commons.box.app;

import commons.box.util.Maps;
import commons.box.util.Strs;

import javax.annotation.Nonnull;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

/**
 * 应用标签
 */
public class I18nLabel implements DataName, DataObject {
    private static final long serialVersionUID = -8506335191932934608L;
    public static final String ALL = "*";
    private String name;
    private String label;
    private Map labels;

    /**
     * 获取对应的定义名称
     *
     * @return
     */
    @Override
    public String name() {
        return this.name;
    }

    /**
     * 获取显示标签
     *
     * @return
     */
    public String label() {
        if (this.label == null) return this.labelsMap().get(ALL);
        return this.label;
    }

    /**
     * 获取显示标签 按语区
     *
     * @param locale
     * @return
     */
    public String label(String locale) {
        if (Maps.isEmpty(this.labelsMap()) || Strs.isBlank(locale) || Strs.equals(locale, ALL)) return this.label();
        String lb = this.labelsMap().get(locale);
        if (lb == null) return this.label();
        return lb;
    }

    /**
     * 获取显示标签 按语区
     *
     * @param locale
     * @return
     */
    public String label(Locale locale) {
        if (locale == null) return this.label();
        return this.label(locale.toString());
    }

    /**
     * 加入默认值
     *
     * @param label
     */
    public I18nLabel add(String label) {
        this.label = label;
        this.labelsMap().put(ALL, label);
        return this;
    }

    public I18nLabel add(String locale, String label) {
        if (label == null) return this;

        if (this.label == null) {
            this.label = label;
            labelsMap().put(ALL, label);
        } else if (Strs.isBlank(locale) || Strs.equals(locale, ALL)) { // 空语区或者*语区,以默认语区为准
            this.label = label;
            labelsMap().put(ALL, label);
        } else {
            if (!labelsMap().containsKey(ALL)) labelsMap().put(ALL, label);

            if (Strs.contains(locale, "_")) {
                String l = Strs.subBefore(locale, "_");
                if (Strs.isNotBlank(l) && !labelsMap().containsKey(l)) labelsMap().put(l, label);
            }
            labelsMap().put(locale, label);
        }
        return this;
    }

    public I18nLabel add(Locale locale, String label) {
        if (label == null) return this;
        String lo = (locale != null) ? locale.toString() : "";
        this.add(lo, label);
        return this;
    }

    public I18nLabel addAll(Map labels) {
        if (labels != null) labels.forEach(this::add);
        return this;
    }

    @Nonnull
    private Map labelsMap() {
        if (this.labels == null) this.labels = new SafeMap<>();
        return this.labels;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public Map getLabels() {
        return labels;
    }

    public void setLabels(Map labels) {
        this.labels = new SafeMap<>(Maps.immmap(labels));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        I18nLabel i18nLabel = (I18nLabel) o;
        return Objects.equals(name, i18nLabel.name) &&
                Objects.equals(label, i18nLabel.label) &&
                Objects.equals(labels, i18nLabel.labels);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, label, labels);
    }

    @Override
    public String toString() {
        return "I18nLabel{" +
                "name='" + name + '\'' +
                ", label='" + label + '\'' +
                ", labels=" + labels +
                '}';
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy