blob: d934a3b952d17ccb40b318431c378891c2be2479 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.event;
tomd7356722014-08-26 01:07:39 -070017
18import com.google.common.collect.ImmutableSet;
19
20import java.util.Map;
21import java.util.Set;
22import java.util.concurrent.ConcurrentHashMap;
23
24import static com.google.common.base.Preconditions.checkArgument;
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
tom96dfcab2014-08-28 09:26:03 -070028 * Base implementation of event sink registry.
tomd7356722014-08-26 01:07:39 -070029 */
tom96dfcab2014-08-28 09:26:03 -070030public class DefaultEventSinkRegistry implements EventSinkRegistry {
tomd7356722014-08-26 01:07:39 -070031
tom96dfcab2014-08-28 09:26:03 -070032 private final Map<Class<? extends Event>, EventSink<? extends Event>>
33 sinks = new ConcurrentHashMap<>();
tomd7356722014-08-26 01:07:39 -070034
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");
tomd7356722014-08-26 01:07:39 -070039 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 Vachuskab17c41f2015-05-19 11:16:05 -070052 checkNotNull(eventClass, "Event class cannot be null");
tomd7356722014-08-26 01:07:39 -070053 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}