AbstractMetaDataNode.java 2.67 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.cq.mcm.campaign.profile.impl;

import com.adobe.cq.mcm.campaign.profile.MetaDataNode;
import com.adobe.cq.mcm.campaign.profile.Options;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class AbstractMetaDataNode
implements MetaDataNode {
    private final String id;
    private final String path;
    private final String label;
    private final String type;
    private final List<MetaDataNode> children;
    private final Map<String, MetaDataNode> childById;
    private final Options options;

    protected AbstractMetaDataNode(String id, String parentPath, String label, String type, Options options) {
        this.id = id;
        this.path = parentPath != null ? parentPath + "/" + id : "";
        this.label = label;
        this.type = type != null && type.length() > 0 ? type : null;
        this.children = new ArrayList<MetaDataNode>(4);
        this.childById = new HashMap<String, MetaDataNode>(4);
        this.options = options;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public String getPath() {
        return this.path;
    }

    @Override
    public String getLabel() {
        return this.label;
    }

    @Override
    public String getType() {
        return this.type;
    }

    @Override
    public MetaDataNode add(MetaDataNode child) {
        this.children.add(child);
        this.childById.put(child.getId(), child);
        return child;
    }

    @Override
    public Iterator<MetaDataNode> getChildren() {
        return this.children.iterator();
    }

    @Override
    public MetaDataNode getChildByPath(String relPath) {
        int nextSegPos = relPath.indexOf("/");
        if (nextSegPos >= 0) {
            String childSeg = relPath.substring(0, nextSegPos);
            MetaDataNode segment = this.childById.get(childSeg);
            if (segment == null) {
                return null;
            }
            String childPath = relPath.substring(nextSegPos + 1);
            return segment.getChildByPath(childPath);
        }
        return this.childById.get(relPath);
    }

    @Override
    public boolean hasOptions() {
        return this.options != null;
    }

    @Override
    public Options getOptions() {
        return this.options;
    }

    public String toString() {
        StringBuilder dump = new StringBuilder();
        dump.append(this.path).append("->").append(this.label).append(" (").append(this.type).append("); ");
        for (MetaDataNode node : this.children) {
            dump.append(node.toString());
        }
        return dump.toString();
    }
}