Skip to content

Add getProperty(propertyNamesList, defaultValue) #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/main/java/org/spdx/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
Expand Down Expand Up @@ -59,6 +60,8 @@ private Configuration() {
}

/**
* Retrieve the singleton instance of the Configuration class
*
* @return The singleton instance of the Configuration class.
*/
public static Configuration getInstance() {
Expand All @@ -69,6 +72,8 @@ public static Configuration getInstance() {
}

/**
* Retrieve the value of a configuration property by its name
*
* @param propertyName The name of the configuration property to retrieve.
* @return The value of the given property name, or null if it wasn't found.
*/
Expand All @@ -77,6 +82,9 @@ public String getProperty(final String propertyName) {
}

/**
* Retrieve the value of a configuration property by its name;
* return a default value if was not found
*
* @param propertyName The name of the configuration property to retrieve.
* @param defaultValue The default value to return, if the property isn't found.
* @return The value of the given property name, or defaultValue if it wasn't found.
Expand All @@ -85,6 +93,31 @@ public String getProperty(final String propertyName, final String defaultValue)
return System.getProperty(propertyName, properties == null ? defaultValue : properties.getProperty(propertyName, defaultValue));
}

/**
* Retrieve the value of the first found configuration property from a list
* of names; return a default value if none are found
* <p>
* This method checks each property name in the provided list in order.
* If a property name is not found, it moves on to the next one.
* If a property name is found, its value is returned.
* If none of the property names are found, the provided default value is returned.
*
* @param propertyNames An ordered list of configuration property names to retrieve.
* @param defaultValue The default value to return if none of the properties are found.
* @return The value of the first found property name, or {@code defaultValue} if none are found.
*/
public String getProperty(final List<String> propertyNames, final String defaultValue) {
if (propertyNames != null) {
for (String propertyName : propertyNames) {
String value = getProperty(propertyName);
if (value != null) {
return value;
}
}
}
return defaultValue;
}

/**
* Tries to load properties from the CLASSPATH, using the provided filename, ignoring errors
* encountered during the process (e.g., the properties file doesn't exist, etc.).
Expand Down