Skip to main content
The PropertyKey class provides a unified way to access configuration values from both environment variables and Java system properties, with automatic camelCase to CONSTANT_CASE conversion.

Package

me.hsgamer.mcreleaser.core.property.PropertyKey

Class Declaration

public class PropertyKey {
    private final String key;
    private final String env;
}

Constructors

PropertyKey(String key, String env)

Creates a PropertyKey with explicit property key and environment variable names.
key
String
required
The system property key name
env
String
required
The environment variable name
PropertyKey token = new PropertyKey("github.token", "GITHUB_TOKEN");

PropertyKey(String key)

Creates a PropertyKey with automatic environment variable name conversion from camelCase to CONSTANT_CASE.
key
String
required
The system property key name (camelCase)
// Automatically creates env var "GITHUB_TOKEN" from "githubToken"
PropertyKey token = new PropertyKey("githubToken");

Methods

getKey

Returns the system property key name.
key
String
The system property key
String key = propertyKey.getKey(); // "githubToken"

getEnv

Returns the environment variable name.
env
String
The environment variable name
String env = propertyKey.getEnv(); // "GITHUB_TOKEN"

getValue

Retrieves the property value, checking environment variables first, then system properties.
value
String
The property value, or null if not found
String value = propertyKey.getValue();

getValue(String defaultValue)

Retrieves the property value with a fallback default.
defaultValue
String
required
The default value to return if the property is not found
value
String
The property value, or the default value if not found
String version = versionKey.getValue("1.0.0");

setValue(String value)

Sets the property value as a system property.
value
String
required
The value to set
propertyKey.setValue("my-value");

setValue(Object value)

Sets the property value from any object using toString().
value
Object
required
The object to convert to a string and set
propertyKey.setValue(123);
propertyKey.setValue(true);

isPresent

Checks if the property has a value.
present
boolean
True if the property value is not null
if (propertyKey.isPresent()) {
    // Use the value
}

isAbsent

Checks if the property has no value.
absent
boolean
True if the property value is null
if (propertyKey.isAbsent()) {
    logger.warn("Property not configured");
}

asNumber

Parses the property value as a number.
defaultValue
Number
required
The default value to return if parsing fails or property is absent
number
Number
The parsed number, or the default value
Number timeout = timeoutKey.asNumber(5000);

asBoolean

Parses the property value as a boolean.
defaultValue
boolean
required
The default value to return if property is absent
boolean
boolean
The parsed boolean, or the default value
boolean isDraft = draftKey.asBoolean(false);

CamelCase to CONSTANT_CASE Conversion

The automatic conversion transforms property keys:
"githubToken""GITHUB_TOKEN"
"versionType""VERSION_TYPE"
"gameVersions""GAME_VERSIONS"
"announceMissingKey""ANNOUNCE_MISSING_KEY"

Property Resolution Order

  1. Environment variable (e.g., GITHUB_TOKEN)
  2. System property (e.g., github.token)
  3. Default value (if provided)
  4. null (if no default)

Example Usage

// Define property keys
public interface MyAppConfig {
    PropertyKey API_KEY = new PropertyKey("apiKey");
    PropertyKey TIMEOUT = new PropertyKey("timeout");
    PropertyKey DEBUG = new PropertyKey("debug");
}

// Use in application
String apiKey = MyAppConfig.API_KEY.getValue();
Number timeout = MyAppConfig.TIMEOUT.asNumber(3000);
boolean debug = MyAppConfig.DEBUG.asBoolean(false);

if (MyAppConfig.API_KEY.isAbsent()) {
    throw new IllegalStateException("API key required");
}

Build docs developers (and LLMs) love