/* 
 * 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.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;

/**
 *
 * @author brettcsorba
 */
class CustomVariableList{
    private final Map<Integer, CustomVariable> map = new HashMap<>();

    void add(CustomVariable cv){
        boolean found = false;
        for (Entry<Integer, CustomVariable> entry : map.entrySet()){
            if (entry.getValue().getKey().equals(cv.getKey())){
                map.put(entry.getKey(), cv);
                found = true;
            }
        }
        if (!found){
            int i = 1;
            while (map.putIfAbsent(i++, cv) != null){}        
        }
    }
    
    void add(CustomVariable cv, int index){
        if (index <= 0){
            throw new IllegalArgumentException("Index must be greater than 0.");
        }
        map.put(index, cv);
    }
    
    CustomVariable get(int index){
        if (index <= 0){
            throw new IllegalArgumentException("Index must be greater than 0.");
        }
        return map.get(index);
    }
    
    String get(String key){
        Iterator<Entry<Integer, CustomVariable>> i = map.entrySet().iterator();
        while (i.hasNext()){
            CustomVariable value = i.next().getValue();
            if (value.getKey().equals(key)){
                return value.getValue();
            }
        }
        return null;
    }
    
    void remove(int index){
        map.remove(index);
    }
    
    void remove(String key){
        Iterator<Entry<Integer, CustomVariable>> i = map.entrySet().iterator();
        while (i.hasNext()){
            Entry<Integer, CustomVariable> entry = i.next();
            if (entry.getValue().getKey().equals(key)){
                i.remove();
            }
        }
    }
    
    boolean isEmpty(){
        return map.isEmpty();
    }
        
    @Override
    public String toString(){
        JsonObjectBuilder ob = Json.createObjectBuilder();
        
        for (Entry<Integer, CustomVariable> entry : map.entrySet()){
            JsonArrayBuilder ab = Json.createArrayBuilder();
            ab.add(entry.getValue().getKey());
            ab.add(entry.getValue().getValue());
            ob.add(entry.getKey().toString(), ab);
        }
        
        return ob.build().toString();
    }
}

@@ -1,12 +1,11 @@
 /*
- 
 %0A
+
  * Piwik
@@ -20,17 +20,16 @@
 acker%0A *
- 
 %0A * @lin
@@ -900,47 +900,89 @@
 map.
-putIfAbsent(i++, cv) != null)%7B%7D        
+containsKey(i)) %7B%0A                ++i;%0A            %7D%0A%0A            map.put(i, cv);
 %0A   
@@ -990,29 +990,25 @@
     %7D%0A    %7D%0A
-    
 %0A
+
     void add
@@ -1185,29 +1185,25 @@
  cv);%0A    %7D%0A
-    
 %0A
+
     CustomVa
@@ -1374,29 +1374,25 @@
 dex);%0A    %7D%0A
-    
 %0A
+
     String g
@@ -1701,37 +1701,33 @@
 urn null;%0A    %7D%0A
-    
 %0A
+
     void remove(
@@ -1767,29 +1767,25 @@
 dex);%0A    %7D%0A
-    
 %0A
+
     void rem
@@ -2085,21 +2085,17 @@
 %7D%0A    %7D%0A
-    
 %0A
+
     bool
@@ -2141,33 +2141,25 @@
 ty();%0A    %7D%0A
-        
 %0A
+
     @Overrid
@@ -2249,25 +2249,17 @@
 lder();%0A
-        
 %0A
+
         
@@ -2537,24 +2537,16 @@
       %7D%0A
-        
 %0A       

Replace java 8 method putIfAbsent with java 7 compatable approach