org.opendaylight.yangtools.binding.impl.AbstractDataObjectReferenceBuilder Maven / Gradle / Ivy
/*
* Copyright (c) 2024 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.binding.impl;
import static java.util.Objects.requireNonNull;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.binding.Augmentation;
import org.opendaylight.yangtools.binding.ChildOf;
import org.opendaylight.yangtools.binding.ChoiceIn;
import org.opendaylight.yangtools.binding.DataObject;
import org.opendaylight.yangtools.binding.DataObjectReference;
import org.opendaylight.yangtools.binding.DataObjectReference.Builder;
import org.opendaylight.yangtools.binding.DataObjectStep;
import org.opendaylight.yangtools.binding.EntryObject;
import org.opendaylight.yangtools.binding.ExactDataObjectStep;
import org.opendaylight.yangtools.binding.InexactDataObjectStep;
import org.opendaylight.yangtools.binding.Key;
import org.opendaylight.yangtools.binding.KeyStep;
import org.opendaylight.yangtools.binding.NodeStep;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
/**
* Base implementation of {@link Builder}.
*/
public abstract sealed class AbstractDataObjectReferenceBuilder implements Builder
permits AbstractDataObjectIdentifierBuilder, DataObjectReferenceBuilder, DataObjectReferenceBuilderWithKey,
InstanceIdentifier.Builder {
private final ArrayList<@NonNull DataObjectStep>> pathBuilder;
private final Iterable extends @NonNull DataObjectStep>> basePath;
private boolean wildcard;
protected AbstractDataObjectReferenceBuilder(final AbstractDataObjectReferenceBuilder> prev) {
pathBuilder = prev.pathBuilder;
basePath = prev.basePath;
wildcard = prev.wildcard;
}
protected AbstractDataObjectReferenceBuilder(final DataObjectReference base) {
pathBuilder = new ArrayList<>(4);
wildcard = base.isWildcarded();
basePath = base.steps();
}
protected AbstractDataObjectReferenceBuilder(final DataObjectStep> item) {
pathBuilder = new ArrayList<>(4);
basePath = null;
pathBuilder.add(requireNonNull(item));
wildcard = item instanceof InexactDataObjectStep;
}
protected AbstractDataObjectReferenceBuilder(final ExactDataObjectStep> item) {
pathBuilder = new ArrayList<>(4);
basePath = null;
pathBuilder.add(requireNonNull(item));
wildcard = false;
}
@Override
public > Builder augmentation(final Class augmentation) {
return append(new NodeStep<>(augmentation));
}
@Override
public > Builder child(final Class container) {
return append(DataObjectStep.of(container));
}
@Override
public & DataObject, N extends ChildOf super C>> Builder child(
final Class caze, final Class container) {
return append(DataObjectStep.of(caze, container));
}
@Override
public & ChildOf super T>, K extends Key> WithKey child(
final Class listItem, final K listKey) {
return append(new KeyStep<>(listItem, listKey));
}
@Override
public & DataObject, K extends Key,
N extends EntryObject & ChildOf super C>>
WithKey child(final Class caze, final Class listItem, final K listKey) {
return append(new KeyStep<>(listItem, requireNonNull(caze), listKey));
}
@Override
public abstract DataObjectReference build();
protected abstract @NonNull Builder append(@NonNull DataObjectStep step);
protected abstract , Y extends Key> @NonNull WithKey append(
@NonNull KeyStep step);
protected final boolean wildcard() {
return wildcard;
}
protected final void appendItem(final DataObjectStep> item) {
switch (item) {
case ExactDataObjectStep> exact -> appendItem(exact);
case InexactDataObjectStep> inexact -> appendItem(inexact);
}
}
protected final void appendItem(final @NonNull ExactDataObjectStep> item) {
pathBuilder.add(item);
}
// see AbstractDataObjectIdentifierBuilder.appendItem()
protected void appendItem(final @NonNull InexactDataObjectStep> item) {
pathBuilder.add(item);
wildcard = true;
}
protected final @NonNull Iterable extends @NonNull DataObjectStep>> buildSteps() {
final var prefix = basePath;
if (prefix == null) {
return pathBuilder.isEmpty() ? ImmutableList.of() : ImmutableList.copyOf(pathBuilder);
}
return switch (pathBuilder.size()) {
case 0 -> prefix;
case 1 -> new AppendIterable<>(prefix, pathBuilder.getFirst());
default -> ImmutableList.>builder().addAll(prefix).addAll(pathBuilder).build();
};
}
}