/* * Copyright (c) 2013 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.binding; import org.opendaylight.yangtools.util.HashCodeBuilder; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; final class InstanceIdentifierBuilderImpl implements InstanceIdentifier.InstanceIdentifierBuilder { private final ImmutableList.Builder pathBuilder = ImmutableList.builder(); private final HashCodeBuilder hashBuilder; private final Iterable basePath; private boolean wildcard = false; private PathArgument arg = null; InstanceIdentifierBuilderImpl() { this.hashBuilder = new HashCodeBuilder<>(); this.basePath = null; } InstanceIdentifierBuilderImpl(final PathArgument item, final Iterable pathArguments, final int hash, final boolean wildcard) { this.hashBuilder = new HashCodeBuilder<>(hash); this.basePath = pathArguments; this.wildcard = wildcard; this.arg = item; } @Override public int hashCode() { return hashBuilder.build(); } @SuppressWarnings("unchecked") InstanceIdentifierBuilderImpl addNode(final Class container) { arg = new Item(container); hashBuilder.addArgument(arg); pathBuilder.add(arg); if (Identifiable.class.isAssignableFrom(container)) { wildcard = true; } return (InstanceIdentifierBuilderImpl) this; } @SuppressWarnings("unchecked") , K extends Identifier> InstanceIdentifierBuilderImpl addNode(final Class listItem, final K listKey) { arg = new IdentifiableItem(listItem, listKey); hashBuilder.addArgument(arg); pathBuilder.add(arg); return (InstanceIdentifierBuilderImpl) this; } @Override public > InstanceIdentifierBuilderImpl child(final Class container) { return addNode(container); } @Override public & ChildOf, K extends Identifier> InstanceIdentifierBuilderImpl child(final Class listItem, final K listKey) { return addNode(listItem, listKey); } /** * Build an identifier which refers to a specific augmentation of the current InstanceIdentifier referenced by * the builder * * @param container * @param * @return */ @Override public > InstanceIdentifierBuilderImpl augmentation(final Class container) { return addNode(container); } @Override public InstanceIdentifier build() { Preconditions.checkState(arg != null, "No path arguments present"); final Iterable pathArguments; if (basePath == null) { pathArguments = pathBuilder.build(); } else { pathArguments = Iterables.concat(basePath, pathBuilder.build()); } @SuppressWarnings("unchecked") final InstanceIdentifier ret = (InstanceIdentifier) InstanceIdentifier.trustedCreate(arg, pathArguments, hashBuilder.build(), wildcard); return ret; } /* * @deprecated Use #build() instead. */ @Deprecated public InstanceIdentifier toInstance() { return build(); } }