1. import java.util.HashMap;
  2. import java.util.Map;
  3. public class EnvironmentFilter {
  4. /**
  5. * Filters environment variables for non-production use.
  6. * @param envVars A map of environment variables.
  7. * @return A map containing only non-production environment variables.
  8. */
  9. public static Map<String, String> filterForNonProduction(Map<String, String> envVars) {
  10. Map<String, String> nonProductionVars = new HashMap<>();
  11. if (envVars == null) {
  12. return nonProductionVars; // Return empty map if input is null
  13. }
  14. for (Map.Entry<String, String> entry : envVars.entrySet()) {
  15. String key = entry.getKey();
  16. String value = entry.getValue();
  17. // Define non-production environment variable prefixes. Extend as needed.
  18. if (key.startsWith("dev_") || key.startsWith("local_") || key.startsWith("test_")) {
  19. nonProductionVars.put(key, value);
  20. }
  21. }
  22. return nonProductionVars;
  23. }
  24. public static void main(String[] args) {
  25. // Example usage
  26. Map<String, String> env = new HashMap<>();
  27. env.put("DATABASE_URL", "prod_db_url");
  28. env.put("DEBUG_MODE", "true");
  29. env.put("API_KEY", "secret_key");
  30. env.put("dev_API_KEY", "dev_secret");
  31. env.put("LOCAL_PORT", "8080");
  32. Map<String, String> filteredEnv = filterForNonProduction(env);
  33. System.out.println(filteredEnv);
  34. }
  35. }

Add your comment