Skip to content

Instantly share code, notes, and snippets.

@iblinov65apps
Created June 6, 2018 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iblinov65apps/131171afff85662d4ec1c65089ef7212 to your computer and use it in GitHub Desktop.
Save iblinov65apps/131171afff85662d4ec1c65089ef7212 to your computer and use it in GitHub Desktop.
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