*/
package org.opendaylight.yangtools.binding;
+import com.google.common.collect.ImmutableList;
+import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.binding.impl.DataObjectIdentifierImpl;
+import org.opendaylight.yangtools.binding.impl.DataObjectIdentifierWithKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.KeyedBuilder;
/**
*
* @param <T> type of {@link DataObject} held in the last step.
*/
-// FIXME: YANGTOOLS-1577: seal this type
-public non-sealed interface DataObjectIdentifier<T extends DataObject>
- extends DataObjectReference<T>, BindingInstanceIdentifier {
+public sealed interface DataObjectIdentifier<T extends DataObject>
+ extends DataObjectReference<T>, BindingInstanceIdentifier
+ permits DataObjectIdentifier.WithKey, DataObjectIdentifierImpl {
/**
* A {@link DataObjectIdentifier} pointing to a {@link KeyAware} {@link DataObject}, typically a map entry.
*
* @param <K> Key type
* @param <T> KeyAware type
*/
- non-sealed interface WithKey<T extends KeyAware<K> & DataObject, K extends Key<T>>
- extends DataObjectIdentifier<T>, DataObjectReference.WithKey<T, K> {
+ sealed interface WithKey<T extends KeyAware<K> & DataObject, K extends Key<T>>
+ extends DataObjectIdentifier<T>, DataObjectReference.WithKey<T, K>
+ permits DataObjectIdentifierWithKey {
@Override
KeyedBuilder<T, K> toBuilder();
}
+ static @NonNull DataObjectIdentifier<?> ofUnsafeSteps(
+ final Iterable<? extends @NonNull ExactDataObjectStep<?>> steps) {
+ return ofUnsafeSteps(ImmutableList.copyOf(steps));
+ }
+
+ static @NonNull DataObjectIdentifier<?> ofUnsafeSteps(
+ final List<? extends @NonNull ExactDataObjectStep<?>> steps) {
+ return ofUnsafeSteps(ImmutableList.copyOf(steps));
+ }
+
+ static @NonNull DataObjectIdentifier<?> ofUnsafeSteps(
+ final ImmutableList<? extends @NonNull ExactDataObjectStep<?>> steps) {
+ // FIXME: YANGTOOLS-1577: DataObjectReferenceImpl.ofUnsafeSteps() instead
+ return DataObjectIdentifierImpl.ofUnsafeSteps(steps);
+ }
+
@Override
Iterable<? extends @NonNull ExactDataObjectStep<?>> steps();
*/
package org.opendaylight.yangtools.binding;
+import com.google.common.collect.ImmutableList;
import java.io.Serializable;
+import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.binding.impl.AbstractDataObjectReference;
+import org.opendaylight.yangtools.binding.impl.DataObjectReferenceImpl;
+import org.opendaylight.yangtools.binding.impl.DataObjectReferenceWithKey;
import org.opendaylight.yangtools.concepts.Immutable;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Builder;
//* {@link InexactDataObjectStep#matches(DataObjectStep)}.
//* </ul>
public sealed interface DataObjectReference<T extends DataObject> extends Immutable, Serializable
- permits DataObjectIdentifier, DataObjectReference.WithKey, InstanceIdentifier {
+ permits DataObjectIdentifier, DataObjectReference.WithKey, AbstractDataObjectReference {
/**
* A {@link DataObjectReference} pointing to a {@link KeyAware} {@link DataObject}, typically a map entry.
*
*/
sealed interface WithKey<T extends KeyAware<K> & DataObject, K extends Key<T>>
extends DataObjectReference<T>, KeyAware<K>
- permits DataObjectIdentifier.WithKey, KeyedInstanceIdentifier {
+ permits DataObjectIdentifier.WithKey, DataObjectReferenceWithKey, KeyedInstanceIdentifier {
@Override
KeyedBuilder<T, K> toBuilder();
}
}
+ static @NonNull DataObjectReference<?> ofUnsafeSteps(final Iterable<? extends @NonNull DataObjectStep<?>> steps) {
+ return ofUnsafeSteps(ImmutableList.copyOf(steps));
+ }
+
+ static @NonNull DataObjectReference<?> ofUnsafeSteps(final List<? extends @NonNull DataObjectStep<?>> steps) {
+ return ofUnsafeSteps(ImmutableList.copyOf(steps));
+ }
+
+ static @NonNull DataObjectReference<?> ofUnsafeSteps(
+ final ImmutableList<? extends @NonNull DataObjectStep<?>> steps) {
+ return DataObjectReferenceImpl.ofUnsafeSteps(steps);
+ }
+
/**
* Return the steps of this reference. Returned {@link Iterable} does not support removals and contains one or more
* non-null items.
* @return A builder instance
* @deprecated Use {@link #toBuilder()} instead.
*/
- @Deprecated(since = "14.0.0", forRemoval = true)
+ @Deprecated(since = "14.0.0")
default @NonNull Builder<T> builder() {
return toBuilder();
}
--- /dev/null
+/*
+ * 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 com.google.common.base.MoreObjects;
+import com.google.common.base.MoreObjects.ToStringHelper;
+import java.io.IOException;
+import java.io.NotSerializableException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.binding.DataObject;
+import org.opendaylight.yangtools.binding.DataObjectReference;
+import org.opendaylight.yangtools.binding.DataObjectStep;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Base implementation of {@link DataObjectReference}.
+ */
+public abstract sealed class AbstractDataObjectReference<T extends DataObject, S extends DataObjectStep<?>>
+ implements DataObjectReference<T>
+ permits DataObjectIdentifierImpl, DataObjectReferenceImpl, InstanceIdentifier {
+ @java.io.Serial
+ private static final long serialVersionUID = 1L;
+
+ // FIXME: YANGTOOLS-1577: final
+ @Override
+ public abstract Iterable<? extends @NonNull S> steps();
+
+ // FIXME: YANGTOOLS-1577: final
+ @Override
+ public abstract int hashCode();
+
+ // FIXME: YANGTOOLS-1577: final
+ @Override
+ public abstract boolean equals(Object obj);
+
+ @Override
+ public final String toString() {
+ // FIXME: YANGTOOLS-1577: pretty-print steps instead
+ return addToStringAttributes(MoreObjects.toStringHelper(contract())).toString();
+ }
+
+ protected @NonNull Class<?> contract() {
+ return DataObjectReference.class;
+ }
+
+ /**
+ * Add class-specific toString attributes.
+ *
+ * @param toStringHelper ToStringHelper instance
+ * @return ToStringHelper instance which was passed in
+ */
+ protected abstract ToStringHelper addToStringAttributes(ToStringHelper toStringHelper);
+
+ protected final void throwNSE() throws NotSerializableException {
+ throw new NotSerializableException(getClass().getName());
+ }
+
+ @java.io.Serial
+ protected final Object writeReplace() throws ObjectStreamException {
+ return toSerialForm();
+ }
+
+ protected @NonNull Object toSerialForm() {
+ return new ORv1(this);
+ }
+
+ @java.io.Serial
+ private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ throwNSE();
+ }
+
+ @java.io.Serial
+ private void readObjectNoData() throws ObjectStreamException {
+ throwNSE();
+ }
+
+ @java.io.Serial
+ private void writeObject(final ObjectOutputStream stream) throws IOException {
+ throwNSE();
+ }
+}
--- /dev/null
+/*
+ * 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 com.google.common.collect.ImmutableList;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.binding.DataObject;
+import org.opendaylight.yangtools.binding.DataObjectIdentifier;
+import org.opendaylight.yangtools.binding.ExactDataObjectStep;
+
+// FIXME: YANGTOOLS-1577: non-abstract
+public abstract sealed class DataObjectIdentifierImpl<T extends DataObject>
+ extends AbstractDataObjectReference<T, ExactDataObjectStep<?>> implements DataObjectIdentifier<T>
+ permits DataObjectIdentifierWithKey {
+ @java.io.Serial
+ private static final long serialVersionUID = 1L;
+
+ public static final @NonNull DataObjectIdentifierImpl<?> ofUnsafeSteps(
+ final ImmutableList<? extends @NonNull ExactDataObjectStep<?>> steps) {
+ // FIXME: YANGTOOLS-1577: implement this
+ throw new UnsupportedOperationException();
+ }
+
+ @java.io.Serial
+ private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ throwNSE();
+ }
+
+ @java.io.Serial
+ private void readObjectNoData() throws ObjectStreamException {
+ throwNSE();
+ }
+
+ @java.io.Serial
+ private void writeObject(final ObjectOutputStream stream) throws IOException {
+ throwNSE();
+ }
+}
--- /dev/null
+/*
+ * 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 java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.binding.DataObject;
+import org.opendaylight.yangtools.binding.DataObjectIdentifier.WithKey;
+import org.opendaylight.yangtools.binding.Key;
+import org.opendaylight.yangtools.binding.KeyAware;
+
+// FIXME: YANGTOOLS-1577: final
+public abstract non-sealed class DataObjectIdentifierWithKey<T extends KeyAware<K> & DataObject, K extends Key<T>>
+ extends DataObjectIdentifierImpl<T> implements WithKey<T, K> {
+ @java.io.Serial
+ private static final long serialVersionUID = 1L;
+
+ private final @NonNull K key;
+
+ protected DataObjectIdentifierWithKey(final K key) {
+ this.key = requireNonNull(key);
+ }
+
+ @Override
+ public final K key() {
+ return key;
+ }
+
+ @java.io.Serial
+ private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ throwNSE();
+ }
+
+ @java.io.Serial
+ private void readObjectNoData() throws ObjectStreamException {
+ throwNSE();
+ }
+
+ @java.io.Serial
+ private void writeObject(final ObjectOutputStream stream) throws IOException {
+ throwNSE();
+ }
+}
--- /dev/null
+/*
+ * 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 com.google.common.collect.ImmutableList;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.binding.DataObject;
+import org.opendaylight.yangtools.binding.DataObjectReference;
+import org.opendaylight.yangtools.binding.DataObjectStep;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+// FIXME: YANGTOOLS-1577: non-abstract
+public abstract sealed class DataObjectReferenceImpl<T extends DataObject>
+ extends AbstractDataObjectReference<T, DataObjectStep<?>>
+ permits DataObjectReferenceWithKey {
+ @java.io.Serial
+ private static final long serialVersionUID = 1L;
+
+ public static final @NonNull DataObjectReference<?> ofUnsafeSteps(
+ final ImmutableList<? extends @NonNull DataObjectStep<?>> steps) {
+ // FIXNE: YANGTOOLS-1577: dispatch to this class
+ return InstanceIdentifier.unsafeOf(steps);
+ }
+
+ @java.io.Serial
+ private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ throwNSE();
+ }
+
+ @java.io.Serial
+ private void readObjectNoData() throws ObjectStreamException {
+ throwNSE();
+ }
+
+ @java.io.Serial
+ private void writeObject(final ObjectOutputStream stream) throws IOException {
+ throwNSE();
+ }
+}
--- /dev/null
+/*
+ * 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 java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ObjectStreamException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.binding.DataObject;
+import org.opendaylight.yangtools.binding.DataObjectReference.WithKey;
+import org.opendaylight.yangtools.binding.Key;
+import org.opendaylight.yangtools.binding.KeyAware;
+
+// FIXME: YANGTOOLS-1577: final
+public abstract non-sealed class DataObjectReferenceWithKey<T extends KeyAware<K> & DataObject, K extends Key<T>>
+ extends DataObjectReferenceImpl<T> implements WithKey<T, K> {
+ @java.io.Serial
+ private static final long serialVersionUID = 1L;
+
+ private final @NonNull K key;
+
+ protected DataObjectReferenceWithKey(final K key) {
+ this.key = requireNonNull(key);
+ }
+
+ @Override
+ public final K key() {
+ return key;
+ }
+
+ @java.io.Serial
+ private void readObject(final ObjectInputStream stream) throws IOException, ClassNotFoundException {
+ throwNSE();
+ }
+
+ @java.io.Serial
+ private void readObjectNoData() throws ObjectStreamException {
+ throwNSE();
+ }
+
+ @java.io.Serial
+ private void writeObject(final ObjectOutputStream stream) throws IOException {
+ throwNSE();
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2018 Pantheon Technologies, 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 com.google.common.collect.ImmutableList;
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.io.ObjectStreamException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.binding.DataObjectReference;
+import org.opendaylight.yangtools.binding.DataObjectStep;
+
+final class ORv1 implements Externalizable {
+ @java.io.Serial
+ private static final long serialVersionUID = 1L;
+
+ private ImmutableList<@NonNull DataObjectStep<?>> steps;
+
+ @SuppressWarnings("redundantModifier")
+ public ORv1() {
+ // For Externalizable
+ }
+
+ ORv1(final DataObjectReference<?> source) {
+ steps = ImmutableList.copyOf(source.steps());
+ }
+
+ @Override
+ public void writeExternal(final ObjectOutput out) throws IOException {
+ out.writeInt(steps.size());
+ for (var step : steps) {
+ out.writeObject(step);
+ }
+ }
+
+ @Override
+ public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
+ final int size = in.readInt();
+ final var builder = ImmutableList.<DataObjectStep<?>>builderWithExpectedSize(size);
+ for (int i = 0; i < size; ++i) {
+ builder.add((DataObjectStep<?>) in.readObject());
+ }
+ steps = builder.build();
+ }
+
+ @java.io.Serial
+ private Object readResolve() throws ObjectStreamException {
+ return DataObjectReferenceImpl.ofUnsafeSteps(steps);
+ }
+}
--- /dev/null
+/*
+ * 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
+ */
+/**
+ * Internal implementation details. This package exists only for the use with
+ * {@link org.opendaylight.yangtools.yang.binding.InstanceIdentifier}, so the dance we are doing with class hierarchy
+ * is not (entirely) visible.
+ */
+package org.opendaylight.yangtools.binding.impl;
\ No newline at end of file
import static java.util.Objects.requireNonNull;
import com.google.common.annotations.Beta;
-import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.VerifyException;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import java.io.IOException;
-import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
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.DataObjectStep;
import org.opendaylight.yangtools.binding.DataRoot;
import org.opendaylight.yangtools.binding.ExactDataObjectStep;
import org.opendaylight.yangtools.binding.KeyStep;
import org.opendaylight.yangtools.binding.KeylessStep;
import org.opendaylight.yangtools.binding.NodeStep;
+import org.opendaylight.yangtools.binding.impl.AbstractDataObjectReference;
import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
import org.opendaylight.yangtools.util.HashCodeBuilder;
* <p>
* This would be the same as using a path like so, "/nodes/node/openflow:1" to refer to the openflow:1 node
*/
-public sealed class InstanceIdentifier<T extends DataObject>
- implements DataObjectReference<T>, HierarchicalIdentifier<InstanceIdentifier<? extends DataObject>>
+public sealed class InstanceIdentifier<T extends DataObject> extends AbstractDataObjectReference<T, DataObjectStep<?>>
+ implements HierarchicalIdentifier<InstanceIdentifier<? extends DataObject>>
permits KeyedInstanceIdentifier {
@java.io.Serial
private static final long serialVersionUID = 3L;
}
@Override
- public final String toString() {
- return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
+ protected final Class<?> contract() {
+ return getClass();
}
- /**
- * Add class-specific toString attributes.
- *
- * @param toStringHelper ToStringHelper instance
- * @return ToStringHelper instance which was passed in
- */
+ @Override
protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
return toStringHelper.add("targetType", targetType).add("path", Iterables.toString(pathArguments));
}
return childIdentifier(new NodeStep<>(container));
}
- @java.io.Serial
- Object writeReplace() throws ObjectStreamException {
+ @Override
+ protected Object toSerialForm() {
return new IIv4<>(this);
}
throwNSE();
}
- private void throwNSE() throws NotSerializableException {
- throw new NotSerializableException(getClass().getName());
- }
-
@Override
public Builder<T> toBuilder() {
return new RegularBuilder<>(this);
*/
package org.opendaylight.yangtools.yang.binding;
-import java.io.ObjectStreamException;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.binding.DataObject;
import org.opendaylight.yangtools.binding.DataObjectReference.WithKey;
}
@Override
- Object writeReplace() throws ObjectStreamException {
+ protected Object toSerialForm() {
return new KIIv4<>(this);
}
}