blob: 7f99e7a0460fbf4f2922830aa0816ab6909edebe [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
tomd7356722014-08-26 01:07:39 -070016package org.onlab.onos.event;
17
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");
39 checkArgument(!sinks.containsKey(eventClass),
40 "Event sink already registered for %s", eventClass.getName());
41 sinks.put(eventClass, sink);
42 }
43
44 @Override
45 public <E extends Event> void removeSink(Class<E> eventClass) {
46 checkNotNull(eventClass, "Event class cannot be null");
47 checkArgument(sinks.remove(eventClass) != null,
48 "Event sink not registered for %s", eventClass.getName());
49 }
50
51 @Override
52 @SuppressWarnings("unchecked")
53 public <E extends Event> EventSink<E> getSink(Class<E> eventClass) {
tomb36046e2014-08-27 00:22:24 -070054 // TODO: add implicit registration of descendant classes
tomd7356722014-08-26 01:07:39 -070055 return (EventSink<E>) sinks.get(eventClass);
56 }
57
58 @Override
59 public Set<Class<? extends Event>> getSinks() {
60 return ImmutableSet.copyOf(sinks.keySet());
61 }
62}