mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-21 09:20:19 +01:00
Add sms export library and sample app.
This commit is contained in:
committed by
Cody Henthorne
parent
6120f90dcb
commit
5212b33b47
58
core-util/src/main/java/org/signal/core/util/Result.kt
Normal file
58
core-util/src/main/java/org/signal/core/util/Result.kt
Normal file
@@ -0,0 +1,58 @@
|
||||
package org.signal.core.util
|
||||
|
||||
/**
|
||||
* A Result that allows for generic definitions of success/failure values.
|
||||
*/
|
||||
sealed class Result<out S, out F> {
|
||||
|
||||
data class Failure<out F>(val failure: F) : Result<Nothing, F>()
|
||||
data class Success<out S>(val success: S) : Result<S, Nothing>()
|
||||
|
||||
companion object {
|
||||
fun <S> success(value: S) = Success(value)
|
||||
fun <F> failure(value: F) = Failure(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps an Result<S, F> to an Result<T, F>. Failure values will pass through, while
|
||||
* right values will be operated on by the parameter.
|
||||
*/
|
||||
fun <T> map(onSuccess: (S) -> T): Result<T, F> {
|
||||
return when (this) {
|
||||
is Failure -> this
|
||||
is Success -> success(onSuccess(success))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows the caller to operate on the Result such that the correct function is applied
|
||||
* to the value it contains.
|
||||
*/
|
||||
fun <T> either(
|
||||
onSuccess: (S) -> T,
|
||||
onFailure: (F) -> T
|
||||
): T {
|
||||
return when (this) {
|
||||
is Success -> onSuccess(success)
|
||||
is Failure -> onFailure(failure)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps an Result<L, R> to an Result<L, T>. Failure values will pass through, while
|
||||
* right values will be operated on by the parameter.
|
||||
*
|
||||
* Note this is an extension method in order to make the generics happy.
|
||||
*/
|
||||
fun <T, S, F> Result<S, F>.flatMap(onSuccess: (S) -> Result<T, F>): Result<T, F> {
|
||||
return when (this) {
|
||||
is Result.Success -> onSuccess(success)
|
||||
is Result.Failure -> this
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Try is a specialization of Result where the Failure is fixed to Throwable.
|
||||
*/
|
||||
typealias Try<S> = Result<S, Throwable>
|
||||
58
core-util/src/main/java/org/signal/core/util/Stopwatch.java
Normal file
58
core-util/src/main/java/org/signal/core/util/Stopwatch.java
Normal file
@@ -0,0 +1,58 @@
|
||||
package org.signal.core.util;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class Stopwatch {
|
||||
|
||||
private final long startTime;
|
||||
private final String title;
|
||||
private final List<Split> splits;
|
||||
|
||||
public Stopwatch(@NonNull String title) {
|
||||
this.startTime = System.currentTimeMillis();
|
||||
this.title = title;
|
||||
this.splits = new LinkedList<>();
|
||||
}
|
||||
|
||||
public void split(@NonNull String label) {
|
||||
splits.add(new Split(System.currentTimeMillis(), label));
|
||||
}
|
||||
|
||||
public void stop(@NonNull String tag) {
|
||||
StringBuilder out = new StringBuilder();
|
||||
out.append("[").append(title).append("] ");
|
||||
|
||||
if (splits.size() > 0) {
|
||||
out.append(splits.get(0).label).append(": ");
|
||||
out.append(splits.get(0).time - startTime);
|
||||
out.append(" ");
|
||||
}
|
||||
|
||||
if (splits.size() > 1) {
|
||||
for (int i = 1; i < splits.size(); i++) {
|
||||
out.append(splits.get(i).label).append(": ");
|
||||
out.append(splits.get(i).time - splits.get(i - 1).time);
|
||||
out.append(" ");
|
||||
}
|
||||
|
||||
out.append("total: ").append(splits.get(splits.size() - 1).time - startTime);
|
||||
}
|
||||
|
||||
Log.d(tag, out.toString());
|
||||
}
|
||||
|
||||
private static class Split {
|
||||
final long time;
|
||||
final String label;
|
||||
|
||||
Split(long time, String label) {
|
||||
this.time = time;
|
||||
this.label = label;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user