Skip to content

Commit

Permalink
wip: EnvUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Chan committed Jul 5, 2024
1 parent bd619b7 commit 8fdeaed
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tile-util/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<TILE_TEST_KEY_1>TEST</TILE_TEST_KEY_1>
<TILE_SYS_KEY_1>SYS</TILE_SYS_KEY_1>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
88 changes: 88 additions & 0 deletions tile-util/src/main/java/com/power4j/tile/util/EnvUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.power4j.tile.util;

import lombok.experimental.UtilityClass;

import java.io.PrintStream;
import java.util.Map;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* @author CJ ([email protected])
* @since 1.0
*/
@UtilityClass
public class EnvUtil {

/**
* Lookup environment variables.
* @param keyFilter Filter for key
* @return Environment variables
*/
public Map<String, String> lookupKey(Predicate<String> keyFilter) {
return System.getenv()
.entrySet()
.stream()
.filter(entry -> keyFilter.test(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (x, y) -> y));
}

/**
* Lookup environment variables by prefix.
* @param regex Key regex
* @return Environment variables
*/
public Map<String, String> lookupKeyPattern(String regex) {
Pattern p = Pattern.compile(regex);
return lookupKey(key -> p.matcher(key).matches());
}

/**
* Lookup environment variables by prefix.
* @param keyPrefix Key prefix
* @return Environment variables
*/
public Map<String, String> lookupKeyPrefix(String keyPrefix) {
return lookupKey(key -> key.startsWith(keyPrefix));
}

/**
* Dump environment variables by prefix.
* @param keyPrefix Key prefix
* @param out PrintStream
*/
public void dumpKeyPrefix(String keyPrefix, PrintStream out) {
for (Map.Entry<String, String> entry : lookupKeyPrefix(keyPrefix).entrySet()) {
out.println(entry.getKey() + "=" + entry.getValue());
}
}

/**
* Dump environment variables by prefix.
* @param regex Key regex
* @param out PrintStream
*/
public void dumpKeyPattern(String regex, PrintStream out) {
for (Map.Entry<String, String> entry : lookupKeyPattern(regex).entrySet()) {
out.println(entry.getKey() + "=" + entry.getValue());
}
}

}
48 changes: 48 additions & 0 deletions tile-util/src/test/java/com/power4j/tile/util/EnvUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.power4j.tile.util;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Map;

/**
* @author CJ ([email protected])
* @since 1.0
*/
class EnvUtilTest {

private final String VALUE_TILE_TEST_KEY_1 = "TEST";

private final String VALUE_TILE_SYS_KEY_1 = "SYS";

@Test
void lookupKeyPattern() {
Map<String, String> map = EnvUtil.lookupKeyPrefix("^TILE_*$");
Assertions.assertEquals(VALUE_TILE_TEST_KEY_1, map.get("TILE_TEST_KEY_1"));
Assertions.assertEquals(VALUE_TILE_SYS_KEY_1, map.get("TILE_SYS_KEY_1"));
}

@Test
void lookupKeyPrefix() {
Map<String, String> map = EnvUtil.lookupKeyPrefix("TILE_");
Assertions.assertEquals(VALUE_TILE_TEST_KEY_1, map.get("TILE_TEST_KEY_1"));
Assertions.assertEquals(VALUE_TILE_SYS_KEY_1, map.get("TILE_SYS_KEY_1"));
}

}

0 comments on commit 8fdeaed

Please sign in to comment.