Add NoZone support
[mdsal.git] / binding / yang-binding / src / main / java / org / opendaylight / yangtools / yang / binding / InstanceIdentifierBuilderImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.binding;
9
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;
19
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;
26
27     InstanceIdentifierBuilderImpl() {
28         this.hashBuilder = new HashCodeBuilder<>();
29         this.basePath = null;
30     }
31
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;
37         this.arg = item;
38     }
39
40     @Override
41     public int hashCode() {
42         return hashBuilder.build();
43     }
44
45     @Override
46     public boolean equals(final Object obj) {
47         if (this == obj) {
48             return true;
49         }
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());
55         }
56         return false;
57     }
58
59     @SuppressWarnings("unchecked")
60     <N extends DataObject> InstanceIdentifierBuilderImpl<N> addNode(final Class<N> container) {
61         arg = new Item<>(container);
62         hashBuilder.addArgument(arg);
63         pathBuilder.add(arg);
64
65         if (Identifiable.class.isAssignableFrom(container)) {
66             wildcard = true;
67         }
68
69         return (InstanceIdentifierBuilderImpl<N>) this;
70     }
71
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);
77         pathBuilder.add(arg);
78         return (InstanceIdentifierBuilderImpl<N>) this;
79     }
80
81     @Override
82     public <N extends ChildOf<? super T>> InstanceIdentifierBuilderImpl<N> child(final Class<N> container) {
83         return addNode(container);
84     }
85
86     @Override
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);
90     }
91
92     /**
93      * Build an identifier which refers to a specific augmentation of the current InstanceIdentifier referenced by
94      * the builder.
95      *
96      * @param container Augmentation to be added
97      * @param <N> Augmentation type
98      * @return This builder
99      */
100     @Override
101     public <N extends DataObject & Augmentation<? super T>> InstanceIdentifierBuilderImpl<N> augmentation(
102             final Class<N> container) {
103         return addNode(container);
104     }
105
106     @Override
107     public InstanceIdentifier<T> build() {
108         Preconditions.checkState(arg != null, "No path arguments present");
109
110         final Iterable<PathArgument> pathArguments;
111         if (basePath == null) {
112             pathArguments = pathBuilder.build();
113         } else {
114             pathArguments = Iterables.concat(basePath, pathBuilder.build());
115         }
116
117         @SuppressWarnings("unchecked")
118         final InstanceIdentifier<T> ret = (InstanceIdentifier<T>) InstanceIdentifier.trustedCreate(arg, pathArguments,
119             hashBuilder.build(), wildcard);
120         return ret;
121     }
122
123     /**
124      * Build the resulting InstanceIdentifier.
125      *
126      * @deprecated Use #build() instead.
127      */
128     @Override
129     @Deprecated
130     public InstanceIdentifier<T> toInstance() {
131         return build();
132     }
133 }