/* * 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 com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import java.util.Objects; import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.yangtools.util.HashCodeBuilder; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument; final class InstanceIdentifierBuilderImpl implements 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(); } @Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (obj instanceof InstanceIdentifierBuilderImpl) { @SuppressWarnings("unchecked") final InstanceIdentifierBuilderImpl otherBuilder = (InstanceIdentifierBuilderImpl) obj; return wildcard == otherBuilder.wildcard && Objects.equals(basePath, otherBuilder.basePath) && Objects.equals(arg, otherBuilder.arg) && Objects.equals(hashBuilder.build(), otherBuilder.hashBuilder.build()); } return false; } @Override public > InstanceIdentifierBuilderImpl child(final Class container) { return addNode(container); } @Override public & DataObject, N extends ChildOf> InstanceIdentifierBuilder child(final Class caze, final Class container) { return addWildNode(Item.of(caze, container)); } @Override public & ChildOf, K extends Identifier> InstanceIdentifierBuilderImpl child(final Class<@NonNull N> listItem, final K listKey) { return addNode(IdentifiableItem.of(listItem, listKey)); } @Override public & DataObject, K extends Identifier, N extends Identifiable & ChildOf> InstanceIdentifierBuilder child(final Class caze, final Class listItem, final K listKey) { return addNode(IdentifiableItem.of(caze, listItem, listKey)); } /** * Build an identifier which refers to a specific augmentation of the current InstanceIdentifier referenced by * the builder. * * @param container Augmentation to be added * @param Augmentation type * @return This builder */ @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()); } return InstanceIdentifier.trustedCreate(arg, pathArguments, hashBuilder.build(), wildcard); } @NonNull InstanceIdentifierBuilderImpl addWildNode(final PathArgument newArg) { if (Identifiable.class.isAssignableFrom(newArg.getType())) { wildcard = true; } return addNode(newArg); } @SuppressWarnings("unchecked") @NonNull InstanceIdentifierBuilderImpl addNode(final PathArgument newArg) { arg = newArg; hashBuilder.addArgument(newArg); pathBuilder.add(newArg); return (InstanceIdentifierBuilderImpl) this; } private @NonNull InstanceIdentifierBuilderImpl addNode(final Class container) { return addWildNode(Item.of(container)); } }