belongs to Maven artifact androidx.car:car:1.0.0-alpha1

ContentRateLimiter

public class ContentRateLimiter
extends Object

java.lang.Object
   ↳ androidx.car.moderator.ContentRateLimiter


A class that keeps track of a general number of permitted actions that happen over time and determines if a subsequent interaction is allowed. The type of interaction is arbitrary and not transparent to this class. Instead, it will refer to these actions as "permits," short for "permitted action." It is up to a user of this class to determine the unit of permits.

This class allows for two quick acquires in succession to only consume one permit. This is intended behavior to account for the fact that the user can be using many taps to scroll quickly. This can fit within the window for which a user does not necessary have their eyes off the road for a long period of time, and thus should not be penalized.

This class allows for the maximum number of permits that can be stored,the amount of permits that are filled each second, as well as the delay before re-fill to be configured.

Summary

Public constructors

ContentRateLimiter(double acquiredPermitsPerSecond, double maxStoredPermits, long fillDelayMs)

Creates a ContentRateLimiter with the given parameters.

Public methods

double getAvailablePermits()

Gets the current number of stored permits ready to be used.

double getMaxStoredPermits()

Gets the max number of permits allowed to be stored for future usage.

void setAvailablePermits(double availablePermits)

Sets the current number of stored permits that are ready to be used.

void setUnlimitedMode(boolean enabled)

Sets unlimited mode.

boolean tryAcquire()

Checks if there are enough available permits for a single permit to be acquired.

boolean tryAcquire(int permits)

Checks whether there are enough available permits to acquire.

Inherited methods

Public constructors

ContentRateLimiter

ContentRateLimiter (double acquiredPermitsPerSecond, 
                double maxStoredPermits, 
                long fillDelayMs)

Creates a ContentRateLimiter with the given parameters.

Parameters
acquiredPermitsPerSecond double: The amount of permits that are acquired each second.

maxStoredPermits double: The maximum number of permits that can be stored.

fillDelayMs long: The amount of time to wait between when a permit is acquired and when the number of available permits start refilling.

Public methods

getAvailablePermits

double getAvailablePermits ()

Gets the current number of stored permits ready to be used.

Returns
double

getMaxStoredPermits

double getMaxStoredPermits ()

Gets the max number of permits allowed to be stored for future usage.

Returns
double

setAvailablePermits

void setAvailablePermits (double availablePermits)

Sets the current number of stored permits that are ready to be used. If this value exceeds the maximum number of stored permits that is passed to the constructor, then the max value is used instead.

Parameters
availablePermits double

setUnlimitedMode

void setUnlimitedMode (boolean enabled)

Sets unlimited mode. If enabled, there is no restriction on the number of permits that can be acquired and any interaction does not consume stored permits.

Parameters
enabled boolean

tryAcquire

boolean tryAcquire ()

Checks if there are enough available permits for a single permit to be acquired.

Returns
boolean true if unlimited mode is enabled or enough permits are acquirable at the time of this call; false if there isn't the number of permits requested available currently.

tryAcquire

boolean tryAcquire (int permits)

Checks whether there are enough available permits to acquire.

Parameters
permits int

Returns
boolean true if unlimited mode is enabled or enough permits are acquirable at the time of this call; false if there isn't the number of permits requested available currently.