2 * Copyright (c) 2024 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.yangtools.binding.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.collect.Iterables;
14 import java.io.IOException;
15 import java.io.NotSerializableException;
16 import java.io.ObjectInputStream;
17 import java.io.ObjectOutputStream;
18 import java.io.ObjectStreamException;
19 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.opendaylight.yangtools.binding.DataObject;
23 import org.opendaylight.yangtools.binding.DataObjectReference;
24 import org.opendaylight.yangtools.binding.DataObjectStep;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 * Base implementation of {@link DataObjectReference}.
30 public abstract sealed class AbstractDataObjectReference<T extends DataObject, S extends DataObjectStep<?>>
31 implements DataObjectReference<T>
32 permits DataObjectIdentifierImpl, DataObjectReferenceImpl, InstanceIdentifier {
34 private static final long serialVersionUID = 1L;
36 private final @NonNull Iterable<? extends @NonNull S> steps;
38 protected AbstractDataObjectReference(final Iterable<? extends @NonNull S> steps) {
39 this.steps = requireNonNull(steps);
43 public final Iterable<? extends @NonNull S> steps() {
48 public DataObjectStep<T> lastStep() {
49 return getLast(steps);
53 public final int hashCode() {
55 for (var step : steps) {
56 hash = 31 * hash + step.hashCode();
62 public final boolean equals(final Object obj) {
63 return this == obj || obj instanceof AbstractDataObjectReference other
64 && Iterables.elementsEqual(steps, other.steps);
68 public final String toString() {
69 // FIXME: YANGTOOLS-1577: pretty-print steps instead
70 return MoreObjects.toStringHelper(contract()).add("steps", Iterables.toString(steps())).toString();
73 protected @NonNull Class<?> contract() {
74 return DataObjectReference.class;
77 @SuppressWarnings("unchecked")
78 protected static final <T> @NonNull T getLast(final Iterable<?> steps) {
79 return (@NonNull T) switch (steps) {
80 case AppendIterable<?> append -> append.last();
81 case List<?> list -> list.getLast();
82 default -> Iterables.getLast(steps);
86 protected final void throwNSE() throws NotSerializableException {
87 throw new NotSerializableException(getClass().getName());
91 protected final Object writeReplace() throws ObjectStreamException {
92 return toSerialForm();
95 protected @NonNull Object toSerialForm() {
96 return new ORv1(this);
100 public static final <T> Iterable<? extends T> concat(final Iterable<? extends T> others, final T last) {
101 return new AppendIterable<>(others, last);
105 private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
110 private void readObjectNoData() throws ObjectStreamException {
115 private void writeObject(final ObjectOutputStream stream) throws IOException {