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 java.util.Objects.requireNonNull;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.MethodHandles.Lookup;
15 import java.lang.invoke.VarHandle;
16 import java.util.Map.Entry;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.mdsal.binding.api.query.QueryResult;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 final class DefaultQueryResultItem<T extends DataObject> implements QueryResult.Item<T> {
27 private static final VarHandle OBJECT;
28 private static final VarHandle PATH;
31 final Lookup lookup = MethodHandles.lookup();
33 OBJECT = lookup.findVarHandle(DefaultQueryResultItem.class, "object", DataObject.class);
34 PATH = lookup.findVarHandle(DefaultQueryResultItem.class, "path", InstanceIdentifier.class);
35 } catch (NoSuchFieldException | IllegalAccessException e) {
36 throw new ExceptionInInitializerError(e);
40 private final Entry<YangInstanceIdentifier, NormalizedNode> domItem;
41 private final DefaultQueryResult<T> result;
43 @SuppressWarnings("unused")
45 value = { "NP_STORE_INTO_NONNULL_FIELD", "URF_UNREAD_FIELD" },
46 justification = "Ungrokked type annotation. https://github.com/spotbugs/spotbugs/issues/2749")
47 private volatile @Nullable InstanceIdentifier<T> path = null;
48 @SuppressWarnings("unused")
50 value = { "NP_STORE_INTO_NONNULL_FIELD", "URF_UNREAD_FIELD" },
51 justification = "Ungrokked type annotation. https://github.com/spotbugs/spotbugs/issues/2749")
52 private volatile @Nullable T object = null;
54 DefaultQueryResultItem(final DefaultQueryResult<T> result,
55 final Entry<YangInstanceIdentifier, NormalizedNode> domItem) {
56 this.result = requireNonNull(result);
57 this.domItem = requireNonNull(domItem);
62 final @Nullable T local = (T) OBJECT.getAcquire(this);
63 return local != null ? local : loadObject();
67 public InstanceIdentifier<T> path() {
68 final @Nullable InstanceIdentifier<T> local = (InstanceIdentifier<T>) PATH.getAcquire(this);
69 return local != null ? local : loadPath();
72 private T loadObject() {
73 final T ret = result.createObject(domItem);
74 final Object witness = OBJECT.compareAndExchangeRelease(this, null, ret);
75 return witness == null ? ret : (T) witness;
78 @SuppressWarnings("unchecked")
79 private InstanceIdentifier<T> loadPath() {
80 final InstanceIdentifier<T> ret = result.createPath(domItem.getKey());
81 final Object witness = PATH.compareAndExchangeRelease(this, null, ret);
82 return witness == null ? ret : (InstanceIdentifier<T>) witness;