Created
June 6, 2018 11:24
-
-
Save iblinov65apps/131171afff85662d4ec1c65089ef7212 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.alexfitness.data.core.dto; | |
import android.support.annotation.Nullable; | |
import org.jetbrains.annotations.Contract; | |
public class TimeoutData<T> { | |
@Nullable | |
private final T data; | |
private final long timeoutMilliseconds; | |
private final long startTime; | |
public TimeoutData(@Nullable T data, long timeoutMilliseconds) { | |
this.data = data; | |
this.timeoutMilliseconds = timeoutMilliseconds; | |
startTime = getCurrentMilliseconds(); | |
} | |
@Contract("null -> false") | |
public static boolean isActual(@Nullable TimeoutData timeoutData) { | |
return timeoutData != null && !timeoutData.isTimedOut() && timeoutData.data != null; | |
} | |
public static <T> TimeoutData<T> createNotActual() { | |
return new TimeoutData<>(null, 0); | |
} | |
public T getData() throws NullPointerException { | |
if (data == null) { | |
throw new NullPointerException("data is null"); | |
} | |
return data; | |
} | |
private boolean isTimedOut() { | |
return getCurrentMilliseconds() > startTime + timeoutMilliseconds; | |
} | |
private long getCurrentMilliseconds() { | |
return System.currentTimeMillis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment