Add Optional version of projecting Behaviour
Change-Id: I3d3411763be1a75a2bdb04fb59c70d9779f1b02a
diff --git a/apps/optical-model/src/main/java/org/onosproject/net/optical/device/OpticalDeviceServiceView.java b/apps/optical-model/src/main/java/org/onosproject/net/optical/device/OpticalDeviceServiceView.java
index 286ccbc..af14264 100644
--- a/apps/optical-model/src/main/java/org/onosproject/net/optical/device/OpticalDeviceServiceView.java
+++ b/apps/optical-model/src/main/java/org/onosproject/net/optical/device/OpticalDeviceServiceView.java
@@ -73,13 +73,7 @@
= CacheBuilder.newBuilder()
.weakKeys() // == for Key comparison
.maximumSize(100)
- .build(CacheLoader.from(elm -> {
- if (elm.is(OpticalDevice.class)) {
- return Optional.of(elm.as(OpticalDevice.class));
- } else {
- return Optional.empty();
- }
- }));
+ .build(CacheLoader.from(elm -> elm.project(OpticalDevice.class)));
// Not intended to be instantiated directly
protected OpticalDeviceServiceView(DeviceService base) {
diff --git a/core/api/src/main/java/org/onosproject/net/driver/Projectable.java b/core/api/src/main/java/org/onosproject/net/driver/Projectable.java
index 90877a0..3f7139e 100644
--- a/core/api/src/main/java/org/onosproject/net/driver/Projectable.java
+++ b/core/api/src/main/java/org/onosproject/net/driver/Projectable.java
@@ -16,6 +16,8 @@
package org.onosproject.net.driver;
+import java.util.Optional;
+
import com.google.common.annotations.Beta;
/**
@@ -46,4 +48,20 @@
*/
<B extends Behaviour> boolean is(Class<B> projectionClass);
+ /**
+ * Returns the specified projection of this entity if such projection
+ * is supported.
+ *
+ * @param projectionClass requested projection class
+ * @param <B> type of behaviour
+ * @return projection instance
+ */
+ default <B extends Behaviour> Optional<B> project(Class<B> projectionClass) {
+ if (is(projectionClass)) {
+ return Optional.of(as(projectionClass));
+ } else {
+ return Optional.empty();
+ }
+ }
+
}