Skip to content

Commit

Permalink
renamed config to variable
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Nov 16, 2023
1 parent 4963dcc commit 2403c85
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 252 deletions.
86 changes: 0 additions & 86 deletions core/src/main/java/io/github/mmm/base/config/ConfigMap.java

This file was deleted.

94 changes: 0 additions & 94 deletions core/src/main/java/io/github/mmm/base/config/ConfigOption.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.base.variable;

import java.util.Map;

/**
* Definition of a variable for a {@link VariableMap}. It is a simple container for the {@link #getName() name} of the
* variable with its {@link #getDefaultValue()} and its {@link #getType() type} making the
* {@link VariableMap#get(VariableDefinition) access of the value} type-safe.
*
* @param <T> type of the value for this option. See {@link #getType()}.
* @since 1.0.0
*/
public class VariableDefinition<T> {

private final String name;

private final Class<T> type;

private final T defaultValue;

/**
* The constructor.
*
* @param name the {@link #getName() name}.
* @param type the {@link #getType() type}.
*/
public VariableDefinition(String name, Class<T> type) {

this(name, type, null);
}

/**
* The constructor.
*
* @param name the {@link #getName() name}.
* @param defaultValue the {@link #getDefaultValue() default value}.
*/
public VariableDefinition(String name, T defaultValue) {

this(name, null, defaultValue);
}

/**
* The constructor.
*
* @param name the {@link #getName() name}.
* @param type the {@link #getType() type}.
* @param defaultValue the {@link #getDefaultValue() default value}.
*/
@SuppressWarnings("unchecked")
public VariableDefinition(String name, Class<T> type, T defaultValue) {

super();
this.name = name;
if ((type == null) && (defaultValue != null)) {
this.type = (Class<T>) defaultValue.getClass();
} else {
this.type = type;
}
this.defaultValue = defaultValue;
}

/**
* @return the name of the variable used as {@link Map#containsKey(Object) key} to
* {@link VariableMap#get(VariableDefinition) get its value}.
*/
public String getName() {

return this.name;
}

/**
* @return the {@link Class} reflecting the value of this variable.
*/
public Class<T> getType() {

return this.type;
}

/**
* @return the optional default value to use if the variable is not explicitly defined.
*/
public T getDefaultValue() {

return this.defaultValue;
}

@Override
public String toString() {

return this.name;
}

}
114 changes: 114 additions & 0 deletions core/src/main/java/io/github/mmm/base/variable/VariableMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.base.variable;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
* Simple wrapper for a {@link Map} of {@link VariableDefinition variables}.
*
* @since 1.0.0
*/
public class VariableMap {

private final Map<String, Object> map;

private final Map<String, Object> unmodifyableMap;

/**
* The constructor.
*/
public VariableMap() {

this(new HashMap<>());
}

/**
* The constructor.
*
* @param map the raw {@link Map} with the configuration values.
*/
public VariableMap(Map<String, Object> map) {

super();
this.map = map;
this.unmodifyableMap = Collections.unmodifiableMap(map);
}

/**
* @param <T> type of config value.
* @param variable the {@link VariableDefinition}.
* @return the value of the given {@link VariableDefinition}.
*/
@SuppressWarnings("unchecked")
public <T> T get(VariableDefinition<T> variable) {

if (variable == null) {
return null;
}
String key = variable.getName();
T value = (T) this.map.get(key);
if ((value == null) && !this.map.containsKey(key)) {
value = variable.getDefaultValue();
}
return value;
}

/**
* @param variable the {@link VariableDefinition}.
* @return the value of the given {@link VariableDefinition}.
*/
public boolean getBoolean(VariableDefinition<Boolean> variable) {

Boolean value = get(variable);
if (value != null) {
return value.booleanValue();
}
throw new IllegalStateException("The variable " + variable.getName() + " is undefined and has no default value.");
}

/**
* @param variable the {@link VariableDefinition}.
* @param defaultValue the additional default value returned if the variable is undefined and the given
* {@link VariableDefinition} has no {@link VariableDefinition#getDefaultValue() default value}.
* @return the value of the given {@link VariableDefinition}.
*/
public boolean getBoolean(VariableDefinition<Boolean> variable, boolean defaultValue) {

Boolean value = get(variable);
if (value != null) {
return value.booleanValue();
}
return defaultValue;
}

/**
* @param <T> type of config value.
* @param key the {@link VariableDefinition}.
* @param value the new configuration value for the given {@link VariableDefinition}.
* @return the old configuration value that was previously associated with the given {@link VariableDefinition}. Will
* be {@code null} if no value was configured before.
*/
@SuppressWarnings("unchecked")
public <T> T set(VariableDefinition<T> key, T value) {

return (T) this.map.put(key.getName(), value);
}

/**
* @return the raw {@link Map} with the configuration.
*/
public Map<String, Object> getMap() {

return this.unmodifyableMap;
}

@Override
public String toString() {

return this.map.toString();
}

}
Loading

0 comments on commit 2403c85

Please sign in to comment.