Skip to content

Commit

Permalink
Create Env utility class (#48)
Browse files Browse the repository at this point in the history
* Add Env utility class

Fixes #47
  • Loading branch information
ksclarke committed Oct 18, 2022
1 parent 3c5326f commit 7e1162d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/main/java/info/freelibrary/util/Env.java
@@ -0,0 +1,41 @@

package info.freelibrary.util;

import java.util.Objects;

/**
* An extension to the Java environment properties interface.
*/
public final class Env {

/**
* Creates a new environment object.
*/
private Env() {
// This is intentionally left empty.
}

/**
* Gets an environmental property value, optionally falling back to the supplied alternative.
*
* @param aPropertyName An environmental property name
* @param aFallbackValue A fallback value used if the required property isn't set
* @return The environmental property value or its supplied fallback value
*/
public static String get(final String aPropertyName, final String aFallbackValue) {
final String envProperty = StringUtils.trimToNull(System.getenv(aPropertyName));
return envProperty == null ? aFallbackValue : envProperty;
}

/**
* Gets an environmental property value or throws a NullPointerException if that property isn't found.
*
* @param aPropertyName An environmental property name
* @param aMessage An exception message used if property is not found
* @return The environmental property value
* @throws NullPointerException If the requested property name isn't found in the environment
*/
public static String getOrFail(final String aPropertyName, final String aMessage) {
return Objects.requireNonNull(StringUtils.trimToNull(System.getenv(aPropertyName)), aMessage);
}
}
5 changes: 5 additions & 0 deletions src/main/java/info/freelibrary/util/warnings/PMD.java
Expand Up @@ -18,6 +18,11 @@ public final class PMD {
@SuppressWarnings("PMD.LongVariable")
public static final String ABSTRACT_CLASS_WITHOUT_ABSTRACT_METHOD = "PMD.AbstractClassWithoutAbstractMethod";

/**
* Cf. https://pmd.github.io/latest/pmd_rules_java_design.html#avoidthrowingnullpointerexception
*/
public static final String AVOID_THROWING_NULLPOINTEREXCEPTION = "AvoidThrowingNullPointerException";

/**
* Cf. https://pmd.github.io/latest/pmd_rules_java_design.html#avoiddeeplynestedifstmts
*/
Expand Down

0 comments on commit 7e1162d

Please sign in to comment.