1 /* 2 * Copyright (C) 2021 The Android Open Source Project 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 com.android.car.telemetry.publisher; 18 19 import com.android.car.telemetry.databroker.DataSubscriber; 20 21 import java.util.Collection; 22 import java.util.Collections; 23 import java.util.HashSet; 24 25 /** 26 * Abstract class for publishers. It is 1-1 with data source and manages sending data to 27 * subscribers. Publisher stops itself when there are no subscribers. 28 * 29 * <p>Note that it doesn't map 1-1 to {@link com.android.car.telemetry.TelemetryProto.Publisher} 30 * configuration. Single publisher instance can send data as several 31 * {@link com.android.car.telemetry.TelemetryProto.Publisher} to subscribers. 32 * 33 * <p>Not thread-safe. 34 */ 35 public abstract class AbstractPublisher { 36 private final HashSet<DataSubscriber> mDataSubscribers = new HashSet<>(); 37 38 /** 39 * Adds a subscriber that listens for data produced by this publisher. 40 * 41 * @param subscriber a subscriber to receive data 42 * @throws IllegalArgumentException if an invalid subscriber was provided. 43 */ addDataSubscriber(DataSubscriber subscriber)44 public void addDataSubscriber(DataSubscriber subscriber) { 45 // This method can throw exceptions if data is invalid - do now swap these 2 lines. 46 onDataSubscriberAdded(subscriber); 47 mDataSubscribers.add(subscriber); 48 } 49 50 /** 51 * Removes the subscriber from the publisher. Publisher stops if necessary. 52 * 53 * @throws IllegalArgumentException if the subscriber was not found. 54 */ removeDataSubscriber(DataSubscriber subscriber)55 public void removeDataSubscriber(DataSubscriber subscriber) { 56 if (!mDataSubscribers.remove(subscriber)) { 57 throw new IllegalArgumentException("Failed to remove, subscriber not found"); 58 } 59 onDataSubscribersRemoved(Collections.singletonList(subscriber)); 60 } 61 62 /** 63 * Removes all the subscribers from the publisher. The publisher may stop. 64 */ removeAllDataSubscribers()65 public void removeAllDataSubscribers() { 66 onDataSubscribersRemoved(mDataSubscribers); 67 mDataSubscribers.clear(); 68 } 69 70 /** 71 * Called when a new subscriber is added to the publisher. 72 * 73 * @throws IllegalArgumentException if the invalid subscriber was provided. 74 */ onDataSubscriberAdded(DataSubscriber subscriber)75 protected abstract void onDataSubscriberAdded(DataSubscriber subscriber); 76 77 /** Called when subscribers are removed from the publisher. */ onDataSubscribersRemoved(Collection<DataSubscriber> subscribers)78 protected abstract void onDataSubscribersRemoved(Collection<DataSubscriber> subscribers); 79 getDataSubscribers()80 protected HashSet<DataSubscriber> getDataSubscribers() { 81 return mDataSubscribers; 82 } 83 } 84