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

org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifierBuilder Maven / Gradle / Ivy

/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.yang.data.api;

import static java.util.Objects.requireNonNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.util.HashCodeBuilder;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;

final class YangInstanceIdentifierBuilder implements InstanceIdentifierBuilder {
    private final HashCodeBuilder hash;
    private final List path;

    YangInstanceIdentifierBuilder() {
        this.hash = new HashCodeBuilder<>();
        this.path = new ArrayList<>();
    }

    YangInstanceIdentifierBuilder(final List prefix, final int hash) {
        this.path = new ArrayList<>(prefix);
        this.hash = new HashCodeBuilder<>(hash);
    }

    private @NonNull InstanceIdentifierBuilder addArgument(final PathArgument arg) {
        path.add(arg);
        hash.addArgument(arg);
        return this;
    }

    @Override
    public InstanceIdentifierBuilder node(final PathArgument arg) {
        return addArgument(requireNonNull(arg));
    }

    @Override
    public InstanceIdentifierBuilder node(final QName nodeType) {
        return addArgument(new NodeIdentifier(nodeType));
    }

    @Override
    public InstanceIdentifierBuilder append(final Collection args) {
        path.addAll(args);
        args.forEach(hash::addArgument);
        return this;
    }

    @Override
    public InstanceIdentifierBuilder nodeWithKey(final QName nodeType, final QName key, final Object value) {
        return addArgument(NodeIdentifierWithPredicates.of(nodeType, key, value));
    }

    @Override
    public InstanceIdentifierBuilder nodeWithKey(final QName nodeType, final Map keyValues) {
        return addArgument(NodeIdentifierWithPredicates.of(nodeType, keyValues));
    }

    @Override
    public YangInstanceIdentifier build() {
        return FixedYangInstanceIdentifier.create(path, hash.build());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy