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

org.bidib.wizard.localhost.BidibNodeContainer Maven / Gradle / Ivy

package org.bidib.wizard.localhost;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.wizard.api.model.NodeInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * The {@code BidibNodeContainer} is a container that wraps the data of a node. It keeps the subNodes of a node and the
 * index of the feature to allow serve this information without call the real nodes.
 */
public class BidibNodeContainer {

    private static final Logger LOGGER = LoggerFactory.getLogger(BidibNodeContainer.class);

    private final Long uniqueId;

    private int currentNodeTabIndex;

    private int currentFeatureIndex;

    private List subNodes;

    public BidibNodeContainer(Long uniqueId) {
        this.uniqueId = uniqueId;

        LOGGER.info("Create BidibNodeContainer for uniqueId: {}", ByteUtils.formatHexUniqueId(this.uniqueId));
    }

    public void setCurrentNodeTabIndex(int currentNodeTabIndex) {
        this.currentNodeTabIndex = currentNodeTabIndex;
    }

    public int getCurrentNodeTabIndex() {
        return currentNodeTabIndex;
    }

    public void setSubNodes(final List subNodes) {
        this.subNodes = new LinkedList<>(subNodes);

        Collections.sort(this.subNodes, new Comparator() {

            @Override
            public int compare(NodeInterface o1, NodeInterface o2) {
                return (o1.getAddr()[0] < o2.getAddr()[0]) ? -1 : ((o1.getAddr()[0] == o2.getAddr()[0]) ? 0 : 1);
            }
        });

        LOGGER.info("Set the subNodes: {}", this.subNodes);
    }

    public List getSubNodes() {
        return this.subNodes;
    }

    public void clearSubNodes() {
        this.subNodes = null;
    }

    public NodeInterface getNextSubNode() {

        LOGGER
            .info("Get next subnode for uniqueId: {}, currentNodeTabIndex: {}",
                ByteUtils.formatHexUniqueId(this.uniqueId), this.currentNodeTabIndex);

        NodeInterface subNode = null;
        if (this.subNodes != null && currentNodeTabIndex < this.subNodes.size()) {
            subNode = this.subNodes.get(currentNodeTabIndex);
            currentNodeTabIndex++;
        }
        return subNode;
    }

    /**
     * @return the currentFeatureIndex
     */
    public int getNextFeatureIndex() {
        int featureIndex = currentFeatureIndex;
        currentFeatureIndex++;
        return featureIndex;
    }

    /**
     * @param currentFeatureIndex
     *            the currentFeatureIndex to set
     */
    public void setCurrentFeatureIndex(int currentFeatureIndex) {
        this.currentFeatureIndex = currentFeatureIndex;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy