/* * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.mdsal.binding.dom.adapter.query; import static com.google.common.base.Verify.verifyNotNull; import static java.util.Objects.requireNonNull; import com.google.common.base.Function; import com.google.common.base.VerifyException; import com.google.common.collect.Iterators; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; import java.util.Iterator; import java.util.Map.Entry; import java.util.Spliterator; import java.util.stream.Stream; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.gaul.modernizer_maven_annotations.SuppressModernizer; import org.opendaylight.mdsal.binding.api.query.QueryResult; import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree; import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode; import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode; import org.opendaylight.mdsal.dom.api.query.DOMQueryResult; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; @NonNullByDefault @SuppressModernizer final class DefaultQueryResult implements QueryResult, Function, QueryResult.Item> { private static final VarHandle ITEM_CODEC; static { try { ITEM_CODEC = MethodHandles.lookup().findVarHandle(DefaultQueryResult.class, "itemCodec", BindingDataObjectCodecTreeNode.class); } catch (NoSuchFieldException | IllegalAccessException e) { throw new ExceptionInInitializerError(e); } } private final DOMQueryResult domResult; private final BindingCodecTree codec; @SuppressWarnings("unused") @SuppressFBWarnings( value = { "NP_STORE_INTO_NONNULL_FIELD", "URF_UNREAD_FIELD" }, justification = "Ungrokked type annotation. https://github.com/spotbugs/spotbugs/issues/2749") private volatile @Nullable BindingDataObjectCodecTreeNode itemCodec = null; DefaultQueryResult(final BindingCodecTree codec, final DOMQueryResult domResult) { this.codec = requireNonNull(codec); this.domResult = requireNonNull(domResult); } @Override public Iterator> iterator() { return Iterators.transform(domResult.iterator(), this); } @Override public Spliterator> spliterator() { return new DefaultQueryResultSpliterator<>(this, domResult.spliterator()); } @Override public Stream> stream() { return domResult.stream().map(this); } @Override public Stream> parallelStream() { return domResult.parallelStream().map(this); } @Override public Item apply(final Entry domItem) { return new DefaultQueryResultItem<>(this, domItem); } T createObject(final Entry domItem) { final @Nullable BindingDataObjectCodecTreeNode local = (BindingDataObjectCodecTreeNode) ITEM_CODEC.getAcquire(this); return (local != null ? local : loadItemCodec(domItem.getKey())).deserialize(domItem.getValue()); } InstanceIdentifier createPath(final YangInstanceIdentifier domPath) { return verifyNotNull(codec.getInstanceIdentifierCodec().toBinding(domPath), "path"); } private BindingDataObjectCodecTreeNode loadItemCodec(final YangInstanceIdentifier domPath) { final BindingCodecTreeNode codecNode = codec.getSubtreeCodec(domPath); if (!(codecNode instanceof BindingDataObjectCodecTreeNode)) { throw new VerifyException("Unexpected codec " + codecNode); } @SuppressWarnings("unchecked") final BindingDataObjectCodecTreeNode ret = (BindingDataObjectCodecTreeNode) codecNode; final Object witness = ITEM_CODEC.compareAndExchangeRelease(this, null, ret); return witness == null ? ret : (BindingDataObjectCodecTreeNode) witness; } }