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

Few enhancements in the builder framework #941

Merged
merged 1 commit into from
May 18, 2020
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
15 changes: 8 additions & 7 deletions api/src/main/java/io/minio/BaseArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,26 @@
import java.util.function.Consumer;

public abstract class BaseArgs {
public abstract static class BaseBuilder<T extends BaseBuilder<T, B>, B extends BaseArgs> {
protected List<Consumer<B>> operations;
public abstract static class Builder<B extends Builder<B, A>, A extends BaseArgs> {
protected List<Consumer<A>> operations;

public BaseBuilder() {
public Builder() {
this.operations = new ArrayList<>();
}

protected B build(Class<B> clazz) throws IllegalArgumentException {
@SuppressWarnings("unchecked") // safe as B will always be the builder of the current args class
public A build() throws IllegalArgumentException {
try {
B args = clazz.getDeclaredConstructor().newInstance();
A args = (A) this.getClass().getEnclosingClass().getDeclaredConstructor().newInstance();
operations.forEach(operation -> operation.accept(args));
return args;
} catch (InstantiationException
| IllegalAccessException
| InvocationTargetException
| NoSuchMethodException
| SecurityException e) {
// This should never happen as we'll always be
// sending a proper class as argument to build()
// This should never happen as we'll always have the
// Builder class as an enclosed class of the args class
e.printStackTrace();
return null;
}
Expand Down
12 changes: 6 additions & 6 deletions api/src/main/java/io/minio/BucketArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public String region() {
}

/** Base argument builder class. */
public abstract static class Builder<T extends Builder<T, B>, B extends BucketArgs>
extends BaseArgs.BaseBuilder<Builder<T, B>, B> {
public abstract static class Builder<B extends Builder<B, A>, A extends BucketArgs>
extends BaseArgs.Builder<B, A> {
protected void validateName(String name) {
if (name == null) {
throw new IllegalArgumentException("null bucket name");
Expand Down Expand Up @@ -61,16 +61,16 @@ protected void validateName(String name) {
}

@SuppressWarnings("unchecked") // Its safe to type cast to T as T is inherited by this class
public T bucket(String name) {
public B bucket(String name) {
validateName(name);
operations.add(args -> args.name = name);
return (T) this;
return (B) this;
}

@SuppressWarnings("unchecked") // Its safe to type cast to T as T is inherited by this class
public T region(String region) {
public B region(String region) {
operations.add(args -> args.region = region);
return (T) this;
return (B) this;
}
}
}
4 changes: 0 additions & 4 deletions api/src/main/java/io/minio/MakeBucketArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,5 @@ public Builder objectLock(boolean objectLock) {
operations.add(args -> args.objectLock = objectLock);
return this;
}

public MakeBucketArgs build() throws IllegalArgumentException {
return build(MakeBucketArgs.class);
}
}
}