diff options
author | Treehugger Robot <treehugger-gerrit@google.com> | 2017-11-18 09:16:18 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-11-18 09:16:18 +0000 |
commit | 3b450ceb4290364df58a541c60cc690cc36ba4c8 (patch) | |
tree | c9bb61716fc202d23c4966a1d2407cfa09744791 /java/com/android/dialer/common/backoff/ExponentialBackoff.java | |
parent | a8b9f7de79559caef0aaab7dc05f585314024790 (diff) | |
parent | 7f1076d08b39467ac8c550f60a7fbbad604ab4fa (diff) |
Merge changes Iee4e3db8,I6e74c7fe,Ibe722477,Ia22751f0,Ic28fb197, ...
* changes:
DialpadView cleanup.
Merge the following methods in InCallActivityCommon into InCallActivity:
Add contact source options.
Automated rollback of changelist 174944384
Refactoring and adding TOS check before transcribing
Adding exponential backoff utilities
Implement suggested SIM
Fix dialer simulator for conference calling funcitonality. Updated the following contents: 1.Fix the order of spawning connections for GSM conference. 2.Make VOLTE conference call more realistic. 3.Fix minor bugs about simulator. 4.Add SimulatorConnectionsBank class to store connection tags created by simulator. 5.Fix tests influenced by SimulatorConnectionsBank.
Diffstat (limited to 'java/com/android/dialer/common/backoff/ExponentialBackoff.java')
-rw-r--r-- | java/com/android/dialer/common/backoff/ExponentialBackoff.java | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/java/com/android/dialer/common/backoff/ExponentialBackoff.java b/java/com/android/dialer/common/backoff/ExponentialBackoff.java new file mode 100644 index 000000000..67727d8c2 --- /dev/null +++ b/java/com/android/dialer/common/backoff/ExponentialBackoff.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dialer.common.backoff; + +import com.android.dialer.common.Assert; + +/** + * Given an initial backoff delay, D, a base multiplier, B, and a total number of backoffs, N, this + * class returns values in the exponential sequence, D, D*B, D*B^2, ... D*B^(N-1), ... + * + * <p>Example usage: + * + * <pre> + * long initialDelayMillis = 1000; + * double multiplier = 1.2; + * int backoffs = 10; + * ExponentialBackoff backoff = new ExponentialBackoff(initialDelayMillis, multiplier, backoffs); + * while (backoff.isInRange()) { + * ... + * sleep(backoff.getNextBackoff()); + * } + * </pre> + * + * <p>Note: the base multiplier can be calculated using {@code ExponentialBaseCalculator} + */ +public final class ExponentialBackoff { + public final long initialDelayMillis; + public final double baseMultiplier; + public final int maximumBackoffs; + private double nextBackoff; + private int backoffCount; + + /** + * Setup an exponential backoff with an initial delay, a base multiplier and a maximum number of + * backoff steps. + * + * @throws IllegalArgumentException for negative argument values + */ + public ExponentialBackoff(long initialDelayMillis, double baseMultiplier, int maximumBackoffs) { + Assert.checkArgument(initialDelayMillis > 0); + Assert.checkArgument(baseMultiplier > 0); + Assert.checkArgument(maximumBackoffs > 0); + this.initialDelayMillis = initialDelayMillis; + this.baseMultiplier = baseMultiplier; + this.maximumBackoffs = maximumBackoffs; + reset(); + } + + /** + * @return the next backoff time in the exponential sequence. Specifically, if D is the initial + * delay, B is the base multiplier and N is the total number of backoffs, then the return + * values will be: D, D*B, D*B^2, ... D*B^(N-1), ... + */ + public long getNextBackoff() { + long backoff = Math.round(nextBackoff); + backoffCount++; + nextBackoff *= baseMultiplier; + return backoff; + } + + /** @return the number of times getNextBackoff() has been called */ + public int getBackoffCount() { + return backoffCount; + } + + /** + * @return {@code true} if getNextBackoff() has been called less than the maximumBackoffs value + * specified in the constructor. + */ + public boolean isInRange() { + return backoffCount < maximumBackoffs; + } + + /** + * Reset the sequence of backoff values so the next call to getNextBackoff() will return the + * initial delay and getBackoffCount() will return 0 + */ + public void reset() { + nextBackoff = initialDelayMillis; + backoffCount = 0; + } +} |