2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.mdsal.binding.dom.adapter.query;
10 import org.opendaylight.mdsal.binding.api.query.DescendantQueryBuilder;
11 import org.opendaylight.mdsal.binding.api.query.MatchBuilderPath;
12 import org.opendaylight.mdsal.binding.api.query.QueryExpression;
13 import org.opendaylight.mdsal.binding.api.query.QueryStructureException;
14 import org.opendaylight.yangtools.yang.binding.ChildOf;
15 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
20 final class DefaultDescendantQueryBuilder<R extends DataObject, T extends DataObject>
21 implements DescendantQueryBuilder<T> {
22 private final InstanceIdentifierBuilder<T> childPath;
23 private final QueryBuilderState builder;
25 DefaultDescendantQueryBuilder(final DefaultQueryFactory factory, final InstanceIdentifier<T> rootPath) {
26 this.builder = new QueryBuilderState(factory, rootPath);
27 this.childPath = rootPath.builder();
31 @SuppressWarnings("unchecked")
32 public <N extends ChildOf<? super T>> DescendantQueryBuilder<N> extractChild(final Class<N> childClass) {
33 childPath.child(childClass);
34 return (DefaultDescendantQueryBuilder<R, N>) this;
38 @SuppressWarnings("unchecked")
39 public <C extends ChoiceIn<? super T> & DataObject, N extends ChildOf<? super C>>
40 DescendantQueryBuilder<N> extractChild(final Class<C> caseClass, final Class<N> childClass) {
41 childPath.child(caseClass, childClass);
42 return (DefaultDescendantQueryBuilder<R, N>) this;
46 public MatchBuilderPath<T, T> matching() {
47 final InstanceIdentifier<T> selectPath = childPath.build();
48 builder.setSelectPath(selectPath);
49 return new DefaultMatchBuilderPath<>(builder, selectPath, childPath);
53 public QueryExpression<T> build() throws QueryStructureException {
54 builder.setSelectPath(childPath.build());
55 return builder.buildQuery();