helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -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 | */ |
| 16 | |
| 17 | package org.onosproject.store.service; |
| 18 | |
Madan Jampani | ecf9971 | 2016-08-22 10:05:20 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 20 | |
Madan Jampani | ecf9971 | 2016-08-22 10:05:20 -0700 | [diff] [blame] | 21 | import java.util.Map; |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 22 | import java.util.concurrent.CompletableFuture; |
Madan Jampani | ecf9971 | 2016-08-22 10:05:20 -0700 | [diff] [blame] | 23 | import java.util.concurrent.Executor; |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 24 | import java.util.function.Consumer; |
| 25 | |
| 26 | /** |
| 27 | * Test implementation of topic. |
| 28 | */ |
| 29 | public class TestTopic<T> implements Topic<T> { |
| 30 | private final String name; |
Madan Jampani | ecf9971 | 2016-08-22 10:05:20 -0700 | [diff] [blame] | 31 | private final Map<Consumer<T>, Executor> callbacks = Maps.newIdentityHashMap(); |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 32 | |
| 33 | public TestTopic(String name) { |
| 34 | this.name = name; |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public CompletableFuture<Void> publish(T message) { |
Madan Jampani | ecf9971 | 2016-08-22 10:05:20 -0700 | [diff] [blame] | 39 | callbacks.forEach((k, v) -> { |
| 40 | v.execute(() -> { |
| 41 | k.accept(message); |
| 42 | }); |
| 43 | }); |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 44 | return CompletableFuture.completedFuture(null); |
| 45 | } |
| 46 | |
| 47 | @Override |
Madan Jampani | ecf9971 | 2016-08-22 10:05:20 -0700 | [diff] [blame] | 48 | public CompletableFuture<Void> subscribe(Consumer<T> callback, Executor executor) { |
| 49 | callbacks.put(callback, executor); |
helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 50 | 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 | } |