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 @SuppressWarnings("unchecked")
52 final InstanceIdentifierBuilderImpl<T> otherBuilder = (InstanceIdentifierBuilderImpl<T>) obj;
53 return wildcard == otherBuilder.wildcard && Objects.equals(basePath, otherBuilder.basePath)
54 && Objects.equals(arg, otherBuilder.arg)
55 && Objects.equals(hashBuilder.build(), otherBuilder.hashBuilder.build());
61 public <N extends ChildOf<? super T>> InstanceIdentifierBuilderImpl<N> child(final Class<N> container) {
62 return addNode(container);
66 public <C extends ChoiceIn<? super T> & DataObject, N extends ChildOf<? super C>> InstanceIdentifierBuilder<N>
67 child(final Class<C> caze, final Class<N> container) {
68 return addWildNode(Item.of(caze, container));
72 public <N extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<N>> InstanceIdentifierBuilderImpl<N>
73 child(final Class<N> listItem, final K listKey) {
74 return addNode(IdentifiableItem.of(listItem, listKey));
78 public <C extends ChoiceIn<? super T> & DataObject, K extends Identifier<N>,
79 N extends Identifiable<K> & ChildOf<? super C>> InstanceIdentifierBuilder<N> child(final Class<C> caze,
80 final Class<N> listItem, final K listKey) {
81 return addNode(IdentifiableItem.of(caze, listItem, listKey));
85 * Build an identifier which refers to a specific augmentation of the current InstanceIdentifier referenced by
88 * @param container Augmentation to be added
89 * @param <N> Augmentation type
90 * @return This builder
93 public <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilderImpl<N> augmentation(
94 final Class<N> container) {
95 return addNode(container);
99 public InstanceIdentifier<T> build() {
100 Preconditions.checkState(arg != null, "No path arguments present");
102 final Iterable<PathArgument> pathArguments;
103 if (basePath == null) {
104 pathArguments = pathBuilder.build();
106 pathArguments = Iterables.concat(basePath, pathBuilder.build());
109 @SuppressWarnings("unchecked")
110 final InstanceIdentifier<T> ret = (InstanceIdentifier<T>) InstanceIdentifier.trustedCreate(arg, pathArguments,
111 hashBuilder.build(), wildcard);
115 <N extends DataObject> InstanceIdentifierBuilderImpl<N> addWildNode(final PathArgument newArg) {
116 if (Identifiable.class.isAssignableFrom(newArg.getType())) {
119 return addNode(newArg);
122 @SuppressWarnings("unchecked")
123 <N extends DataObject> InstanceIdentifierBuilderImpl<N> addNode(final PathArgument newArg) {
125 hashBuilder.addArgument(newArg);
126 pathBuilder.add(newArg);
127 return (InstanceIdentifierBuilderImpl<N>) this;
130 private <N extends DataObject> InstanceIdentifierBuilderImpl<N> addNode(final Class<N> container) {
131 return addWildNode(Item.of(container));