blob: 72c05ce251644c44c53c27ba11918316d3776167 [file] [log] [blame]
helenyrwua1c41152016-08-18 16:16:14 -07001/*
2 * Copyright 2016-present 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 */
16
17package org.onosproject.store.service;
18
Madan Jampaniecf99712016-08-22 10:05:20 -070019import com.google.common.collect.Maps;
helenyrwua1c41152016-08-18 16:16:14 -070020
Madan Jampaniecf99712016-08-22 10:05:20 -070021import java.util.Map;
helenyrwua1c41152016-08-18 16:16:14 -070022import java.util.concurrent.CompletableFuture;
Madan Jampaniecf99712016-08-22 10:05:20 -070023import java.util.concurrent.Executor;
helenyrwua1c41152016-08-18 16:16:14 -070024import java.util.function.Consumer;
25
26/**
27 * Test implementation of topic.
28 */
29public class TestTopic<T> implements Topic<T> {
30 private final String name;
Madan Jampaniecf99712016-08-22 10:05:20 -070031 private final Map<Consumer<T>, Executor> callbacks = Maps.newIdentityHashMap();
helenyrwua1c41152016-08-18 16:16:14 -070032
33 public TestTopic(String name) {
34 this.name = name;
35 }
36
37 @Override
38 public CompletableFuture<Void> publish(T message) {
Madan Jampaniecf99712016-08-22 10:05:20 -070039 callbacks.forEach((k, v) -> {
40 v.execute(() -> {
41 k.accept(message);
42 });
43 });
helenyrwua1c41152016-08-18 16:16:14 -070044 return CompletableFuture.completedFuture(null);
45 }
46
47 @Override
Madan Jampaniecf99712016-08-22 10:05:20 -070048 public CompletableFuture<Void> subscribe(Consumer<T> callback, Executor executor) {
49 callbacks.put(callback, executor);
helenyrwua1c41152016-08-18 16:16:14 -070050 return CompletableFuture.completedFuture(null);
51 }
52
53 @Override
54 public CompletableFuture<Void> unsubscribe(Consumer<T> callback) {
55 callbacks.remove(callback);
56 return CompletableFuture.completedFuture(null);
57 }
58
59 @Override
60 public String name() {
61 return name;
62 }
63
64 @Override
65 public Type primitiveType() {
66 return Type.TOPIC;
67 }
68}