ChartNamingRule.java

package com.mycim.utils;


import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;

/**
 * SPC data processing utility
 *
 * @author Qiansheng.Wang
 * @since 2020-03-27
 */
public class ChartNamingRule {

    public static final String IGNORE_CHAR = "M";

    private static final String CHART_SEPARATOR = "-";

    private static final Integer PARAMETER_PREFIX_LENGTH = 3;

    private static final Integer CHART_PREFIX_LENGTH = 2;

    private static final Integer IGNORE_CHAR_INDEX = 1;

    private ChartNamingRule() {
    }

    /**
     * Get Code Represented As Module/Section
     * <p>
     * The third character of the chart name is represented as a module / section
     *
     * @param chartName
     * @return
     */
    public static String getCodeRepresentedAsModule(String chartName) {
        if (StringUtils.isEmpty(chartName) || chartName.length() <= PARAMETER_PREFIX_LENGTH) {
            return null;
        }
        String temp = StringUtils.substringBefore(chartName, CHART_SEPARATOR);
        if (temp.length() >= 2) {
            return StringUtils.toString(temp.toCharArray()[temp.length() - 1]);
        } else {
            return null;
        }
    }

    /**
     * Get Code Represented As Permission User Group
     * <p>
     * The third character of the chart name is represented as a Permission User Group
     *
     * @param chartName
     * @return
     */
    public static String getCodeRepresentedAsPermissionUserGroup(String chartName) {
        if (StringUtils.isEmpty(chartName) || chartName.length() < PARAMETER_PREFIX_LENGTH) {
            return null;
        }

        return StringUtils.toString(chartName.toCharArray()[CHART_PREFIX_LENGTH]);
    }

    /**
     * 将MES中的parameter ID处理为SPC中的Chart Name.<br>
     *
     * <pre>
     * ChartNamingRule.getChartNameByParameterId(null)    = null
     * ChartNamingRule.getChartNameByParameterId("")      = ""
     * ChartNamingRule.getChartNameByParameterId("AA")    = "AA"
     * ChartNamingRule.getChartNameByParameterId("AM")    = "AM"
     * ChartNamingRule.getChartNameByParameterId("AAA")   = "AAA"
     * ChartNamingRule.getChartNameByParameterId("AAM")   = "AAM"
     * ChartNamingRule.getChartNameByParameterId("AMA")   = "AA"
     * ChartNamingRule.getChartNameByParameterId("AMMAM") = "AMAM"
     * </pre>
     *
     * @param parameterId
     * @return
     */
    public static String getChartNameByParameterId(String parameterId) {
        if (StringUtils.isEmpty(parameterId) || parameterId.length() < PARAMETER_PREFIX_LENGTH) {
            return parameterId;
        }

        String secondCode = StringUtils.toString(parameterId.toCharArray()[IGNORE_CHAR_INDEX]);
        if (!StringUtils.equalsIgnoreCase(IGNORE_CHAR, secondCode)) {
            return parameterId;
        }

        return StringUtils.replace(parameterId, IGNORE_CHAR, StringUtils.EMPTY, NumberUtils.INTEGER_ONE);
    }

    public static String getChartRuleRelationByParameterId(String parameterId) {
        String prefix = StringUtils.substringBefore(parameterId, CHART_SEPARATOR);

        if (prefix.length() >= CHART_PREFIX_LENGTH) {
            String secondCode = StringUtils.toString(prefix.toCharArray()[IGNORE_CHAR_INDEX]);
            if (prefix.length() == PARAMETER_PREFIX_LENGTH && StringUtils.equals(IGNORE_CHAR, secondCode)) {
                return StringUtils.replaceOnce(prefix, IGNORE_CHAR, StringUtils.EMPTY);
            } else {
                return StringUtils.substring(prefix, NumberUtils.INTEGER_ZERO, CHART_PREFIX_LENGTH);
            }
        } else {
            return StringUtils.EMPTY;
        }

    }

}