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 static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.Function;
14 import com.google.common.base.VerifyException;
15 import com.google.common.collect.Iterators;
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.lang.invoke.MethodHandles;
18 import java.lang.invoke.VarHandle;
19 import java.util.Iterator;
20 import java.util.Map.Entry;
21 import java.util.Spliterator;
22 import java.util.stream.Stream;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.gaul.modernizer_maven_annotations.SuppressModernizer;
26 import org.opendaylight.mdsal.binding.api.query.QueryResult;
27 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
28 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
29 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
30 import org.opendaylight.mdsal.dom.api.query.DOMQueryResult;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 final class DefaultQueryResult<T extends DataObject>
39 implements QueryResult<T>, Function<Entry<YangInstanceIdentifier, NormalizedNode>, QueryResult.Item<T>> {
40 private static final VarHandle ITEM_CODEC;
44 ITEM_CODEC = MethodHandles.lookup().findVarHandle(DefaultQueryResult.class,
45 "itemCodec", BindingDataObjectCodecTreeNode.class);
46 } catch (NoSuchFieldException | IllegalAccessException e) {
47 throw new ExceptionInInitializerError(e);
51 private final DOMQueryResult domResult;
52 private final BindingCodecTree codec;
54 @SuppressWarnings("unused")
56 value = { "NP_STORE_INTO_NONNULL_FIELD", "URF_UNREAD_FIELD" },
57 justification = "Ungrokked type annotation. https://github.com/spotbugs/spotbugs/issues/2749")
58 private volatile @Nullable BindingDataObjectCodecTreeNode<T> itemCodec = null;
60 DefaultQueryResult(final BindingCodecTree codec, final DOMQueryResult domResult) {
61 this.codec = requireNonNull(codec);
62 this.domResult = requireNonNull(domResult);
66 public Iterator<Item<T>> iterator() {
67 return Iterators.transform(domResult.iterator(), this);
71 public Spliterator<Item<T>> spliterator() {
72 return new DefaultQueryResultSpliterator<>(this, domResult.spliterator());
76 public Stream<Item<T>> stream() {
77 return domResult.stream().map(this);
81 public Stream<Item<T>> parallelStream() {
82 return domResult.parallelStream().map(this);
86 public Item<T> apply(final Entry<YangInstanceIdentifier, NormalizedNode> domItem) {
87 return new DefaultQueryResultItem<>(this, domItem);
90 T createObject(final Entry<YangInstanceIdentifier, NormalizedNode> domItem) {
91 final @Nullable BindingDataObjectCodecTreeNode<T> local =
92 (BindingDataObjectCodecTreeNode<T>) ITEM_CODEC.getAcquire(this);
93 return (local != null ? local : loadItemCodec(domItem.getKey())).deserialize(domItem.getValue());
96 InstanceIdentifier<T> createPath(final YangInstanceIdentifier domPath) {
97 return verifyNotNull(codec.getInstanceIdentifierCodec().toBinding(domPath), "path");
100 private BindingDataObjectCodecTreeNode<T> loadItemCodec(final YangInstanceIdentifier domPath) {
101 final BindingCodecTreeNode codecNode = codec.getSubtreeCodec(domPath);
102 if (!(codecNode instanceof BindingDataObjectCodecTreeNode)) {
103 throw new VerifyException("Unexpected codec " + codecNode);
106 @SuppressWarnings("unchecked")
107 final BindingDataObjectCodecTreeNode<T> ret = (BindingDataObjectCodecTreeNode<T>) codecNode;
108 final Object witness = ITEM_CODEC.compareAndExchangeRelease(this, null, ret);
109 return witness == null ? ret : (BindingDataObjectCodecTreeNode<T>) witness;