Home | History | Annotate | Download | only in internal
      1 /*
      2  * Copyright (C) 2018 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.internal;
     18 
     19 import java.util.Collection;
     20 import java.util.Collections;
     21 import java.util.HashMap;
     22 import java.util.Map;
     23 
     24 /**
     25  * Represent listeners for a property grouped by their rate.
     26  * T is a type of EventListener such as CarPropertyEventListener
     27  * in {@link android.car.hardware.property.CarPropertyManager}
     28  * @param <T>
     29  * @hide
     30  */
     31 public class CarRatedFloatListeners<T> {
     32     private final Map<T, Float> mListenersToRate = new HashMap<>(4);
     33 
     34     private float mUpdateRate;
     35 
     36     protected long mLastUpdateTime = -1;
     37 
     38     protected CarRatedFloatListeners(float rate) {
     39         mUpdateRate = rate;
     40     }
     41 
     42     /** Check listener */
     43     public boolean contains(T listener) {
     44         return mListenersToRate.containsKey(listener);
     45     }
     46     /** Return current rate after updating */
     47     public float getRate() {
     48         return mUpdateRate;
     49     }
     50 
     51     /**
     52      * Remove given listener from the list and update rate if necessary.
     53      *
     54      * @param listener
     55      * @return true if rate was updated. Otherwise, returns false.
     56      */
     57     public boolean remove(T listener) {
     58         mListenersToRate.remove(listener);
     59         if (mListenersToRate.isEmpty()) {
     60             return false;
     61         }
     62         Float updateRate = Collections.max(mListenersToRate.values());
     63         if (updateRate != mUpdateRate) {
     64             mUpdateRate = updateRate;
     65             return true;
     66         }
     67         return false;
     68     }
     69 
     70     public boolean isEmpty() {
     71         return mListenersToRate.isEmpty();
     72     }
     73 
     74     /**
     75      * Add given listener to the list and update rate if necessary.
     76      *
     77      * @param listener if null, add part is skipped.
     78      * @param updateRate
     79      * @return true if rate was updated. Otherwise, returns false.
     80      */
     81     public boolean addAndUpdateRate(T listener, float updateRate) {
     82         Float oldUpdateRate = mListenersToRate.put(listener, updateRate);
     83         if (mUpdateRate < updateRate) {
     84             mUpdateRate = updateRate;
     85             return true;
     86         } else if (oldUpdateRate != null && oldUpdateRate == mUpdateRate) {
     87             mUpdateRate = Collections.max(mListenersToRate.values());
     88         }
     89         return false;
     90     }
     91 
     92     public Collection<T> getListeners() {
     93         return mListenersToRate.keySet();
     94     }
     95 }
     96 
     97