forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFiltersBuilder.java
More file actions
145 lines (116 loc) · 4 KB
/
Copy pathFiltersBuilder.java
File metadata and controls
145 lines (116 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.github.dockerjava.core.util;
import com.google.common.base.Strings;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
/**
* Representation of Docker filters.
*
* @author Carlos Sanchez <carlos@apache.org>
*
*/
public class FiltersBuilder {
private static final Pattern UNTIL_TIMESTAMP_PATTERN =
Pattern.compile("^\\d{1,10}$");
private static final Pattern UNTIL_DATETIME_PATTERN =
Pattern.compile("^([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])([Tt]([01][0-9]|2[0-3]):([0-5][0-9]):"
+ "([0-5][0-9]|60)(\\.[0-9]+)?)?(([Zz])|([\\+|\\-]([01][0-9]|2[0-3]):[0-5][0-9]))?$");
private static final Pattern UNTIL_GO_PATTERN =
Pattern.compile("^([1-9][0-9]*h)?([1-9][0-9]*m)?([1-9][0-9]*s)?$");
private Map<String, List<String>> filters = new HashMap<>();
public FiltersBuilder() {
}
public FiltersBuilder withFilter(String key, String... value) {
filters.put(key, Arrays.asList(value));
return this;
}
public FiltersBuilder withFilter(String key, Collection<String> value) {
filters.put(key, value instanceof List ? (List<String>) value : new ArrayList<>(value));
return this;
}
public List<String> getFilter(String key) {
return filters.get(key);
}
public FiltersBuilder withImages(String... image) {
withFilter("image", image);
return this;
}
public List<String> getImage() {
return getFilter("image");
}
public FiltersBuilder withContainers(String... container) {
withFilter("container", container);
return this;
}
public List<String> getContainer() {
return getFilter("container");
}
/**
* Filter by labels
*
* @param labels
* string array in the form ["key"] or ["key=value"] or a mix of both
*/
public FiltersBuilder withLabels(String... labels) {
withFilter("label", labels);
return this;
}
/**
* Filter by labels
*
* @param labels
* {@link Map} of labels that contains label keys and values
*/
public FiltersBuilder withLabels(Map<String, String> labels) {
withFilter("label", labelsMapToList(labels).toArray(new String[labels.size()]));
return this;
}
public FiltersBuilder withUntil(String until) throws NumberFormatException {
if (!isValidUntil(until)) {
throw new NumberFormatException("Not valid format of 'until': " + until);
}
return withFilter("until", until);
}
private static List<String> labelsMapToList(Map<String, String> labels) {
List<String> result = new ArrayList<>();
for (Entry<String, String> entry : labels.entrySet()) {
String rest = (entry.getValue() != null & !entry.getValue().isEmpty()) ? "=" + entry.getValue() : "";
String label = entry.getKey() + rest;
result.add(label);
}
return result;
}
private boolean isValidUntil(String until) {
if (UNTIL_DATETIME_PATTERN.matcher(until).matches()) {
return true;
} else if (!Strings.isNullOrEmpty(until) && UNTIL_GO_PATTERN.matcher(until).matches()) {
return true;
} else if (UNTIL_TIMESTAMP_PATTERN.matcher(until).matches()) {
return true;
}
return false;
}
// CHECKSTYLE:OFF
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
FiltersBuilder filters1 = (FiltersBuilder) o;
return filters.equals(filters1.filters);
}
// CHECKSTYLE:ON
@Override
public int hashCode() {
return filters.hashCode();
}
public Map<String, List<String>> build() {
return filters;
}
}