Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.provider; |
tom | 64b7aac | 2014-08-26 00:18:21 -0700 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkState; |
| 19 | |
| 20 | /** |
| 21 | * Base implementation of a provider service, which tracks the provider to |
| 22 | * which it is issued and can be invalidated. |
| 23 | * |
| 24 | * @param <P> type of the information provider |
| 25 | */ |
| 26 | public abstract class AbstractProviderService<P extends Provider> implements ProviderService<P> { |
| 27 | |
| 28 | private boolean isValid = true; |
| 29 | private final P provider; |
| 30 | |
| 31 | /** |
| 32 | * Creates a provider service on behalf of the specified provider. |
| 33 | * |
| 34 | * @param provider provider to which this service is being issued |
| 35 | */ |
| 36 | protected AbstractProviderService(P provider) { |
| 37 | this.provider = provider; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Invalidates this provider service. |
| 42 | */ |
| 43 | public void invalidate() { |
| 44 | isValid = false; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Checks the validity of this provider service. |
| 49 | * |
| 50 | * @throws java.lang.IllegalStateException if the service is no longer valid |
| 51 | */ |
| 52 | public void checkValidity() { |
| 53 | checkState(isValid, "Provider service is no longer valid"); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public P provider() { |
| 58 | return provider; |
| 59 | } |
| 60 | |
| 61 | } |