package io.cucumber.pro.environment;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Bamboo prefixes all environment variables with `bamboo_`. This class
 * strips them off.
 */
public class BambooEnvironmentVariables {
    private static Pattern BAMBOO_PATTERN = Pattern.compile("^bamboo_(.+)");

    public Map<String, String> convert(Map<String, String> env) {
        Map<String, String> result = new HashMap<>();

        SortedSet<String> vars = new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String v1, String v2) {
                if (BAMBOO_PATTERN.matcher(v1).lookingAt()) return -1;
                if (BAMBOO_PATTERN.matcher(v2).lookingAt()) return 1;
                return 0;
            }
        });
        vars.addAll(env.keySet());

        for (String key : vars) {
            Matcher matcher = BAMBOO_PATTERN.matcher(key);
            if (matcher.lookingAt()) {
                String value = env.get(key);
                String strippedVar = matcher.group(1);
                result.put(strippedVar, value);
            }
        }
        result.putAll(env);
        return result;
    }
}

@@ -52,120 +52,37 @@
 til.
-Comparator;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.SortedSet;%0Aimport java.util.TreeSet
+HashMap;%0Aimport java.util.Map
 ;%0Aim
@@ -484,25 +484,24 @@
 ap%3C%3E();%0A
-%0A
         
 SortedSe
@@ -496,410 +496,38 @@
     
-SortedSet%3CString%3E vars = new TreeSet%3C%3E(new Comparator%3CString%3E() %7B%0A            @Override%0A            public int compare(String v1, String v2) %7B%0A                if (BAMBOO_PATTERN.matcher(v1).lookingAt()) return -1;%0A                if (BAMBOO_PATTERN.matcher(v2).lookingAt()) return 1;%0A                return 0;%0A            %7D%0A        %7D);%0A        vars.addAll(env.keySet());%0A%0A        for (String key : vars
+for (String key : env.keySet()
 ) %7B%0A

Simplify bamboo filtering