org.opendaylight.yangtools.concepts.ExtensibleObject Maven / Gradle / Ivy
/*
* Copyright (c) 2019 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.concepts;
import static java.util.Objects.requireNonNull;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
/**
* Interface specifying access to extensions attached to a particular object. This functionality is loosely based on
* Extensible Object
* pattern.
*
* @param Type of extensible object
* @param Extension marker interface
*/
public interface ExtensibleObject, E extends ObjectExtension> {
/**
* Return an extension interface, if currently available.
*
* @implSpec
* Default implementation defers to linear search of {@link #supportedExtensions()}.
*
* @param Extension type
* @param type Extension type class
* @return An extension instance, or {@code null}
* @throws NullPointerException if {@code type} is {@code null}
*/
default @Nullable T extension(final Class type) {
final var nonnull = requireNonNull(type);
return supportedExtensions().stream().filter(nonnull::isInstance).findFirst().map(nonnull::cast).orElse(null);
}
default Optional findExtension(final Class type) {
return Optional.ofNullable(extension(type));
}
/**
* Return currently-supported extensions. Note that the returned collection may change if this object is mutable.
*
* @implSpec
* Default implementations returns an empty List.
*
* @return Supported extensions
*/
default @NonNull Collection extends E> supportedExtensions() {
return List.of();
}
}