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

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

The newest version!
package commons.box.app;

import commons.box.util.Maps;
import commons.box.util.Strs;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.Locale;
import java.util.Map;
import java.util.Objects;

/**
 *
 */
public abstract class MetaLabelValueBase implements MetaLabelValue {
    private I18nLabel label;
    private Map labels;

    protected abstract R root();

    /**
     * 主对象对应的标签
     *
     * @return
     */
    @Override
    public String label() {
        if (this.label == null) return this.name();
        String lb = this.label.label();
        return (lb != null) ? lb : this.name();
    }

    /**
     * 主对象对应的标签 按语区获取
     *
     * @param locale
     * @return
     */
    @Override
    public String label(String locale) {
        if (this.label == null) return this.name();
        String lb = this.label.label(locale);
        return (lb != null) ? lb : this.name();
    }

    /**
     * 主对象对应的标签 按语区获取
     *
     * @param locale
     * @return
     */
    @Override
    public String label(Locale locale) {
        if (this.label == null) return this.name();
        String lb = this.label.label(locale);
        return (lb != null) ? lb : this.name();
    }

    /**
     * 全部 标签
     *
     * @return
     */
    @Override
    public Map labels() {
        return Maps.immmap(this.labels);
    }

    /**
     * 值对应的标签
     *
     * @param value
     * @return
     */
    @Override
    public String valueLabel(String value) {
        if (value == null || this.labels == null) return null;
        I18nLabel l = this.labels.get(this.name() + "." + value);
        if (l == null) return null;
        return l.label();
    }

    /**
     * 值对应的标签 带语区
     *
     * @param locale
     * @param value
     * @return
     */
    @Override
    public String valueLabel(String locale, String value) {
        if (value == null || this.labels == null) return null;
        I18nLabel l = this.labels.get(this.name() + "." + value);
        if (l == null) return null;
        return l.label(locale);
    }

    /**
     * 此方法用于内部机制 不允许直接调用
     *
     * @param label
     * @return
     */
    public R withLabel(String label) {
        if (this.label == null) this.label = new I18nLabel();
        this.label.add(label);
        return this.root();
    }

    /**
     * 此方法用于内部机制 不允许直接调用
     * 

* 注意,本方法每次调用均复制原内容并生成不可变实例,所以如果要批量增加请使用批量方法 * * @param name * @param labels * @return */ public R withLabels(String name, Map labels) { if (labels == null) return this.root(); if (Strs.isBlank(name)) { if (this.label == null) this.label = new I18nLabel(); this.label.addAll(labels); } else { if (this.labels == null) this.labels = new SafeMap<>(); I18nLabel i18n = this.labels.computeIfAbsent(name, k -> new I18nLabel()); i18n.addAll(labels); } return this.root(); } /** * 此方法用于内部机制 不允许直接调用 * * @param labels * @return */ public R withLabels(Map> labels) { return this.withLabels(labels, false); } /** * 此方法用于内部机制 不允许直接调用 *

* 因每次增加均应用了不可变实例,所以如果有批量记录需要添加请使用本方法 * * @param labels * @param overwrite * @return */ public R withLabels(Map> labels, boolean overwrite) { Map map = new SafeMap<>((this.labels == null || overwrite) ? Maps.immmap() : this.labels); if (labels != null) labels.forEach((k, v) -> { if (Strs.isBlank(k)) { if (this.label == null) this.label = new I18nLabel(); this.label.addAll(v); } else { I18nLabel i18n = map.computeIfAbsent(k, m -> new I18nLabel()); i18n.addAll(v); } }); this.labels = map; return this.root(); } public I18nLabel getLabel() { return label; } public void setLabel(I18nLabel 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; MetaLabelValueBase metaLabel = (MetaLabelValueBase) o; return Objects.equals(this.name(), metaLabel.name()) && Objects.equals(label, metaLabel.label) && Objects.equals(labels, metaLabel.labels); } @Override public int hashCode() { return Objects.hash(name(), label, labels); } @Override public String toString() { return new ToStringBuilder(this) .append("name", this.name()) .append("label", label) .append("labels", labels) .toString(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy