forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFiltersBuilder.java
More file actions
108 lines (86 loc) · 2.57 KB
/
Copy pathFiltersBuilder.java
File metadata and controls
108 lines (86 loc) · 2.57 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
package com.github.dockerjava.core.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* Representation of Docker filters.
*
* @author Carlos Sanchez <carlos@apache.org>
*
*/
public class FiltersBuilder {
private Map<String, List<String>> filters = new HashMap<String, List<String>>();
public FiltersBuilder() {
}
public FiltersBuilder withFilter(String key, String... value) {
filters.put(key, Arrays.asList(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;
}
private static List<String> labelsMapToList(Map<String, String> labels) {
List<String> result = new ArrayList<String>();
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;
}
// 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;
}
}