-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathMask.java
More file actions
80 lines (69 loc) · 2.39 KB
/
Mask.java
File metadata and controls
80 lines (69 loc) · 2.39 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
package com.softlayer.api;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/** Object mask parameter. See http://sldn.softlayer.com/article/Object-Masks */
public class Mask {
private final Set<String> localProperties = new HashSet<>();
private final Map<String, Mask> subMasks = new HashMap<>();
/** Clear out all previously masked objects and local properties */
public void clear() {
localProperties.clear();
subMasks.clear();
}
private int getChildCount() {
return localProperties.size() + subMasks.size();
}
protected void withLocalProperty(String localProperty) {
localProperties.add(localProperty);
}
@SuppressWarnings("unchecked")
protected <T extends Mask> T withSubMask(String name, Class<T> maskClass) {
T subMask = (T) subMasks.get(name);
if (subMask == null) {
try {
subMask = maskClass.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException();
}
subMasks.put(name, subMask);
}
return subMask;
}
protected String getMask() {
return "mask[" + toString() + "]";
}
@Override
public String toString() {
return toString(new StringBuilder()).toString();
}
/** Append this mask's string representation to the given builder and return it */
public StringBuilder toString(StringBuilder builder) {
boolean first = true;
for (String localProperty : localProperties) {
if (first) {
first = false;
} else {
builder.append(',');
}
builder.append(localProperty);
}
for (Map.Entry<String, Mask> entry : subMasks.entrySet()) {
if (first) {
first = false;
} else {
builder.append(',');
}
builder.append(entry.getKey());
// No count means add nothing, single is a dot, and multiple means brackets
int count = entry.getValue().getChildCount();
if (count == 1) {
entry.getValue().toString(builder.append('.'));
} else if (count > 1) {
entry.getValue().toString(builder.append('[')).append(']');
}
}
return builder;
}
}