Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jonathan Hart | f04b7d9 | 2016-03-29 09:39:11 -0700 | [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.flowobjective; |
| 18 | |
| 19 | import java.util.function.BiConsumer; |
| 20 | import java.util.function.Consumer; |
| 21 | |
| 22 | /** |
| 23 | * Implementation of objective context that delegates calls to provided |
| 24 | * consumers. |
| 25 | */ |
| 26 | public class DefaultObjectiveContext implements ObjectiveContext { |
| 27 | |
| 28 | private final Consumer<Objective> onSuccess; |
| 29 | private final BiConsumer<Objective, ObjectiveError> onError; |
| 30 | |
| 31 | /** |
| 32 | * Creates a new objective context using the given success and error |
| 33 | * consumers. |
| 34 | * |
| 35 | * @param onSuccess consumer to be called on success |
| 36 | * @param onError consumer to be called on error |
| 37 | */ |
| 38 | public DefaultObjectiveContext(Consumer<Objective> onSuccess, |
| 39 | BiConsumer<Objective, ObjectiveError> onError) { |
| 40 | this.onSuccess = onSuccess; |
| 41 | this.onError = onError; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Creates a new objective context using the given success consumer. |
| 46 | * |
| 47 | * @param onSuccess consumer to be called on success |
| 48 | */ |
| 49 | public DefaultObjectiveContext(Consumer<Objective> onSuccess) { |
| 50 | this(onSuccess, null); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Creates a new objective context using the given error consumer. |
| 55 | * |
| 56 | * @param onError consumer to be called on error |
| 57 | */ |
| 58 | public DefaultObjectiveContext(BiConsumer<Objective, ObjectiveError> onError) { |
| 59 | this(null, onError); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void onSuccess(Objective objective) { |
| 64 | if (onSuccess != null) { |
| 65 | onSuccess.accept(objective); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void onError(Objective objective, ObjectiveError error) { |
| 71 | if (onError != null) { |
| 72 | onError.accept(objective, error); |
| 73 | } |
| 74 | } |
| 75 | } |