View Javadoc
1   package org.kuali.common.devops.jenkins.upgrade;
2   
3   import static com.google.common.collect.Maps.newHashMap;
4   import static org.kuali.common.util.base.Precondition.checkNotBlank;
5   
6   import java.util.Map;
7   
8   import org.kuali.common.aws.s3.model.ObjectSummary;
9   import org.kuali.common.core.io.DiskUsage;
10  
11  import com.google.common.base.Predicate;
12  
13  public final class S3 {
14  
15  	public static Predicate<ObjectSummary> startsWith(String prefix) {
16  		return new ObjectSummaryStartsWith(prefix);
17  	}
18  
19  	private static class ObjectSummaryStartsWith implements Predicate<ObjectSummary> {
20  
21  		public ObjectSummaryStartsWith(String prefix) {
22  			this.prefix = checkNotBlank(prefix, "prefix");
23  		}
24  
25  		private final String prefix;
26  
27  		@Override
28  		public boolean apply(ObjectSummary summary) {
29  			return summary.getKey().startsWith(prefix);
30  		}
31  	}
32  
33  	public static Predicate<ObjectSummary> isDir() {
34  		return ObjectSummaryIsDir.INSTANCE;
35  	}
36  
37  	private enum ObjectSummaryIsDir implements Predicate<ObjectSummary> {
38  		INSTANCE;
39  		@Override
40  		public boolean apply(ObjectSummary summary) {
41  			return summary.getSize() == 0 && summary.getKey().endsWith("/");
42  		}
43  	}
44  
45  	public static DiskUsage sumFileObjects(Iterable<ObjectSummary> objects) {
46  		long sum = 0;
47  		long count = 0;
48  		for (ObjectSummary object : objects) {
49  			String key = object.getKey();
50  			if (!key.endsWith("/")) {
51  				count++;
52  				sum += object.getSize();
53  			}
54  		}
55  		return DiskUsage.builder().withCount(count).withSize(sum).build();
56  	}
57  
58  	public static Map<String, ObjectSummary> objectsAsMap(Iterable<ObjectSummary> objects) {
59  		Map<String, ObjectSummary> map = newHashMap();
60  		for (ObjectSummary object : objects) {
61  			map.put(object.getKey(), object);
62  		}
63  		return map;
64  	}
65  
66  }