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

org.bidib.wizard.common.labels.BidibLabelUtils Maven / Gradle / Ivy

package org.bidib.wizard.common.labels;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.lang3.StringUtils;
import org.bidib.jbidibc.core.schema.bidibbase.BaseLabel;
import org.bidib.jbidibc.core.schema.bidiblabels.AccessoryLabel;
import org.bidib.jbidibc.core.schema.bidiblabels.FeedbackPortLabels;
import org.bidib.jbidibc.core.schema.bidiblabels.FeedbackPositionLabels;
import org.bidib.jbidibc.core.schema.bidiblabels.FlagLabels;
import org.bidib.jbidibc.core.schema.bidiblabels.MacroLabels;
import org.bidib.jbidibc.core.schema.bidiblabels.NodeLabel;
import org.bidib.jbidibc.core.schema.bidiblabels.NodeLabels;
import org.bidib.jbidibc.core.schema.bidiblabels.PortLabel;
import org.bidib.jbidibc.core.schema.bidiblabels.PortLabels;
import org.bidib.wizard.model.ports.Port;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BidibLabelUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(BidibLabelUtils.class);

    public static BaseLabel getFeedbackPortLabel(final NodeLabels nodeLabels, int labelId) {

        if (nodeLabels.getFeedbackPortLabels() == null) {
            return null;
        }
        // find the label for the id
        BaseLabel portLabel =
            IterableUtils.find(nodeLabels.getFeedbackPortLabels().getPortLabel(), label -> label.getIndex() == labelId);

        return portLabel;
    }

    public static BaseLabel getFeedbackPositionLabel(final NodeLabels nodeLabels, int labelId) {

        if (nodeLabels.getFeedbackPositionLabels() == null) {
            return null;
        }
        // find the label for the id
        BaseLabel portLabel =
            IterableUtils
                .find(nodeLabels.getFeedbackPositionLabels().getPortLabel(), label -> label.getIndex() == labelId);

        return portLabel;
    }

    public static BaseLabel getLabel(PortLabels portLabels, final WizardLabelFactory.LabelTypes labelType, int index) {
        if (portLabels == null) {
            return null;
        }

        for (PortLabel label : portLabels.getPortLabel()) {
            if (label.getType() == labelType.getPortType() && label.getIndex() == index) {
                return label;
            }
        }
        return new BaseLabel();
    }

    public static Integer getPortNumberByLabel(
        PortLabels portLabels, final WizardLabelFactory.LabelTypes labelType, String labelString) {
        if (portLabels == null) {
            return null;
        }

        for (PortLabel label : portLabels.getPortLabel()) {
            if (label.getType() == labelType.getPortType() && label.getLabel().equalsIgnoreCase(labelString)) {
                return label.getIndex();
            }
        }
        return null;
    }

    public static Integer getFeedbackPortNumberByLabel(FeedbackPortLabels portLabels, String labelString) {
        if (portLabels == null) {
            return null;
        }

        for (BaseLabel label : portLabels.getPortLabel()) {
            if (label.getLabel().equalsIgnoreCase(labelString)) {
                return label.getIndex();
            }
        }
        return null;
    }

    public static BaseLabel getMacroLabel(final NodeLabels nodeLabels, int macroId) {

        if (nodeLabels.getMacroLabels() == null) {
            return null;
        }
        // find the label for the id
        BaseLabel portLabel =
            IterableUtils.find(nodeLabels.getMacroLabels().getMacroLabel(), label -> label.getIndex() == macroId);

        return portLabel;
    }

    public static String getNodeLabel(final NodeLabels nodeLabels) {

        if (nodeLabels == null || nodeLabels.getNodeLabel() == null) {
            return null;
        }

        return nodeLabels.getNodeLabel().getUserName();
    }

    public static PortLabel getPortLabel(
        List portLabels, final WizardLabelFactory.LabelTypes labelType, int index) {
        for (PortLabel label : portLabels) {
            if (label.getType() == labelType.getPortType() && label.getIndex() == index) {
                return label;
            }
        }
        PortLabel portLabel = new PortLabel().withIndex(index).withType(labelType.getPortType());
        portLabels.add(portLabel);
        return portLabel;
    }

    public static BaseLabel getLabel(List baseLabels, int index) {
        for (BaseLabel label : baseLabels) {
            if (label.getIndex() == index) {
                return label;
            }
        }
        return new BaseLabel();
    }

    public static void replaceFeedbackPortLabel(final NodeLabels nodeLabels, int labelId, final String labelString) {

        if (nodeLabels.getFeedbackPortLabels() == null) {
            nodeLabels.setFeedbackPortLabels(new FeedbackPortLabels());
        }

        replaceLabel(nodeLabels.getFeedbackPortLabels().getPortLabel(), labelId, labelString);
    }

    public static void replaceFeedbackPositionLabel(
        final NodeLabels nodeLabels, int labelId, final String labelString) {

        if (nodeLabels.getFeedbackPositionLabels() == null) {
            nodeLabels.setFeedbackPositionLabels(new FeedbackPositionLabels());
        }

        replaceLabel(nodeLabels.getFeedbackPositionLabels().getPortLabel(), labelId, labelString);
    }

    public static void replaceFlagLabel(final NodeLabels nodeLabels, int labelId, final String labelString) {

        if (nodeLabels.getFlagLabels() == null) {
            nodeLabels.setFlagLabels(new FlagLabels());
        }

        replaceLabel(nodeLabels.getFlagLabels().getFlagLabel(), labelId, labelString);
    }

    public static void replaceMacroLabel(final NodeLabels nodeLabels, int labelId, final String labelString) {

        if (nodeLabels.getMacroLabels() == null) {
            nodeLabels.setMacroLabels(new MacroLabels());
        }

        replaceLabel(nodeLabels.getMacroLabels().getMacroLabel(), labelId, labelString);
    }

    public static void replaceNodeLabel(final NodeLabels nodeLabels, final String labelString) {

        if (nodeLabels.getNodeLabel() == null) {
            nodeLabels.setNodeLabel(new NodeLabel());
        }

        nodeLabels.getNodeLabel().setUserName(labelString);
    }

    public static void replaceLabel(List portLabels, int labelId, final String labelString) {

        // find the label for the id
        BaseLabel portLabel = IterableUtils.find(portLabels, label -> label.getIndex() == labelId);
        if (portLabel == null) {
            portLabel = new BaseLabel().withIndex(labelId).withLabel(labelString);
            portLabels.add(portLabel);
        }
        else {
            portLabel.setLabel(labelString);
        }
    }

    public static void replaceLabel(
        final NodeLabels nodeLabels, final WizardLabelFactory.LabelTypes labelType, int labelId,
        final String labelString) {

        if (nodeLabels.getPortLabels() == null) {
            nodeLabels.setPortLabels(new PortLabels());
        }

        PortLabels portLabels = nodeLabels.getPortLabels();
        if (portLabels == null) {
            portLabels = new PortLabels();
            nodeLabels.setPortLabels(portLabels);
        }

        PortLabel portLabel = getPortLabel(portLabels.getPortLabel(), labelType, labelId);
        if (portLabel != null) {
            portLabel.setLabel(labelString);
        }
        else {
            LOGGER
                .warn("No port label available to replace, labelType: {}, labelId: {}, labelString: {}", labelType,
                    labelId, labelString);
        }
    }

    /**
     * Apply the port labels to the provided ports.
     *
     * @param portLabels
     *            the port labels
     * @param labelType
     *            the label type
     * @param ports
     *            the ports
     */
    public static > void applyFeedbackPortLabels(final NodeLabels nodeLabels, final List ports) {

        if (nodeLabels.getFeedbackPortLabels() == null) {
            nodeLabels.setFeedbackPortLabels(new FeedbackPortLabels());
        }

        applyPortLabels(nodeLabels.getFeedbackPortLabels().getPortLabel(), ports);
    }

    /**
     * Apply the port labels to the provided ports.
     *
     * @param portLabels
     *            the port labels
     * @param labelType
     *            the label type
     * @param ports
     *            the ports
     */
    public static > void applyPortLabels(
        final NodeLabels nodeLabels, final WizardLabelFactory.LabelTypes labelType, final List ports) {

        if (nodeLabels.getPortLabels() == null) {
            nodeLabels.setPortLabels(new PortLabels());
        }

        applyPortLabels(nodeLabels.getPortLabels().getPortLabel(), labelType, ports);
    }

    /**
     * Apply the port labels to the provided ports.
     *
     * @param portLabels
     *            the port labels
     * @param labelType
     *            the label type
     * @param ports
     *            the ports
     */
    private static > void applyPortLabels(
        final List portLabels, final WizardLabelFactory.LabelTypes labelType, final List ports) {

        if (CollectionUtils.isEmpty(portLabels)) {
            LOGGER.warn("No port labels available.");
        }
        else {
            for (T port : ports) {
                // set the label
                String labelString = BidibLabelUtils.getPortLabel(portLabels, labelType, port.getId()).getLabel();
                port.setLabel(labelString);
            }
        }
    }

    /**
     * Apply the port labels to the provided ports.
     *
     * @param portLabels
     *            the port labels
     * @param labelType
     *            the label type
     * @param ports
     *            the ports
     */
    public static > void applyPortLabels(final List portLabels, final List ports) {

        if (CollectionUtils.isEmpty(portLabels)) {
            LOGGER.warn("No labels available.");
        }
        else {
            for (T port : ports) {
                // set the label
                String labelString = BidibLabelUtils.getLabel(portLabels, port.getId()).getLabel();
                port.setLabel(labelString);
            }
        }
    }

    public static Map getCopyOfAccessoryLabelsMap(final NodeLabels inMap) {

        Map labelMap = new HashMap();

        if (inMap != null && inMap.getAccessoryLabels() != null) {

            for (AccessoryLabel labelNode : inMap.getAccessoryLabels().getAccessoryLabel()) {

                if (StringUtils.isNotBlank(labelNode.getLabel())) {
                    labelMap.put(labelNode.getIndex(), labelNode.getLabel());
                }
            }
        }
        return labelMap;
    }

    public static Map getCopyOfMacroLabelsMap(final NodeLabels inMap) {

        Map labelMap = new HashMap();

        if (inMap != null && inMap.getMacroLabels() != null) {

            for (BaseLabel labelNode : inMap.getMacroLabels().getMacroLabel()) {

                if (StringUtils.isNotBlank(labelNode.getLabel())) {
                    labelMap.put(labelNode.getIndex(), labelNode.getLabel());
                }
            }
        }
        return labelMap;
    }

    public static Map getCopyOfFlagLabelsMap(final NodeLabels inMap) {

        Map labelMap = new HashMap();

        if (inMap != null && inMap.getFlagLabels() != null) {

            for (BaseLabel labelNode : inMap.getFlagLabels().getFlagLabel()) {

                if (StringUtils.isNotBlank(labelNode.getLabel())) {
                    labelMap.put(labelNode.getIndex(), labelNode.getLabel());
                }
            }
        }
        return labelMap;
    }

    public static Map getCopyOfPortLabelsMap(
        final NodeLabels inMap, final WizardLabelFactory.LabelTypes labelType) {

        Map labelMap = new HashMap();

        if (inMap != null && inMap.getPortLabels() != null) {

            for (PortLabel labelNode : inMap.getPortLabels().getPortLabel()) {

                if (labelNode.getType() == labelType.getPortType() && StringUtils.isNotBlank(labelNode.getLabel())) {
                    labelMap.put(labelNode.getIndex(), labelNode.getLabel());
                }
            }
        }
        return labelMap;
    }

    public static Map getCopyOfFeedbackPortLabelsMap(final NodeLabels inMap) {

        Map labelMap = new HashMap();

        if (inMap != null && inMap.getFeedbackPortLabels() != null) {

            for (BaseLabel labelNode : inMap.getFeedbackPortLabels().getPortLabel()) {

                if (StringUtils.isNotBlank(labelNode.getLabel())) {
                    labelMap.put(labelNode.getIndex(), labelNode.getLabel());
                }
            }
        }
        return labelMap;
    }

    public static Map getCopyOfFeedbackPositionLabelsMap(final NodeLabels inMap) {

        Map labelMap = new HashMap();

        if (inMap != null && inMap.getFeedbackPositionLabels() != null) {

            for (BaseLabel labelNode : inMap.getFeedbackPositionLabels().getPortLabel()) {

                if (StringUtils.isNotBlank(labelNode.getLabel())) {
                    labelMap.put(labelNode.getIndex(), labelNode.getLabel());
                }
            }
        }
        return labelMap;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy