private void checkGeneralPreconditions(final String host, final int port, final int timeoutInMs, final int httpResponse, final ErrorHandler errorHandler) { Preconditions.checkNotNullOrEmpty(host, "host is null or empty"); Preconditions.checkGreaterThanZero(port, "port is not a positive number"); Preconditions.checkGreaterThanZero(timeoutInMs, "timeoutInMs is not a positive number"); Preconditions.checkNotNull(errorHandler, "errorHandler is null"); Preconditions.checkNotNull(httpResponse, "httpResponse is null"); Preconditions.checkGreaterThanZero(httpResponse, "httpResponse is not a positive number"); }
private void checkGeneralPreconditions(String host, int port, int timeoutInMs, ErrorHandler errorHandler) { Preconditions.checkNotNullOrEmpty(host, "host is null or empty"); Preconditions.checkGreaterThanZero(port, "port is not a positive number"); Preconditions.checkGreaterThanZero(timeoutInMs, "timeoutInMs is not a positive number"); Preconditions.checkNotNull(errorHandler, "errorHandler is null"); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowAnExceptionWhenValueLowerThanZero() { Preconditions.checkGreaterThanZero(-1, MSG_VALUE_IS_NOT_GREATER_THAN_ZERO); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowAnExceptionWhenValueIsZero() { Preconditions.checkGreaterThanZero(0, MSG_VALUE_IS_NOT_GREATER_THAN_ZERO); }
@Test public void shouldNotThrowAnythingWhenValueIsGreaterThanZero() { Preconditions.checkGreaterThanZero(1, MSG_VALUE_IS_NOT_GREATER_THAN_ZERO); }
@Override public Observable<Boolean> observeInternetConnectivity(final int initialIntervalInMs, final int intervalInMs, final String host, final int port, final int timeoutInMs, final int httpResponse, final ErrorHandler errorHandler) { Preconditions.checkGreaterOrEqualToZero(initialIntervalInMs, "initialIntervalInMs is not a positive number"); Preconditions.checkGreaterThanZero(intervalInMs, "intervalInMs is not a positive number"); checkGeneralPreconditions(host, port, timeoutInMs, errorHandler); final String adjustedHost = adjustHost(host); return Observable.interval(initialIntervalInMs, intervalInMs, TimeUnit.MILLISECONDS, Schedulers.io()).map(new Function<Long, Boolean>() { @Override public Boolean apply(@NonNull Long tick) throws Exception { return isConnected(adjustedHost, port, timeoutInMs, errorHandler); } }).distinctUntilChanged(); }
@Override public Observable<Boolean> observeInternetConnectivity(final int initialIntervalInMs, final int intervalInMs, final String host, final int port, final int timeoutInMs, final int httpResponse, final ErrorHandler errorHandler) { Preconditions.checkGreaterOrEqualToZero(initialIntervalInMs, "initialIntervalInMs is not a positive number"); Preconditions.checkGreaterThanZero(intervalInMs, "intervalInMs is not a positive number"); checkGeneralPreconditions(host, port, timeoutInMs, httpResponse, errorHandler); final String adjustedHost = adjustHost(host); return Observable.interval(initialIntervalInMs, intervalInMs, TimeUnit.MILLISECONDS, Schedulers.io()).map(new Function<Long, Boolean>() { @Override public Boolean apply(@NonNull Long tick) { return isConnected(adjustedHost, port, timeoutInMs, httpResponse, errorHandler); } }).distinctUntilChanged(); }