2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.binding;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.Iterables;
13 import java.util.Objects;
14 import org.opendaylight.yangtools.util.HashCodeBuilder;
15 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
20 final class InstanceIdentifierBuilderImpl<T extends DataObject> implements InstanceIdentifierBuilder<T> {
21 private final ImmutableList.Builder<PathArgument> pathBuilder = ImmutableList.builder();
22 private final HashCodeBuilder<PathArgument> hashBuilder;
23 private final Iterable<? extends PathArgument> basePath;
24 private boolean wildcard = false;
25 private PathArgument arg = null;
27 InstanceIdentifierBuilderImpl() {
28 this.hashBuilder = new HashCodeBuilder<>();
32 InstanceIdentifierBuilderImpl(final PathArgument item, final Iterable<? extends PathArgument> pathArguments,
33 final int hash, final boolean wildcard) {
34 this.hashBuilder = new HashCodeBuilder<>(hash);
35 this.basePath = pathArguments;
36 this.wildcard = wildcard;
41 public int hashCode() {
42 return hashBuilder.build();
46 public boolean equals(final Object obj) {
50 if (obj instanceof InstanceIdentifierBuilderImpl) {
51 InstanceIdentifierBuilderImpl<T> otherBuilder = (InstanceIdentifierBuilderImpl<T>) obj;
52 return wildcard == otherBuilder.wildcard && Objects.equals(basePath, otherBuilder.basePath)
53 && Objects.equals(arg, otherBuilder.arg)
54 && Objects.equals(hashBuilder.build(), otherBuilder.hashBuilder.build());
59 @SuppressWarnings("unchecked")
60 <N extends DataObject> InstanceIdentifierBuilderImpl<N> addNode(final Class<N> container) {
61 arg = new Item<>(container);
62 hashBuilder.addArgument(arg);
65 if (Identifiable.class.isAssignableFrom(container)) {
69 return (InstanceIdentifierBuilderImpl<N>) this;
72 @SuppressWarnings("unchecked")
73 <N extends DataObject & Identifiable<K>, K extends Identifier<N>> InstanceIdentifierBuilderImpl<N> addNode(
74 final Class<N> listItem, final K listKey) {
75 arg = new IdentifiableItem<>(listItem, listKey);
76 hashBuilder.addArgument(arg);
78 return (InstanceIdentifierBuilderImpl<N>) this;
82 public <N extends ChildOf<? super T>> InstanceIdentifierBuilderImpl<N> child(final Class<N> container) {
83 return addNode(container);
87 public <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilderImpl<N>
88 child(final Class<N> listItem, final K listKey) {
89 return addNode(listItem, listKey);
93 * Build an identifier which refers to a specific augmentation of the current InstanceIdentifier referenced by
96 * @param container Augmentation to be added
97 * @param <N> Augmentation type
98 * @return This builder
101 public <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilderImpl<N> augmentation(
102 final Class<N> container) {
103 return addNode(container);
107 public InstanceIdentifier<T> build() {
108 Preconditions.checkState(arg != null, "No path arguments present");
110 final Iterable<PathArgument> pathArguments;
111 if (basePath == null) {
112 pathArguments = pathBuilder.build();
114 pathArguments = Iterables.concat(basePath, pathBuilder.build());
117 @SuppressWarnings("unchecked")
118 final InstanceIdentifier<T> ret = (InstanceIdentifier<T>) InstanceIdentifier.trustedCreate(arg, pathArguments,
119 hashBuilder.build(), wildcard);
124 * Build the resulting InstanceIdentifier.
126 * @deprecated Use #build() instead.
130 public InstanceIdentifier<T> toInstance() {