Skip to content

[TM DownloadDirectory Part1] Create POJO classes for download directory #2993

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

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.transfer.s3;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import software.amazon.awssdk.annotations.SdkPreviewApi;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.utils.ToString;
import software.amazon.awssdk.utils.Validate;
import software.amazon.awssdk.utils.builder.CopyableBuilder;
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;

/**
* Represents a completed download directory transfer to Amazon S3. It can be used to track
* failed single file downloads.
*
* @see S3TransferManager#downloadDirectory(DownloadDirectoryRequest)
*/
@SdkPublicApi
@SdkPreviewApi
public final class CompletedDirectoryDownload implements CompletedDirectoryTransfer,
ToCopyableBuilder<CompletedDirectoryDownload.Builder,
CompletedDirectoryDownload> {

private final Collection<FailedFileDownload> failedTransfers;

private CompletedDirectoryDownload(DefaultBuilder builder) {
this.failedTransfers = Collections.unmodifiableCollection(
new ArrayList<>(Validate.paramNotNull(builder.failedTransfers, "failedTransfers")));
}

@Override
public Collection<FailedFileDownload> failedTransfers() {
return failedTransfers;
}
Comment on lines +49 to +51
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussion, non-blocking: IMO, it's more polite to return the most concrete abstract type, e.g. List instead of Collection here so that people can have constant-time random access to its members. I know that's controversial "because order doesn't matter" here, but I think it's more likely that a user would want to use failedTransfers().get(n) to get the n'th failure than a user would get confused about the order being "inconsistent" just because we returned a List instead of a Collection.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, @Bennett-Lynch and I had the same debate before! We can definitely open the discussion. The main reason I think Collection is better because it gives us flexibility to change the implementation (although it's very unlikely we will change it).
I think customers can still do failedFileUploads.stream().collect(Collectors.toList()) if they want to iterate the list by index.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you've already convinced Bennett that this is the correct decision, then I don't want to make you have to debate this again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was pro-List as well, and still somewhat am, but was mostly persuaded to use Collection since it allows us to use our ConcurrentLinkedQueue directly. I agree that ordering doesn't strictly matter here, but it still seems like a nice-to-have.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@millems Everyone's opinion matters! Let's vote on it. :)
@Bennett-Lynch We are still copying it to an ArrayList technically.


/**
* Creates a default builder for {@link CompletedDirectoryDownload}.
*/
public static Builder builder() {
return new DefaultBuilder();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

CompletedDirectoryDownload that = (CompletedDirectoryDownload) o;

return Objects.equals(failedTransfers, that.failedTransfers);
}

@Override
public int hashCode() {
return failedTransfers != null ? failedTransfers.hashCode() : 0;
}

@Override
public String toString() {
return ToString.builder("CompletedDirectoryDownload")
.add("failedTransfers", failedTransfers)
.build();
}

public static Class<? extends Builder> serializableBuilderClass() {
return DefaultBuilder.class;
}

@Override
public Builder toBuilder() {
return new DefaultBuilder(this);
}

public interface Builder extends CopyableBuilder<CompletedDirectoryDownload.Builder,
CompletedDirectoryDownload> {

/**
* Sets a collection of {@link FailedFileDownload}s
*
* @param failedTransfers failed download
* @return This builder for method chaining.
*/
Builder failedTransfers(Collection<FailedFileDownload> failedTransfers);

/**
* Add a {@link FailedFileDownload}
*
* @param failedTransfer failed download
* @return This builder for method chaining.
*/
Builder addFailedTransfer(FailedFileDownload failedTransfer);

/**
* Builds a {@link CompletedDirectoryDownload} based on the properties supplied to this builder
* @return An initialized {@link CompletedDirectoryDownload}
*/
CompletedDirectoryDownload build();
}

private static final class DefaultBuilder implements Builder {
private Collection<FailedFileDownload> failedTransfers = new ArrayList<>();

private DefaultBuilder() {
}

private DefaultBuilder(CompletedDirectoryDownload completedDirectoryDownload) {
this.failedTransfers = new ArrayList<>(completedDirectoryDownload.failedTransfers);
}

@Override
public Builder failedTransfers(Collection<FailedFileDownload> failedTransfers) {
this.failedTransfers = new ArrayList<>(failedTransfers);
return this;
}

@Override
public Builder addFailedTransfer(FailedFileDownload failedTransfer) {
failedTransfers.add(failedTransfer);
return this;
}

public Collection<FailedFileDownload> getFailedTransfers() {
return Collections.unmodifiableCollection(failedTransfers);
}

public void setFailedTransfers(Collection<FailedFileDownload> failedTransfers) {
failedTransfers(failedTransfers);
}

@Override
public CompletedDirectoryDownload build() {
return new CompletedDirectoryDownload(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.utils.ToString;
import software.amazon.awssdk.utils.Validate;
import software.amazon.awssdk.utils.builder.CopyableBuilder;
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;

/**
* Represents a completed upload directory transfer to Amazon S3. It can be used to track
Expand All @@ -32,13 +34,15 @@
*/
@SdkPublicApi
@SdkPreviewApi
public final class CompletedDirectoryUpload implements CompletedDirectoryTransfer {
public final class CompletedDirectoryUpload implements CompletedDirectoryTransfer,
ToCopyableBuilder<CompletedDirectoryUpload.Builder,
CompletedDirectoryUpload> {

private final Collection<FailedFileUpload> failedTransfers;

private CompletedDirectoryUpload(DefaultBuilder builder) {
this.failedTransfers = Collections.unmodifiableCollection(
Validate.paramNotNull(builder.failedTransfers, "failedTransfers"));
new ArrayList<>(Validate.paramNotNull(builder.failedTransfers, "failedTransfers")));
}

@Override
Expand Down Expand Up @@ -83,7 +87,13 @@ public static Class<? extends Builder> serializableBuilderClass() {
return DefaultBuilder.class;
}

public interface Builder {
@Override
public Builder toBuilder() {
return new DefaultBuilder(this);
}

public interface Builder extends CopyableBuilder<CompletedDirectoryUpload.Builder,
CompletedDirectoryUpload> {

/**
* Sets a collection of {@link FailedFileUpload}s
Expand All @@ -109,11 +119,15 @@ public interface Builder {
}

private static final class DefaultBuilder implements Builder {
private Collection<FailedFileUpload> failedTransfers;
private Collection<FailedFileUpload> failedTransfers = new ArrayList<>();

private DefaultBuilder() {
}

private DefaultBuilder(CompletedDirectoryUpload completedDirectoryUpload) {
this.failedTransfers = new ArrayList<>(completedDirectoryUpload.failedTransfers);
}

@Override
public Builder failedTransfers(Collection<FailedFileUpload> failedTransfers) {
this.failedTransfers = new ArrayList<>(failedTransfers);
Expand All @@ -122,9 +136,6 @@ public Builder failedTransfers(Collection<FailedFileUpload> failedTransfers) {

@Override
public Builder addFailedTransfer(FailedFileUpload failedTransfer) {
if (failedTransfers == null) {
failedTransfers = new ArrayList<>();
}
failedTransfers.add(failedTransfer);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.transfer.s3;

import java.util.concurrent.CompletableFuture;
import software.amazon.awssdk.annotations.SdkPreviewApi;
import software.amazon.awssdk.annotations.SdkPublicApi;

/**
* A download transfer of a directory of objects from S3
*/
@SdkPublicApi
@SdkPreviewApi
public interface DirectoryDownload extends DirectoryTransfer {
@Override
CompletableFuture<CompletedDirectoryDownload> completionFuture();
}
Loading