|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.transfer.s3; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Objects; |
| 22 | +import software.amazon.awssdk.annotations.SdkPreviewApi; |
| 23 | +import software.amazon.awssdk.annotations.SdkPublicApi; |
| 24 | +import software.amazon.awssdk.utils.ToString; |
| 25 | +import software.amazon.awssdk.utils.Validate; |
| 26 | +import software.amazon.awssdk.utils.builder.CopyableBuilder; |
| 27 | +import software.amazon.awssdk.utils.builder.ToCopyableBuilder; |
| 28 | + |
| 29 | +/** |
| 30 | + * Represents a completed download directory transfer to Amazon S3. It can be used to track |
| 31 | + * failed single file downloads. |
| 32 | + * |
| 33 | + * @see S3TransferManager#downloadDirectory(DownloadDirectoryRequest) |
| 34 | + */ |
| 35 | +@SdkPublicApi |
| 36 | +@SdkPreviewApi |
| 37 | +public final class CompletedDirectoryDownload implements CompletedDirectoryTransfer, |
| 38 | + ToCopyableBuilder<CompletedDirectoryDownload.Builder, |
| 39 | + CompletedDirectoryDownload> { |
| 40 | + |
| 41 | + private final Collection<FailedFileDownload> failedTransfers; |
| 42 | + |
| 43 | + private CompletedDirectoryDownload(DefaultBuilder builder) { |
| 44 | + this.failedTransfers = Collections.unmodifiableCollection( |
| 45 | + new ArrayList<>(Validate.paramNotNull(builder.failedTransfers, "failedTransfers"))); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Collection<FailedFileDownload> failedTransfers() { |
| 50 | + return failedTransfers; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Creates a default builder for {@link CompletedDirectoryDownload}. |
| 55 | + */ |
| 56 | + public static Builder builder() { |
| 57 | + return new DefaultBuilder(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean equals(Object o) { |
| 62 | + if (this == o) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + if (o == null || getClass() != o.getClass()) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + CompletedDirectoryDownload that = (CompletedDirectoryDownload) o; |
| 70 | + |
| 71 | + return Objects.equals(failedTransfers, that.failedTransfers); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public int hashCode() { |
| 76 | + return failedTransfers != null ? failedTransfers.hashCode() : 0; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public String toString() { |
| 81 | + return ToString.builder("CompletedDirectoryDownload") |
| 82 | + .add("failedTransfers", failedTransfers) |
| 83 | + .build(); |
| 84 | + } |
| 85 | + |
| 86 | + public static Class<? extends Builder> serializableBuilderClass() { |
| 87 | + return DefaultBuilder.class; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Builder toBuilder() { |
| 92 | + return new DefaultBuilder(this); |
| 93 | + } |
| 94 | + |
| 95 | + public interface Builder extends CopyableBuilder<CompletedDirectoryDownload.Builder, |
| 96 | + CompletedDirectoryDownload> { |
| 97 | + |
| 98 | + /** |
| 99 | + * Sets a collection of {@link FailedFileDownload}s |
| 100 | + * |
| 101 | + * @param failedTransfers failed download |
| 102 | + * @return This builder for method chaining. |
| 103 | + */ |
| 104 | + Builder failedTransfers(Collection<FailedFileDownload> failedTransfers); |
| 105 | + |
| 106 | + /** |
| 107 | + * Add a {@link FailedFileDownload} |
| 108 | + * |
| 109 | + * @param failedTransfer failed download |
| 110 | + * @return This builder for method chaining. |
| 111 | + */ |
| 112 | + Builder addFailedTransfer(FailedFileDownload failedTransfer); |
| 113 | + |
| 114 | + /** |
| 115 | + * Builds a {@link CompletedDirectoryDownload} based on the properties supplied to this builder |
| 116 | + * @return An initialized {@link CompletedDirectoryDownload} |
| 117 | + */ |
| 118 | + CompletedDirectoryDownload build(); |
| 119 | + } |
| 120 | + |
| 121 | + private static final class DefaultBuilder implements Builder { |
| 122 | + private Collection<FailedFileDownload> failedTransfers = new ArrayList<>(); |
| 123 | + |
| 124 | + private DefaultBuilder() { |
| 125 | + } |
| 126 | + |
| 127 | + private DefaultBuilder(CompletedDirectoryDownload completedDirectoryDownload) { |
| 128 | + this.failedTransfers = new ArrayList<>(completedDirectoryDownload.failedTransfers); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public Builder failedTransfers(Collection<FailedFileDownload> failedTransfers) { |
| 133 | + this.failedTransfers = new ArrayList<>(failedTransfers); |
| 134 | + return this; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public Builder addFailedTransfer(FailedFileDownload failedTransfer) { |
| 139 | + failedTransfers.add(failedTransfer); |
| 140 | + return this; |
| 141 | + } |
| 142 | + |
| 143 | + public Collection<FailedFileDownload> getFailedTransfers() { |
| 144 | + return Collections.unmodifiableCollection(failedTransfers); |
| 145 | + } |
| 146 | + |
| 147 | + public void setFailedTransfers(Collection<FailedFileDownload> failedTransfers) { |
| 148 | + failedTransfers(failedTransfers); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public CompletedDirectoryDownload build() { |
| 153 | + return new CompletedDirectoryDownload(this); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments