Skip to content

Commit

Permalink
replace usage of guava with standard java
Browse files Browse the repository at this point in the history
Avoid adding extra third party dependency 'guava' by replacing methods with equivalent alternatives in standard jdk.

This helps users of etcd since every transitive third party dependency requires more code to be downloaded and might result in dependency conflicts.

fixes #1207
  • Loading branch information
askoog committed Aug 24, 2023
1 parent aa9e8a0 commit 4796332
Show file tree
Hide file tree
Showing 20 changed files with 105 additions and 76 deletions.
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ grpc = "1.57.2"
log4j = "2.20.0"
mockito = "5.5.0"
slf4j = "2.0.7"
guava = "32.1.2-jre"
assertj = "3.24.2"
junit = "5.10.0"
testcontainers = "1.19.0"
Expand All @@ -29,7 +28,6 @@ testRetryPlugin = "1.5.3"

[libraries]
slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }
guava = { module = "com.google.guava:guava", version.ref = "guava" }
assertj = { module = "org.assertj:assertj-core", version.ref = "assertj" }
junit = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" }
testcontainers = { module = "org.testcontainers:testcontainers", version.ref = "testcontainers" }
Expand Down
1 change: 0 additions & 1 deletion jetcd-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

dependencies {
api libs.slf4j
api libs.guava
api libs.grpcCore

testImplementation libs.bundles.testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.etcd.jetcd.common.exception;

import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Objects;

/**
* Base exception type for all exceptions produced by the etcd service.
Expand All @@ -27,7 +27,7 @@ public class EtcdException extends RuntimeException {

EtcdException(ErrorCode code, String message, Throwable cause) {
super(message, cause);
this.code = checkNotNull(code);
this.code = Objects.requireNonNull(code, "Error code must not be null");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

package io.etcd.jetcd.common.exception;

import java.util.Objects;

import io.grpc.Status;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.grpc.Status.fromThrowable;

/**
Expand Down Expand Up @@ -65,7 +66,7 @@ public static EtcdException handleInterrupt(InterruptedException e) {
}

public static EtcdException toEtcdException(Throwable cause) {
checkNotNull(cause, "cause can't be null");
Objects.requireNonNull(cause, "cause can't be null");
if (cause instanceof EtcdException) {
return (EtcdException) cause;
}
Expand All @@ -74,7 +75,7 @@ public static EtcdException toEtcdException(Throwable cause) {
}

public static EtcdException toEtcdException(Status status) {
checkNotNull(status, "status can't be null");
Objects.requireNonNull(status, "status can't be null");
return fromStatus(status);
}

Expand Down
8 changes: 4 additions & 4 deletions jetcd-core/src/main/java/io/etcd/jetcd/ByteSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package io.etcd.jetcd;

import java.nio.charset.Charset;
import java.util.Objects;

import com.google.common.base.Preconditions;
import com.google.protobuf.ByteString;

/**
Expand All @@ -32,7 +32,7 @@ public final class ByteSequence {
private final ByteString byteString;

private ByteSequence(ByteString byteString) {
Preconditions.checkNotNull(byteString, "byteString should not be null");
Objects.requireNonNull(byteString, "byteString should not be null");
this.byteString = byteString;
this.hashVal = byteString.hashCode();
}
Expand All @@ -58,7 +58,7 @@ public boolean startsWith(ByteSequence prefix) {
* @return a new {@code ByteSequence} instance
*/
public ByteSequence concat(ByteSequence other) {
Preconditions.checkNotNull(other, "other byteSequence should not be null");
Objects.requireNonNull(other, "other byteSequence should not be null");
return new ByteSequence(this.byteString.concat(other.byteString));
}

Expand All @@ -69,7 +69,7 @@ public ByteSequence concat(ByteSequence other) {
* @return a new {@code ByteSequence} instance
*/
public ByteSequence concat(ByteString other) {
Preconditions.checkNotNull(other, "other byteSequence should not be null");
Objects.requireNonNull(other, "other byteSequence should not be null");
return new ByteSequence(this.byteString.concat(other));
}

Expand Down
17 changes: 8 additions & 9 deletions jetcd-core/src/main/java/io/etcd/jetcd/ClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import javax.net.ssl.SSLException;

Expand All @@ -37,9 +38,7 @@
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Streams;

/**
* ClientBuilder knows how to create a Client instance.
Expand Down Expand Up @@ -131,15 +130,15 @@ public ClientBuilder endpoints(URI... endpoints) {
* @throws IllegalArgumentException if some endpoint is invalid
*/
public ClientBuilder endpoints(Iterable<URI> endpoints) {
Preconditions.checkNotNull(endpoints, "endpoints can't be null");
Objects.requireNonNull(endpoints, "endpoints can't be null");

endpoints.forEach(e -> {
if (e.getHost() == null) {
throw new IllegalArgumentException("Unable to compute target from endpoint: '" + e + "'");
}
});

final String target = Streams.stream(endpoints)
final String target = StreamSupport.stream(endpoints.spliterator(), false)
.map(e -> e.getHost() + (e.getPort() != -1 ? ":" + e.getPort() : ""))
.distinct()
.collect(Collectors.joining(","));
Expand Down Expand Up @@ -173,7 +172,7 @@ public ByteSequence user() {
* @throws NullPointerException if user is <code>null</code>
*/
public ClientBuilder user(ByteSequence user) {
Preconditions.checkNotNull(user, "user can't be null");
Objects.requireNonNull(user, "user can't be null");
this.user = user;
return this;
}
Expand All @@ -195,7 +194,7 @@ public ByteSequence password() {
* @throws NullPointerException if password is <code>null</code>
*/
public ClientBuilder password(ByteSequence password) {
Preconditions.checkNotNull(password, "password can't be null");
Objects.requireNonNull(password, "password can't be null");
this.password = password;
return this;
}
Expand All @@ -218,7 +217,7 @@ public ByteSequence namespace() {
* @throws NullPointerException if namespace is <code>null</code>
*/
public ClientBuilder namespace(ByteSequence namespace) {
Preconditions.checkNotNull(namespace, "namespace can't be null");
Objects.requireNonNull(namespace, "namespace can't be null");
this.namespace = namespace;
return this;
}
Expand All @@ -240,7 +239,7 @@ public ExecutorService executorService() {
* @throws NullPointerException if executorService is <code>null</code>
*/
public ClientBuilder executorService(ExecutorService executorService) {
Preconditions.checkNotNull(executorService, "executorService can't be null");
Objects.requireNonNull(executorService, "executorService can't be null");
this.executorService = executorService;
return this;
}
Expand All @@ -253,7 +252,7 @@ public ClientBuilder executorService(ExecutorService executorService) {
* @throws NullPointerException if loadBalancerPolicy is <code>null</code>
*/
public ClientBuilder loadBalancerPolicy(String loadBalancerPolicy) {
Preconditions.checkNotNull(loadBalancerPolicy, "loadBalancerPolicy can't be null");
Objects.requireNonNull(loadBalancerPolicy, "loadBalancerPolicy can't be null");
this.loadBalancerPolicy = loadBalancerPolicy;
return this;
}
Expand Down
32 changes: 32 additions & 0 deletions jetcd-core/src/main/java/io/etcd/jetcd/Preconditions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2016-2023 The jetcd authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.etcd.jetcd;

public class Preconditions {

public static void checkArgument(boolean expression, String errorMessage) {
if (!expression) {
throw new IllegalArgumentException(errorMessage);
}
}

public static void checkState(boolean expression, String errorMessage) {
if (!expression) {
throw new IllegalStateException(errorMessage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import com.google.protobuf.ByteString;

import static com.google.common.base.Preconditions.checkArgument;
import static io.etcd.jetcd.Preconditions.checkArgument;

/**
* AuthTokenInterceptor fills header with Auth token of any rpc calls and
Expand Down
42 changes: 21 additions & 21 deletions jetcd-core/src/main/java/io/etcd/jetcd/impl/AuthImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

import com.google.protobuf.ByteString;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

/**
* Implementation of etcd auth client.
Expand Down Expand Up @@ -88,8 +88,8 @@ public CompletableFuture<AuthDisableResponse> authDisable() {

@Override
public CompletableFuture<AuthUserAddResponse> userAdd(ByteSequence user, ByteSequence password) {
checkNotNull(user, "user can't be null");
checkNotNull(password, "password can't be null");
requireNonNull(user, "user can't be null");
requireNonNull(password, "password can't be null");

AuthUserAddRequest addRequest = AuthUserAddRequest.newBuilder()
.setNameBytes(ByteString.copyFrom(user.getBytes()))
Expand All @@ -103,7 +103,7 @@ public CompletableFuture<AuthUserAddResponse> userAdd(ByteSequence user, ByteSeq

@Override
public CompletableFuture<AuthUserDeleteResponse> userDelete(ByteSequence user) {
checkNotNull(user, "user can't be null");
requireNonNull(user, "user can't be null");

AuthUserDeleteRequest deleteRequest = AuthUserDeleteRequest.newBuilder()
.setNameBytes(ByteString.copyFrom(user.getBytes()))
Expand All @@ -116,8 +116,8 @@ public CompletableFuture<AuthUserDeleteResponse> userDelete(ByteSequence user) {

@Override
public CompletableFuture<AuthUserChangePasswordResponse> userChangePassword(ByteSequence user, ByteSequence password) {
checkNotNull(user, "user can't be null");
checkNotNull(password, "password can't be null");
requireNonNull(user, "user can't be null");
requireNonNull(password, "password can't be null");

AuthUserChangePasswordRequest changePasswordRequest = AuthUserChangePasswordRequest.newBuilder()
.setNameBytes(ByteString.copyFrom(user.getBytes()))
Expand All @@ -131,7 +131,7 @@ public CompletableFuture<AuthUserChangePasswordResponse> userChangePassword(Byte

@Override
public CompletableFuture<AuthUserGetResponse> userGet(ByteSequence user) {
checkNotNull(user, "user can't be null");
requireNonNull(user, "user can't be null");

AuthUserGetRequest userGetRequest = AuthUserGetRequest.newBuilder()
.setNameBytes(ByteString.copyFrom(user.getBytes()))
Expand All @@ -153,8 +153,8 @@ public CompletableFuture<AuthUserListResponse> userList() {

@Override
public CompletableFuture<AuthUserGrantRoleResponse> userGrantRole(ByteSequence user, ByteSequence role) {
checkNotNull(user, "user can't be null");
checkNotNull(role, "key can't be null");
requireNonNull(user, "user can't be null");
requireNonNull(role, "key can't be null");

AuthUserGrantRoleRequest userGrantRoleRequest = AuthUserGrantRoleRequest.newBuilder()
.setUserBytes(ByteString.copyFrom(user.getBytes()))
Expand All @@ -168,8 +168,8 @@ public CompletableFuture<AuthUserGrantRoleResponse> userGrantRole(ByteSequence u

@Override
public CompletableFuture<AuthUserRevokeRoleResponse> userRevokeRole(ByteSequence user, ByteSequence role) {
checkNotNull(user, "user can't be null");
checkNotNull(role, "key can't be null");
requireNonNull(user, "user can't be null");
requireNonNull(role, "key can't be null");

AuthUserRevokeRoleRequest userRevokeRoleRequest = AuthUserRevokeRoleRequest.newBuilder()
.setNameBytes(ByteString.copyFrom(user.getBytes()))
Expand All @@ -183,7 +183,7 @@ public CompletableFuture<AuthUserRevokeRoleResponse> userRevokeRole(ByteSequence

@Override
public CompletableFuture<AuthRoleAddResponse> roleAdd(ByteSequence user) {
checkNotNull(user, "user can't be null");
requireNonNull(user, "user can't be null");

AuthRoleAddRequest roleAddRequest = AuthRoleAddRequest.newBuilder().setNameBytes(ByteString.copyFrom(user.getBytes()))
.build();
Expand All @@ -196,10 +196,10 @@ public CompletableFuture<AuthRoleAddResponse> roleAdd(ByteSequence user) {
@Override
public CompletableFuture<AuthRoleGrantPermissionResponse> roleGrantPermission(ByteSequence role, ByteSequence key,
ByteSequence rangeEnd, Permission.Type permType) {
checkNotNull(role, "role can't be null");
checkNotNull(key, "key can't be null");
checkNotNull(rangeEnd, "rangeEnd can't be null");
checkNotNull(permType, "permType can't be null");
requireNonNull(role, "role can't be null");
requireNonNull(key, "key can't be null");
requireNonNull(rangeEnd, "rangeEnd can't be null");
requireNonNull(permType, "permType can't be null");

io.etcd.jetcd.api.Permission.Type type;
switch (permType) {
Expand Down Expand Up @@ -235,7 +235,7 @@ public CompletableFuture<AuthRoleGrantPermissionResponse> roleGrantPermission(By

@Override
public CompletableFuture<AuthRoleGetResponse> roleGet(ByteSequence role) {
checkNotNull(role, "role can't be null");
requireNonNull(role, "role can't be null");

AuthRoleGetRequest roleGetRequest = AuthRoleGetRequest.newBuilder()
.setRoleBytes(ByteString.copyFrom(role.getBytes()))
Expand All @@ -258,9 +258,9 @@ public CompletableFuture<AuthRoleListResponse> roleList() {
@Override
public CompletableFuture<AuthRoleRevokePermissionResponse> roleRevokePermission(ByteSequence role, ByteSequence key,
ByteSequence rangeEnd) {
checkNotNull(role, "role can't be null");
checkNotNull(key, "key can't be null");
checkNotNull(rangeEnd, "rangeEnd can't be null");
requireNonNull(role, "role can't be null");
requireNonNull(key, "key can't be null");
requireNonNull(rangeEnd, "rangeEnd can't be null");

AuthRoleRevokePermissionRequest roleRevokePermissionRequest = AuthRoleRevokePermissionRequest.newBuilder()
.setRoleBytes(ByteString.copyFrom(role.getBytes()))
Expand All @@ -275,7 +275,7 @@ public CompletableFuture<AuthRoleRevokePermissionResponse> roleRevokePermission(

@Override
public CompletableFuture<AuthRoleDeleteResponse> roleDelete(ByteSequence role) {
checkNotNull(role, "role can't be null");
requireNonNull(role, "role can't be null");
AuthRoleDeleteRequest roleDeleteRequest = AuthRoleDeleteRequest.newBuilder()
.setRoleBytes(ByteString.copyFrom(role.getBytes()))
.build();
Expand Down
Loading

0 comments on commit 4796332

Please sign in to comment.