View Javadoc
1   package org.kuali.common.devops.archive.sweep.jenkins.s3;
2   
3   import static com.google.common.base.Optional.absent;
4   import static com.google.common.collect.Lists.newArrayList;
5   
6   import java.util.List;
7   import java.util.concurrent.Callable;
8   
9   import org.kuali.common.aws.s3.S3Service;
10  import org.kuali.common.aws.s3.model.ObjectMetadata;
11  import org.kuali.common.aws.s3.model.ObjectSummary;
12  import org.kuali.common.core.build.ValidatingBuilder;
13  import org.kuali.common.core.validate.annotation.IdiotProofImmutable;
14  import org.kuali.common.util.inform.PercentCompleteInformer;
15  
16  import com.google.common.base.Optional;
17  import com.google.common.collect.ImmutableList;
18  
19  @IdiotProofImmutable
20  public final class S3ObjectPairCallable implements Callable<List<S3ObjectPair>> {
21  
22  	private final String bucket;
23  	private final Optional<PercentCompleteInformer> informer;
24  	private final S3Service s3;
25  	private final ImmutableList<ObjectSummary> summaries;
26  
27  	@Override
28  	public List<S3ObjectPair> call() {
29  		List<S3ObjectPair> pairs = newArrayList();
30  		for (ObjectSummary summary : summaries) {
31  			ObjectMetadata metadata = s3.getMetadata(bucket, summary.getKey());
32  			S3ObjectPair pair = S3ObjectPair.builder().withMetadata(metadata).withSummary(summary).build();
33  			pairs.add(pair);
34  			if (informer.isPresent()) {
35  				informer.get().incrementProgress();
36  			}
37  		}
38  		return ImmutableList.copyOf(pairs);
39  	}
40  
41  	private S3ObjectPairCallable(Builder builder) {
42  		this.s3 = builder.s3;
43  		this.summaries = ImmutableList.copyOf(builder.summaries);
44  		this.bucket = builder.bucket;
45  		this.informer = builder.informer;
46  	}
47  
48  	public static Builder builder() {
49  		return new Builder();
50  	}
51  
52  	public static class Builder extends ValidatingBuilder<S3ObjectPairCallable> {
53  
54  		private String bucket;
55  		private S3Service s3;
56  		private List<ObjectSummary> summaries;
57  		private Optional<PercentCompleteInformer> informer = absent();
58  
59  		public Builder withBucket(String bucket) {
60  			this.bucket = bucket;
61  			return this;
62  		}
63  
64  		public Builder withS3(S3Service s3) {
65  			this.s3 = s3;
66  			return this;
67  		}
68  
69  		public Builder withInformer(Optional<PercentCompleteInformer> informer) {
70  			this.informer = informer;
71  			return this;
72  		}
73  
74  		public Builder withInformer(PercentCompleteInformer informer) {
75  			return withInformer(Optional.of(informer));
76  		}
77  
78  		public Builder withSummaries(List<ObjectSummary> summaries) {
79  			this.summaries = summaries;
80  			return this;
81  		}
82  
83  		@Override
84  		public S3ObjectPairCallable build() {
85  			return validate(new S3ObjectPairCallable(this));
86  		}
87  	}
88  
89  	public S3Service getS3() {
90  		return s3;
91  	}
92  
93  	public List<ObjectSummary> getSummaries() {
94  		return summaries;
95  	}
96  
97  	public String getBucket() {
98  		return bucket;
99  	}
100 
101 	public Optional<PercentCompleteInformer> getInformer() {
102 		return informer;
103 	}
104 
105 }