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.event; |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | |
| 20 | import java.util.Map; |
| 21 | import java.util.Set; |
| 22 | import java.util.concurrent.ConcurrentHashMap; |
| 23 | |
| 24 | import static com.google.common.base.Preconditions.checkArgument; |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 28 | * Base implementation of event sink registry. |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 29 | */ |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 30 | public class DefaultEventSinkRegistry implements EventSinkRegistry { |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 31 | |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 32 | private final Map<Class<? extends Event>, EventSink<? extends Event>> |
| 33 | sinks = new ConcurrentHashMap<>(); |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 34 | |
| 35 | @Override |
| 36 | public <E extends Event> void addSink(Class<E> eventClass, EventSink<E> sink) { |
| 37 | checkNotNull(eventClass, "Event class cannot be null"); |
| 38 | checkNotNull(sink, "Event sink cannot be null"); |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 39 | sinks.put(eventClass, sink); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public <E extends Event> void removeSink(Class<E> eventClass) { |
| 44 | checkNotNull(eventClass, "Event class cannot be null"); |
| 45 | checkArgument(sinks.remove(eventClass) != null, |
| 46 | "Event sink not registered for %s", eventClass.getName()); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | @SuppressWarnings("unchecked") |
| 51 | public <E extends Event> EventSink<E> getSink(Class<E> eventClass) { |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 52 | checkNotNull(eventClass, "Event class cannot be null"); |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 53 | return (EventSink<E>) sinks.get(eventClass); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public Set<Class<? extends Event>> getSinks() { |
| 58 | return ImmutableSet.copyOf(sinks.keySet()); |
| 59 | } |
| 60 | } |