Add Optional version of projecting Behaviour

Change-Id: I3d3411763be1a75a2bdb04fb59c70d9779f1b02a
This commit is contained in:
Yuta HIGUCHI 2017-10-27 18:14:29 -07:00 committed by Thomas Vachuska
parent 611a065631
commit 4bec2f3923
2 changed files with 19 additions and 7 deletions

View File

@ -73,13 +73,7 @@ public class OpticalDeviceServiceView
= 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) {

View File

@ -16,6 +16,8 @@
package org.onosproject.net.driver;
import java.util.Optional;
import com.google.common.annotations.Beta;
/**
@ -46,4 +48,20 @@ public interface Projectable {
*/
<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();
}
}
}