forked from matomo-org/matomo-java-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiwikJsonObject.java
More file actions
111 lines (101 loc) · 3.43 KB
/
PiwikJsonObject.java
File metadata and controls
111 lines (101 loc) · 3.43 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
/*
* Piwik Java Tracker
*
* @link https://github.com/piwik/piwik-java-tracker
* @license https://github.com/piwik/piwik-java-tracker/blob/master/LICENSE BSD-3 Clause
*/
package org.piwik.java.tracking;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
/**
* Object representing the custom variable array required by some
* Piwik query parameters. An array is represented by an object that has
* the index of the entry as the key (1 indexed) and a 2 entry array representing
* a custom key and custom value as the value.
*
* @author brettcsorba
*/
@Deprecated
public class PiwikJsonObject{
Map<String, String> map = new LinkedHashMap<>();
/**
* Gets the custom value stored at this custom key.
* @param key key used to lookup value
* @return value stored at specified key, null if not present
*/
public String get(String key){
return map.get(key);
}
/**
* Returns true if this object contains no custom key-value pairs.
* @return true if this object contains no custom key-value pairs
*/
public boolean isEmpty(){
return size() == 0;
}
/**
* Puts a custom value at this custom key.
* @param key key to store value at
* @param value value to store at specified key
* @return previous value stored at key if present, null otherwise
*/
public String put(String key, String value){
return map.put(key, value);
}
/**
* Removes the custom value stored at this custom key.
* @param key key used to lookup value to remove
* @return the value that was removed, null if no value was there to remove
*/
public String remove(String key){
return map.remove(key);
}
/**
* Returns the number of custom key-value pairs.
* @return the number of custom key-value pairs
*/
public int size(){
return map.size();
}
/**
* Produces the JSON string representing this object.<br>
* <br>
* For example, if the following values were put into the object<br>
* <br>
* {@code ("key1", "value1") } and {@code ("key2", "value2") }<br>
* <br>
* {@code {"1": ["key1", "value1"], "2": ["key2": "value2"]} }<br>
* <br>
* would be produced. The produced JSON will be ordered according to the
* order the values were put in. Removing an object will cause the values
* to backfill accordingly.<br>
* <br>
* For example, if the following values were put into the object<br>
* <br>
* {@code ("key1", "value1")}, {@code ("key2", "value2")}, and {@code ("key3", "value3")}<br>
* <br>
* and {@code ("key2", "value2") } was then removed, then<br>
* <br>
* {@code {"1": ["key1", "value1"], "2": ["key3": "value3"]} }<br>
* <br>
* would be produced.
* <br>
* @return the JSON string representation of this object
*/
@Override
public String toString(){
JsonObjectBuilder ob = Json.createObjectBuilder();
int x = 1;
for (Entry<String, String> entry : map.entrySet()){
JsonArrayBuilder ab = Json.createArrayBuilder();
ab.add(entry.getKey());
ab.add(entry.getValue());
ob.add(Integer.toString(x++), ab);
}
return ob.build().toString();
}
}