Example usage for org.apache.commons.text Builder build

List of usage examples for org.apache.commons.text Builder build

Introduction

In this page you can find the example usage for org.apache.commons.text Builder build.

Prototype

T build();

Source Link

Document

Returns a reference to the object being constructed or result being calculated by the builder.

Usage

From source file:org.onosproject.d.config.ResourceIds.java

/**
 * Converts instance-identifier String into a ResourceId.
 *
 * @param input instance-identifier//  w  w  w . j  a  v a  2 s.  com
 * @return Corresponding ResourceId relative to root or null if {@code iid} is '/'
 * Returned ResourceId will not have root NodeKey in it's path.
 * consider using {@link #prefixDcsRoot(ResourceId)},
 * {@link #prefixYrsRoot(ResourceId)} to add them.
 */
public static ResourceId fromInstanceIdentifier(String input) {

    String[] nodes = input.split("/");
    List<NodeKey> nodeKeys = Arrays.stream(nodes).filter(s -> !s.isEmpty()).map(ResourceIds::toNodeKey)
            .collect(Collectors.toList());

    if (nodeKeys.isEmpty()) {
        return null;
    }

    Builder builder = ResourceId.builder();

    // fill-in null (=inherit from parent) nameSpace
    String lastNamespace = null;
    for (NodeKey nodeKey : nodeKeys) {
        if (nodeKey.schemaId().namespace() != null) {
            lastNamespace = nodeKey.schemaId().namespace();
        }
        if (nodeKey instanceof LeafListKey) {
            builder.addLeafListBranchPoint(nodeKey.schemaId().name(),
                    firstNonNull(nodeKey.schemaId().namespace(), lastNamespace),
                    ((LeafListKey) nodeKey).value());

        } else if (nodeKey instanceof ListKey) {
            builder.addBranchPointSchema(nodeKey.schemaId().name(), lastNamespace);
            for (KeyLeaf kl : ((ListKey) nodeKey).keyLeafs()) {
                builder.addKeyLeaf(kl.leafSchema().name(),
                        firstNonNull(kl.leafSchema().namespace(), lastNamespace), kl.leafValue());
            }
        } else {
            builder.addBranchPointSchema(nodeKey.schemaId().name(), lastNamespace);
        }
    }
    return builder.build();
}