17
17
*/
18
18
package org .zaproxy .clientapi .core ;
19
19
20
+ import java .util .Collection ;
21
+ import java .util .Collections ;
20
22
import java .util .HashMap ;
21
23
import java .util .Map ;
22
24
import java .util .Map .Entry ;
25
+ import java .util .Set ;
23
26
24
27
import org .w3c .dom .Node ;
25
28
26
29
public class ApiResponseSet extends ApiResponse {
27
30
28
31
private String [] attributes = null ;
29
- private Map <String , String > values = null ;
32
+ private final Map <String , String > valuesMap ;
30
33
34
+ /**
35
+ * Constructs an {@code ApiResponseSet} with the given name and attributes.
36
+ *
37
+ * @param name the name of the API response
38
+ * @param attributes the attributes
39
+ * @deprecated (TODO add version) Unused, there's no replacement.
40
+ */
41
+ @ Deprecated
31
42
public ApiResponseSet (String name , String [] attributes ) {
32
43
super (name );
33
44
this .attributes = attributes ;
45
+ this .valuesMap = Collections .emptyMap ();
34
46
}
35
47
36
48
public ApiResponseSet (String name , Map <String , String > values ) {
37
49
super (name );
38
- this .values = values ;
50
+ this .valuesMap = Collections . unmodifiableMap ( new HashMap <>( values )) ;
39
51
}
40
52
41
53
public ApiResponseSet (Node node ) throws ClientApiException {
42
54
super (node .getNodeName ());
43
55
Node child = node .getFirstChild ();
44
- this . values = new HashMap <String , String >();
56
+ Map < String , String > values = new HashMap <>();
45
57
while (child != null ) {
46
58
ApiResponseElement elem = (ApiResponseElement ) ApiResponseFactory .getResponse (child );
47
59
values .put (elem .getName (), elem .getValue ());
48
60
child = child .getNextSibling ();
49
61
}
62
+ this .valuesMap = Collections .unmodifiableMap (values );
50
63
}
51
64
65
+ /**
66
+ * Gets the attributes.
67
+ *
68
+ * @return the attributes, might be {@code null}.
69
+ * @deprecated (TODO add version) Unused, there's no replacement.
70
+ * @see #getValues()
71
+ */
72
+ @ Deprecated
52
73
public String [] getAttributes () {
53
74
return attributes ;
54
75
}
55
76
56
- public String getAttribute (String name ) {
57
- return this .values .get (name );
77
+ /**
78
+ * Gets the value for the given {@code key}.
79
+ *
80
+ * @param key the key of the value
81
+ * @return the value, or {@code null} if no value exists for the given {@code key}.
82
+ * @deprecated (TODO add version) Use {@link #getValue(String)} instead.
83
+ */
84
+ @ Deprecated
85
+ public String getAttribute (String key ) {
86
+ return getValue (key );
87
+ }
88
+
89
+ /**
90
+ * Gets the value for the given {@code key}.
91
+ *
92
+ * @param key the key of the value
93
+ * @return the value, or {@code null} if no value exists for the given {@code key}.
94
+ * @since TODO add version
95
+ * @see #getKeys()
96
+ */
97
+ public String getValue (String key ) {
98
+ return valuesMap .get (key );
99
+ }
100
+
101
+ /**
102
+ * Gets a {@code Map} with the keys and values.
103
+ * <p>
104
+ * The returned {@code Map} is unmodifiable, any attempt to modify it will result in an
105
+ * {@code UnsupportedOperationException}.
106
+ *
107
+ * @return the map with the keys/values, never {@code null}.
108
+ * @since TODO add version
109
+ */
110
+ public Map <String , String > getValuesMap () {
111
+ return valuesMap ;
112
+ }
113
+
114
+ /**
115
+ * Gets the keys of the values.
116
+ * <p>
117
+ * The returned {@code Set} is unmodifiable, any attempt to modify it will result in an
118
+ * {@code UnsupportedOperationException}.
119
+ *
120
+ * @return the keys, never {@code null}.
121
+ * @since TODO add version
122
+ * @see #getValue(String)
123
+ * @see #getValues()
124
+ * @see #getValuesMap()
125
+ */
126
+ public Set <String > getKeys () {
127
+ return valuesMap .keySet ();
128
+ }
129
+
130
+ /**
131
+ * Gets the values.
132
+ * <p>
133
+ * The returned {@code Collection} is unmodifiable, any attempt to modify it will result in an
134
+ * {@code UnsupportedOperationException}.
135
+ *
136
+ * @return the values, never {@code null}.
137
+ * @since TODO add version
138
+ * @see #getValue(String)
139
+ */
140
+ public Collection <String > getValues () {
141
+ return valuesMap .values ();
58
142
}
59
143
60
144
@ Override
@@ -66,7 +150,7 @@ public String toString(int indent) {
66
150
sb .append ("ApiResponseSet " );
67
151
sb .append (this .getName ());
68
152
sb .append (" : [\n " );
69
- for (Entry <String , String > val : values .entrySet ()) {
153
+ for (Entry <String , String > val : valuesMap .entrySet ()) {
70
154
for (int i =0 ; i < indent +1 ; i ++) {
71
155
sb .append ("\t " );
72
156
}
0 commit comments