import java.util.HashMap;
import java.util.Map;
public class EnvironmentFilter {
/**
* Filters environment variables for non-production use.
* @param envVars A map of environment variables.
* @return A map containing only non-production environment variables.
*/
public static Map<String, String> filterForNonProduction(Map<String, String> envVars) {
Map<String, String> nonProductionVars = new HashMap<>();
if (envVars == null) {
return nonProductionVars; // Return empty map if input is null
}
for (Map.Entry<String, String> entry : envVars.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
// Define non-production environment variable prefixes. Extend as needed.
if (key.startsWith("dev_") || key.startsWith("local_") || key.startsWith("test_")) {
nonProductionVars.put(key, value);
}
}
return nonProductionVars;
}
public static void main(String[] args) {
// Example usage
Map<String, String> env = new HashMap<>();
env.put("DATABASE_URL", "prod_db_url");
env.put("DEBUG_MODE", "true");
env.put("API_KEY", "secret_key");
env.put("dev_API_KEY", "dev_secret");
env.put("LOCAL_PORT", "8080");
Map<String, String> filteredEnv = filterForNonProduction(env);
System.out.println(filteredEnv);
}
}
Add your comment