helenyrwu | a1c4115 | 2016-08-18 16:16:14 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.store.service; |
| 18 | |
| 19 | import com.google.common.collect.Sets; |
| 20 | |
| 21 | import java.util.Set; |
| 22 | import java.util.concurrent.CompletableFuture; |
| 23 | import java.util.function.Consumer; |
| 24 | |
| 25 | /** |
| 26 | * Test implementation of topic. |
| 27 | */ |
| 28 | public class TestTopic<T> implements Topic<T> { |
| 29 | private final String name; |
| 30 | private final Set<Consumer<T>> callbacks = Sets.newConcurrentHashSet(); |
| 31 | |
| 32 | public TestTopic(String name) { |
| 33 | this.name = name; |
| 34 | } |
| 35 | |
| 36 | @Override |
| 37 | public CompletableFuture<Void> publish(T message) { |
| 38 | callbacks.forEach(c -> c.accept(message)); |
| 39 | return CompletableFuture.completedFuture(null); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public CompletableFuture<Void> subscribe(Consumer<T> callback) { |
| 44 | callbacks.add(callback); |
| 45 | return CompletableFuture.completedFuture(null); |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public CompletableFuture<Void> unsubscribe(Consumer<T> callback) { |
| 50 | callbacks.remove(callback); |
| 51 | return CompletableFuture.completedFuture(null); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public String name() { |
| 56 | return name; |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Type primitiveType() { |
| 61 | return Type.TOPIC; |
| 62 | } |
| 63 | } |