/* * 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.yangtools.yang.model.api; import static java.util.Objects.requireNonNull; import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; import java.util.AbstractList; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Deque; import java.util.Iterator; import java.util.Spliterator; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.yangtools.concepts.Immutable; import org.opendaylight.yangtools.yang.common.QName; @Deprecated(since = "7.0.9", forRemoval = true) final class PathFromRoot extends AbstractList implements Immutable { private static final QName[] EMPTY_QNAMES = new QName[0]; private static final VarHandle QNAMES; static { try { QNAMES = MethodHandles.lookup().findVarHandle(PathFromRoot.class, "qnames", QName[].class); } catch (NoSuchFieldException | IllegalAccessException e) { throw new ExceptionInInitializerError(e); } } private final SchemaPath path; @SuppressWarnings("unused") private QName @Nullable [] qnames; PathFromRoot(final SchemaPath path) { this.path = requireNonNull(path); } @Override public Iterator iterator() { return Arrays.asList(qnames()).iterator(); } @Override public Spliterator spliterator() { return Arrays.spliterator(qnames()); } @Override public QName get(final int index) { return qnames()[index]; } @Override public boolean isEmpty() { return false; } @Override public int size() { return qnames().length; } private QName @NonNull [] qnames() { final QName[] local = (QName[]) QNAMES.getAcquire(this); return local != null ? local : loadQNames(); } private QName @NonNull [] loadQNames() { final Deque tmp = new ArrayDeque<>(); for (QName qname : path.getPathTowardsRoot()) { tmp.addFirst(qname); } final QName[] result = tmp.toArray(EMPTY_QNAMES); // We do not care about atomicity here QNAMES.setRelease(this, result); return result; } }