Skip to content

Commit 23df71d

Browse files
committed
Replace java 8 method putIfAbsent with java 7 compatable approach
1 parent 709012c commit 23df71d

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

src/main/java/org/piwik/java/tracking/CustomVariableList.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/*
1+
/*
22
* Piwik Java Tracker
3-
*
3+
*
44
* @link https://github.com/piwik/piwik-java-tracker
55
* @license https://github.com/piwik/piwik-java-tracker/blob/master/LICENSE BSD-3 Clause
66
*/
@@ -31,24 +31,28 @@ void add(CustomVariable cv){
3131
}
3232
if (!found){
3333
int i = 1;
34-
while (map.putIfAbsent(i++, cv) != null){}
34+
while (map.containsKey(i)) {
35+
++i;
36+
}
37+
38+
map.put(i, cv);
3539
}
3640
}
37-
41+
3842
void add(CustomVariable cv, int index){
3943
if (index <= 0){
4044
throw new IllegalArgumentException("Index must be greater than 0.");
4145
}
4246
map.put(index, cv);
4347
}
44-
48+
4549
CustomVariable get(int index){
4650
if (index <= 0){
4751
throw new IllegalArgumentException("Index must be greater than 0.");
4852
}
4953
return map.get(index);
5054
}
51-
55+
5256
String get(String key){
5357
Iterator<Entry<Integer, CustomVariable>> i = map.entrySet().iterator();
5458
while (i.hasNext()){
@@ -59,11 +63,11 @@ String get(String key){
5963
}
6064
return null;
6165
}
62-
66+
6367
void remove(int index){
6468
map.remove(index);
6569
}
66-
70+
6771
void remove(String key){
6872
Iterator<Entry<Integer, CustomVariable>> i = map.entrySet().iterator();
6973
while (i.hasNext()){
@@ -73,22 +77,22 @@ void remove(String key){
7377
}
7478
}
7579
}
76-
80+
7781
boolean isEmpty(){
7882
return map.isEmpty();
7983
}
80-
84+
8185
@Override
8286
public String toString(){
8387
JsonObjectBuilder ob = Json.createObjectBuilder();
84-
88+
8589
for (Entry<Integer, CustomVariable> entry : map.entrySet()){
8690
JsonArrayBuilder ab = Json.createArrayBuilder();
8791
ab.add(entry.getValue().getKey());
8892
ab.add(entry.getValue().getValue());
8993
ob.add(entry.getKey().toString(), ab);
9094
}
91-
95+
9296
return ob.build().toString();
9397
}
9498
}

0 commit comments

Comments
 (0)