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

org.javersion.path.PropertyTree Maven / Gradle / Ivy

There is a newer version: 0.15.3
Show newest version
/*
 * Copyright 2013 Samppa Saarela
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.javersion.path;

import static java.util.Collections.unmodifiableCollection;
import static java.util.Collections.unmodifiableMap;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

import org.javersion.path.PropertyPath.NodeId;

import com.google.common.collect.Maps;

public class PropertyTree {

    public static PropertyTree build(PropertyPath...paths) {
        return build(Arrays.asList(paths));
    }
    public static PropertyTree build(Collection paths) {
        Map nodes = Maps.newHashMapWithExpectedSize(paths.size());
        for (PropertyPath path : paths) {
            PropertyTree parentTree = getOrCreate(PropertyPath.ROOT, nodes);
            for (PropertyPath subPath : path) {
                PropertyTree childTree = getOrCreate(subPath, nodes);
                if (parentTree != null) {
                    parentTree.children.put(subPath.getNodeId(), childTree);
                }
                parentTree = childTree;
            }
        }
        return nodes.get(PropertyPath.ROOT);
    }
    private static PropertyTree getOrCreate(PropertyPath path,
            Map nodes) {
        PropertyTree childTree = nodes.get(path);
        if (childTree == null) {
            childTree = new PropertyTree(path);
            nodes.put(path, childTree);
        }
        return childTree;
    }

    public final PropertyPath path;

    private Map children = Maps.newLinkedHashMap();

    private PropertyTree(PropertyPath path) {
        this.path = path;
    }

    public NodeId getNodeId() {
        return path.getNodeId();
    }

    public Collection getChildren() {
        return unmodifiableCollection(children.values());
    }

    public Map getChildrenMap() {
        return unmodifiableMap(children);
    }

    public PropertyTree get(NodeId childNode) {
        return children.get(childNode);
    }

    public boolean hasChildren() {
        return !children.isEmpty();
    }

    public PropertyTree get(PropertyPath path) {
        PropertyTree match = this;
        for (PropertyPath node : path) {
            if (match == null) continue;
            else match = match.get(node.getNodeId());
        }
        return match;
    }

    public String toString() {
        return path.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy