Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply checkstyle #169

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
<module name="InvalidJavadocPosition"/>
<module name="JavadocMethod"/>
<module name="JavadocType"/>
<module name="JavadocVariable"/>
<module name="JavadocStyle"/>
<module name="MissingJavadocMethod"/>

Expand Down Expand Up @@ -130,7 +129,6 @@
<!-- Checks for Size Violations. -->
<!-- See https://checkstyle.org/checks/sizes/index.html -->
<module name="MethodLength"/>
<module name="ParameterNumber"/>

<!-- Checks for whitespace -->
<!-- See https://checkstyle.org/checks/whitespace/index.html -->
Expand Down Expand Up @@ -162,7 +160,6 @@
<!-- See https://checkstyle.org/checks/coding/index.html -->
<module name="EmptyStatement"/>
<module name="EqualsHashCode"/>
<module name="HiddenField"/>
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<module name="MagicNumber"/>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/fauna/annotation/FaunaColl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FaunaColl { }
public @interface FaunaColl {
}
3 changes: 2 additions & 1 deletion src/main/java/com/fauna/annotation/FaunaFieldImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public FaunaFieldImpl(FaunaField annotation) {

@Override
public String name() {
return (annotation != null && !annotation.name().isEmpty()) ? annotation.name() : null;
return (annotation != null && !annotation.name().isEmpty()) ?
annotation.name() : null;
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/fauna/annotation/FaunaIgnore.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FaunaIgnore { }
public @interface FaunaIgnore {
}
3 changes: 2 additions & 1 deletion src/main/java/com/fauna/annotation/FaunaObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Deprecated
public @interface FaunaObject { }
public @interface FaunaObject {
}
3 changes: 2 additions & 1 deletion src/main/java/com/fauna/annotation/FaunaTs.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FaunaTs { }
public @interface FaunaTs {
}
17 changes: 12 additions & 5 deletions src/main/java/com/fauna/client/BaseFaunaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ public final class BaseFaunaClient extends FaunaClient {
*/
public BaseFaunaClient(FaunaConfig faunaConfig,
HttpClient httpClient, RetryStrategy retryStrategy) {
super(faunaConfig.getSecret(), faunaConfig.getLogHandler(), faunaConfig.getStatsCollector());
super(faunaConfig.getSecret(), faunaConfig.getLogHandler(),
faunaConfig.getStatsCollector());
this.httpClient = httpClient;
if (Objects.isNull(faunaConfig)) {
throw new IllegalArgumentException("FaunaConfig cannot be null.");
} else if (Objects.isNull(httpClient)) {
throw new IllegalArgumentException("HttpClient cannot be null.");
} else {
this.baseRequestBuilder = RequestBuilder.queryRequestBuilder(faunaConfig, getLogger());
this.streamRequestBuilder = RequestBuilder.streamRequestBuilder(faunaConfig, getLogger());
this.feedRequestBuilder = RequestBuilder.feedRequestBuilder(faunaConfig, getLogger());
this.baseRequestBuilder =
RequestBuilder.queryRequestBuilder(faunaConfig,
getLogger());
this.streamRequestBuilder =
RequestBuilder.streamRequestBuilder(faunaConfig,
getLogger());
this.feedRequestBuilder =
RequestBuilder.feedRequestBuilder(faunaConfig, getLogger());
}
this.retryStrategy = retryStrategy;
}
Expand All @@ -47,7 +53,8 @@ public BaseFaunaClient(FaunaConfig faunaConfig,
* @param faunaConfig The Fauna configuration settings.
*/
public BaseFaunaClient(FaunaConfig faunaConfig) {
this(faunaConfig, HttpClient.newBuilder().build(), DEFAULT_RETRY_STRATEGY);
this(faunaConfig, HttpClient.newBuilder().build(),
DEFAULT_RETRY_STRATEGY);
}


Expand Down
43 changes: 28 additions & 15 deletions src/main/java/com/fauna/client/ExponentialBackoffStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class ExponentialBackoffStrategy implements RetryStrategy {

/**
* Construct an Exponential backoff strategy.
* The basic formula for exponential backoff is `b^(a-1)` where `b` is the backoff factor, and `a` is the retry
* attempt number. So for a backoff factor of 2, you get:
* 2^0=1, 2^1=2, 2^3=4, 2^4=8 ...
* The basic formula for exponential backoff is `b^(a-1)` where `b` is the backoff factor, and `a` is the retry
* attempt number. So for a backoff factor of 2, you get:
* 2^0=1, 2^1=2, 2^3=4, 2^4=8 ...
*
* @param maxAttempts The maximum amount of retry attempts. Defaults to 3 retry attempts which means
* the client will make a total of 4 requests before giving up.
Expand All @@ -23,24 +23,29 @@ public class ExponentialBackoffStrategy implements RetryStrategy {
* @param jitterFactor A value between 0 (0%) and 1 (100%) that controls how much to jitter the delay.
* The default is 0.5.
*/
ExponentialBackoffStrategy(int maxAttempts, float backoffFactor, int initialIntervalMillis,
ExponentialBackoffStrategy(int maxAttempts, float backoffFactor,
int initialIntervalMillis,
int maxBackoffMillis, float jitterFactor) {
this.maxAttempts = maxAttempts;
this.backoffFactor = backoffFactor;
this.initialIntervalMillis = initialIntervalMillis;
this.maxBackoffMillis = maxBackoffMillis;
this.jitterFactor = jitterFactor;
if (jitterFactor < 0.0 || jitterFactor > 1.0) {
throw new IllegalArgumentException("Jitter factor must be between 0 and 1.");
throw new IllegalArgumentException(
"Jitter factor must be between 0 and 1.");
}
if (backoffFactor < 0.0) {
throw new IllegalArgumentException("Backoff factor must be positive.");
throw new IllegalArgumentException(
"Backoff factor must be positive.");
}
if (maxAttempts < 0) {
throw new IllegalArgumentException("Max attempts must be a natural number (not negative).");
throw new IllegalArgumentException(
"Max attempts must be a natural number (not negative).");
}
if (initialIntervalMillis < 0) {
throw new IllegalArgumentException("Initial interval must be positive.");
throw new IllegalArgumentException(
"Initial interval must be positive.");
}
if (maxBackoffMillis < 0) {
throw new IllegalArgumentException("Max backoff must be positive.");
Expand All @@ -49,7 +54,8 @@ public class ExponentialBackoffStrategy implements RetryStrategy {

/**
* Get the % to jitter the backoff, will be a value between 0 and jitterFactor.
* @return A randomly generated value between 0 and jitterFactor.
*
* @return A randomly generated value between 0 and jitterFactor.
*/
private double getJitterPercent() {
return Math.random() * jitterFactor;
Expand All @@ -58,20 +64,25 @@ private double getJitterPercent() {
@Override
public boolean canRetry(int retryAttempt) {
if (retryAttempt < 0) {
throw new IllegalArgumentException("Retry attempt must be a natural number (not negative).");
throw new IllegalArgumentException(
"Retry attempt must be a natural number (not negative).");
}
return retryAttempt <= maxAttempts;
}

@Override
public int getDelayMillis(int retryAttempt) {
if (retryAttempt < 0) {
throw new IllegalArgumentException("Retry attempt must be a natural number (not negative).");
throw new IllegalArgumentException(
"Retry attempt must be a natural number (not negative).");
} else if (retryAttempt == 0) {
return 0;
} else {
double deterministicBackoff = Math.pow(this.backoffFactor, retryAttempt - 1);
double calculatedBackoff = deterministicBackoff * (1-getJitterPercent()) * initialIntervalMillis;
double deterministicBackoff =
Math.pow(this.backoffFactor, retryAttempt - 1);
double calculatedBackoff =
deterministicBackoff * (1 - getJitterPercent()) *
initialIntervalMillis;
return (int) Math.min(calculatedBackoff, this.maxBackoffMillis);
}
}
Expand All @@ -88,7 +99,8 @@ public int getMaxRetryAttempts() {
* purposes, then you can use the constructor directly.
*/
public static class Builder {
private float backoffFactor = 2.0f; // Results in delay of 1, 2, 4, 8, 16... seconds.
private float backoffFactor = 2.0f;
// Results in delay of 1, 2, 4, 8, 16... seconds.
private int maxAttempts = 3; // Limits number of retry attempts.
private int initialIntervalMillis = 1000;
private int maxBackoffMillis = 20_000;
Expand Down Expand Up @@ -123,7 +135,8 @@ public Builder jitterFactor(float jitterFactor) {

public ExponentialBackoffStrategy build() {
return new ExponentialBackoffStrategy(
this.maxAttempts, this.backoffFactor, this.initialIntervalMillis,
this.maxAttempts, this.backoffFactor,
this.initialIntervalMillis,
this.maxBackoffMillis, this.jitterFactor);
}
}
Expand Down
48 changes: 30 additions & 18 deletions src/main/java/com/fauna/client/Fauna.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ public class Fauna {

/**
* Create a default Fauna client.
* @return A FaunaClient (or subclass of it).
*
* @return A FaunaClient (or subclass of it).
*/
public static FaunaClient client() {
return new BaseFaunaClient(FaunaConfig.builder().build());
}

/**
* Create a Fauna client with the given FaunaConfig (and default HTTP client, and RetryStrategy).
* @param config Fauna configuration object.
* @return A FaunaClient (or subclass of it).
*
* @param config Fauna configuration object.
* @return A FaunaClient (or subclass of it).
*/
public static FaunaClient client(FaunaConfig config) {
if (config == null) {
Expand All @@ -26,12 +28,14 @@ public static FaunaClient client(FaunaConfig config) {

/**
* Create a Fauna client with the given FaunaConfig, HTTP client, and RetryStrategy.
*
* @param config Fauna configuration object.
* @param httpClient An HTTP client (from java.net.http in Java 11+).
* @param retryStrategy An implementation of RetryStrategy.
* @return A FaunaClient (or subclass of it).
* @return A FaunaClient (or subclass of it).
*/
public static FaunaClient client(FaunaConfig config, HttpClient httpClient, RetryStrategy retryStrategy) {
public static FaunaClient client(FaunaConfig config, HttpClient httpClient,
RetryStrategy retryStrategy) {
if (config == null) {
throw new IllegalArgumentException("FaunaConfig cannot be null.");
}
Expand All @@ -40,43 +44,51 @@ public static FaunaClient client(FaunaConfig config, HttpClient httpClient, Retr

/**
* Create a new Fauna client that wraps an existing client, but is scoped to a specific database.
* @param client Another Fauna client.
* @param database The name of the database.
* @return A FaunaClient (or subclass of it).
*
* @param client Another Fauna client.
* @param database The name of the database.
* @return A FaunaClient (or subclass of it).
*/
public static FaunaClient scoped(FaunaClient client, String database) {
if (client == null) {
throw new IllegalArgumentException("FaunaClient cannot be null.");
}
if (database == null || database.isEmpty()) {
throw new IllegalArgumentException("database cannot be null or empty.");
throw new IllegalArgumentException(
"database cannot be null or empty.");
}
return new ScopedFaunaClient(client, FaunaScope.builder(database).build());
return new ScopedFaunaClient(client,
FaunaScope.builder(database).build());
}

/**
* Create a new Fauna client that wraps an existing client, but is scoped to a specific database.
* @param client Another Fauna client.
* @param database The name of the database.
* @param role A Fauna role (either built-in or user defined).
* @return A FaunaClient (or subclass of it).
*
* @param client Another Fauna client.
* @param database The name of the database.
* @param role A Fauna role (either built-in or user defined).
* @return A FaunaClient (or subclass of it).
*/
public static FaunaClient scoped(FaunaClient client, String database, FaunaRole role) {
public static FaunaClient scoped(FaunaClient client, String database,
FaunaRole role) {
if (client == null) {
throw new IllegalArgumentException("FaunaClient cannot be null.");
}
if (database == null || database.isEmpty()) {
throw new IllegalArgumentException("database cannot be null or empty.");
throw new IllegalArgumentException(
"database cannot be null or empty.");
}
if (role == null) {
throw new IllegalArgumentException("role cannot be null or empty.");
}
return new ScopedFaunaClient(client, FaunaScope.builder(database).withRole(role).build());
return new ScopedFaunaClient(client,
FaunaScope.builder(database).withRole(role).build());
}

/**
* Create a Fauna client for local development using the Fauna Docker container.
* @return A FaunaClient (or subclass of it).
*
* @return A FaunaClient (or subclass of it).
*/
public static FaunaClient local() {
return new BaseFaunaClient(FaunaConfig.LOCAL);
Expand Down
Loading
Loading