Thomas Vachuska | 7b3fe3a | 2016-02-01 11:26:22 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 7b3fe3a | 2016-02-01 11:26:22 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.net.driver; |
| 18 | |
Yuta HIGUCHI | 4bec2f3 | 2017-10-27 18:14:29 -0700 | [diff] [blame] | 19 | import java.util.Optional; |
| 20 | |
Thomas Vachuska | 7b3fe3a | 2016-02-01 11:26:22 -0800 | [diff] [blame] | 21 | import com.google.common.annotations.Beta; |
| 22 | |
| 23 | /** |
| 24 | * Abstraction of an entity capable of being projected as another entity. |
| 25 | */ |
| 26 | @Beta |
| 27 | public interface Projectable { |
| 28 | |
| 29 | /** |
| 30 | * Returns the specified projection of this entity if such projection |
| 31 | * is supported. |
| 32 | * |
| 33 | * @param projectionClass requested projection class |
| 34 | * @param <B> type of behaviour |
Thomas Vachuska | e3ea9a4 | 2016-02-10 17:54:59 -0800 | [diff] [blame] | 35 | * @return projection instance |
| 36 | * @throws IllegalStateException if a driver cannot be found |
| 37 | * @throws IllegalArgumentException if the projection is not supported |
Thomas Vachuska | 7b3fe3a | 2016-02-01 11:26:22 -0800 | [diff] [blame] | 38 | */ |
| 39 | <B extends Behaviour> B as(Class<B> projectionClass); |
| 40 | |
| 41 | /** |
| 42 | * Returns true if this entity is capable of being projected as the |
| 43 | * specified class. |
| 44 | * |
Thomas Vachuska | 3afbc7f | 2016-02-01 15:55:38 -0800 | [diff] [blame] | 45 | * @param projectionClass requested projection class |
Thomas Vachuska | 7b3fe3a | 2016-02-01 11:26:22 -0800 | [diff] [blame] | 46 | * @param <B> type of behaviour |
| 47 | * @return true if the requested projection is supported |
| 48 | */ |
| 49 | <B extends Behaviour> boolean is(Class<B> projectionClass); |
| 50 | |
Yuta HIGUCHI | 4bec2f3 | 2017-10-27 18:14:29 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Returns the specified projection of this entity if such projection |
| 53 | * is supported. |
| 54 | * |
| 55 | * @param projectionClass requested projection class |
| 56 | * @param <B> type of behaviour |
| 57 | * @return projection instance |
| 58 | */ |
| 59 | default <B extends Behaviour> Optional<B> project(Class<B> projectionClass) { |
| 60 | if (is(projectionClass)) { |
| 61 | return Optional.of(as(projectionClass)); |
| 62 | } else { |
| 63 | return Optional.empty(); |
| 64 | } |
| 65 | } |
| 66 | |
Thomas Vachuska | 7b3fe3a | 2016-02-01 11:26:22 -0800 | [diff] [blame] | 67 | } |