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

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

There is a newer version: 0.15.3
Show newest version
package org.javersion.path;

import static com.google.common.base.Preconditions.checkNotNull;

import org.javersion.path.PropertyPath.NodeId;
import org.javersion.util.Check;

import com.google.common.base.Preconditions;

public abstract class SchemaBase> {

    public abstract This getChild(NodeId nodeId);

    public This get(PropertyPath path) {
        This schema = find(path);
        if (schema == null) {
            throw new IllegalArgumentException("Path not found: " + path);
        }
        return schema;
    }

    public This find(PropertyPath path) {
        Check.notNull(path, "path");

        @SuppressWarnings("unchecked")
        This currentMapping = (This) this;
        for (PropertyPath currentPath : path.asList()) {
            NodeId nodeId = currentPath.getNodeId();
            @SuppressWarnings("unchecked")
            This childMapping = currentMapping.getChild(nodeId);
            while (childMapping == null && (nodeId = nodeId.fallbackId()) != null) {
                childMapping = currentMapping.getChild(nodeId);
            }
            if (childMapping != null) {
                currentMapping = childMapping;
            } else {
                return null;
            }
        }
        return currentMapping;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy