Skip to content

Commit

Permalink
meta-info improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Dec 18, 2023
1 parent d709577 commit 30fbded
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ private static String createMessage(Object object, Object key) {
sb.append(object);
if (key != null) {
sb.append(" for key ");
sb.append('\'');
sb.append(key);
sb.append('\'');
}
sb.append(".");
return sb.toString();
Expand Down
140 changes: 139 additions & 1 deletion metainfo/src/main/java/io/github/mmm/base/metainfo/MetaInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
package io.github.mmm.base.metainfo;

import java.lang.reflect.AnnotatedElement;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import io.github.mmm.base.exception.ObjectNotFoundException;
import io.github.mmm.base.metainfo.impl.MetaInfoEmpty;
import io.github.mmm.base.metainfo.impl.MetaInfoValues;

Expand Down Expand Up @@ -55,6 +57,45 @@ default String get(String key) {
return get(true, key);
}

/**
* @param key the key of the requested meta-information.
* @return the value of the meta-information for the given {@code key}. Will be {@code null} if no value is defined
* for the given {@code key}.
* @throws ObjectNotFoundException if the specified value is undefined.
*/
default String getRequired(String key) {

return get(true, true, key);
}

/**
* @param key the key of the requested meta-information.
* @param defaultValue the default value returned if the actual value is undefined.
* @return the value of the meta-information for the given {@code key}. Will be {@code null} if no value is defined
* for the given {@code key}.
*/
default String get(String key, String defaultValue) {

return get(true, key, defaultValue);
}

/**
* @param inherit - {@code true} to inherit meta-information from the {@link #getParent() parent}, {@code false} to
* only return plain meta-information defined in this {@link MetaInfo} itself.
* @param key the key of the requested meta-information.
* @param defaultValue the default value returned if the actual value is undefined.
* @return the value of the meta-information for the given {@code key}. Will be {@code null} if no value is defined
* for the given {@code key}.
*/
default String get(boolean inherit, String key, String defaultValue) {

String value = get(inherit, key);
if (value == null) {
value = defaultValue;
}
return value;
}

/**
* @param inherit - {@code true} to inherit meta-information from the {@link #getParent() parent}, {@code false} to
* only return plain meta-information defined in this {@link MetaInfo} itself.
Expand All @@ -64,6 +105,25 @@ default String get(String key) {
*/
String get(boolean inherit, String key);

/**
* @param inherit - {@code true} to inherit meta-information from the {@link #getParent() parent}, {@code false} to
* only return plain meta-information defined in this {@link MetaInfo} itself.
* @param required - {@code true} if the requested value is required and an exception shall be raised if it is
* undefined, {@code false} otherwise (return {@code null} if undefined).
* @param key the key of the requested meta-information.
* @return the value of the meta-information for the given {@code key}. Will be {@code null} if no value is defined
* for the given {@code key}.
* @throws ObjectNotFoundException if the specified value is undefined and {@code required} was {@code true}.
*/
default String get(boolean inherit, boolean required, String key) {

String value = get(inherit, key);
if (value == null) {
throw new ObjectNotFoundException("MetaInfo-value", key);
}
return value;
}

/**
* @param key the key of the requested meta-information.
* @return the value of the meta-information for the given {@code key} parsed as {@link Long}. Will be {@code null} if
Expand All @@ -75,6 +135,17 @@ default Long getAsLong(String key) {
return getAsLong(true, key);
}

/**
* @param key the key of the requested meta-information.
* @return the value of the meta-information for the given {@code key} parsed as {@link Long}.
* @throws ObjectNotFoundException if the specified value is undefined.
* @throws IllegalArgumentException if the value cannot be parsed as {@link Long}.
*/
default long getAsLongRequired(String key) {

return getAsLong(true, true, key).longValue();
}

/**
* @param inherit - {@code true} to inherit meta-information from the {@link #getParent() parent}, {@code false} to
* only return plain meta-information defined in this {@link MetaInfo} itself.
Expand All @@ -85,7 +156,23 @@ default Long getAsLong(String key) {
*/
default Long getAsLong(boolean inherit, String key) {

String value = get(inherit, key);
return getAsLong(inherit, false, key);
}

/**
* @param inherit - {@code true} to inherit meta-information from the {@link #getParent() parent}, {@code false} to
* only return plain meta-information defined in this {@link MetaInfo} itself.
* @param required - {@code true} if the requested value is required and an exception shall be raised if it is
* undefined, {@code false} otherwise (return {@code null} if undefined).
* @param key the key of the requested meta-information.
* @return the value of the meta-information for the given {@code key} parsed as {@link Long}. Will be {@code null} if
* no value is defined for the given {@code key}.
* @throws ObjectNotFoundException if the specified value is undefined and {@code required} was {@code true}.
* @throws IllegalArgumentException if the value cannot be parsed as {@link Long}.
*/
default Long getAsLong(boolean inherit, boolean required, String key) {

String value = get(inherit, required, key);
if (value == null) {
return null;
}
Expand Down Expand Up @@ -139,14 +226,41 @@ default Boolean getAsBoolean(String key) {

/**
* @param key the key of the requested meta-information.
* @return the value of the meta-information for the given {@code key} parsed as {@link Boolean}.
* @throws ObjectNotFoundException if the specified value is undefined.
* @throws IllegalArgumentException if the value cannot be parsed as {@link Boolean}.
*/
default boolean getAsBooleanRequired(String key) {

return getAsBoolean(true, true, key).booleanValue();
}

/**
* @param inherit - {@code true} to inherit meta-information from the {@link #getParent() parent}, {@code false} to
* only return plain meta-information defined in this {@link MetaInfo} itself.
* @param key the key of the requested meta-information.
* @return the value of the meta-information for the given {@code key} parsed as {@link Boolean}. Will be {@code null}
* if no value is defined for the given {@code key}.
* @throws IllegalArgumentException if the value cannot be parsed as {@link Boolean}.
*/
default Boolean getAsBoolean(boolean inherit, String key) {

return getAsBoolean(inherit, false, key);
}

/**
* @param inherit - {@code true} to inherit meta-information from the {@link #getParent() parent}, {@code false} to
* only return plain meta-information defined in this {@link MetaInfo} itself.
* @param required - {@code true} if the requested value is required and an exception shall be raised if it is
* undefined, {@code false} otherwise (return {@code null} if undefined).
* @param key the key of the requested meta-information.
* @return the value of the meta-information for the given {@code key} parsed as {@link Boolean}. Will be {@code null}
* if no value is defined for the given {@code key}.
* @throws ObjectNotFoundException if the specified value is undefined and {@code required} was {@code true}.
* @throws IllegalArgumentException if the value cannot be parsed as {@link Boolean}.
*/
default Boolean getAsBoolean(boolean inherit, boolean required, String key) {

String value = get(inherit, key);
if (value == null) {
return null;
Expand Down Expand Up @@ -302,6 +416,30 @@ default MetaInfo with(AnnotatedElement annotatedElement) {
*/
MetaInfo with(String keyPrefix);

/**
* @return a new {@link Properties} instance with all values from this {@link MetaInfo}.
*/
default Properties asProperties() {

Properties properties = new Properties();
for (String key : this) {
properties.setProperty(key, get(key));
}
return properties;
}

/**
* @return a new {@link Map} instance with all values from this {@link MetaInfo}.
*/
default Map<String, String> asMap() {

Map<String, String> map = new HashMap<>(size());
for (String key : this) {
map.put(key, get(key));
}
return map;
}

/**
* @return an instance of {@link MetaInfo} that is {@link #isEmpty() empty}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected MetaInfoInherited asParent() {
@Override
public String get(boolean inherit, String key) {

return get(inherit, key, null);
return getInternal(inherit, key, null);
}

/**
Expand All @@ -56,7 +56,7 @@ public String get(boolean inherit, String key) {
* @return the value of the meta-information for the given {@code key}. Will be {@code null} if no value is defined
* for the given {@code key}.
*/
public String get(boolean inherit, String key, MetaInfo stop) {
public String getInternal(boolean inherit, String key, MetaInfo stop) {

String value = getPlain(inherit, qualifyKey(key));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected String findNext() {
while (this.iterator.hasNext()) {
String key = this.currentMetaInfo.unqualifyKey(this.iterator.next());
if ((key != null) && ((this.currentMetaInfo == this.metaInfo)
|| (this.metaInfo.get(true, key, this.currentMetaInfo) == null))) {
|| (this.metaInfo.getInternal(true, key, this.currentMetaInfo) == null))) {
return key;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

import io.github.mmm.base.exception.ObjectNotFoundException;
import io.github.mmm.base.metainfo.MetaInfo;
import io.github.mmm.base.metainfo.MetaInfos;
import io.github.mmm.base.metainfo.impl.AbstractMetaInfo;
Expand Down Expand Up @@ -39,6 +40,25 @@ public void testEmpty() {
assertThat(toString(metaInfo)).isEqualTo("{}");
}

/** Test of {@link MetaInfo#getRequired(String)}. */
@Test
public void testGetRequired() {

// arrange
// act
MetaInfo metaInfo = MetaInfo.empty().with(Map.of("key", "value", "long", "123456789012345567", "boolean", "true"));
// assert
assertThat(metaInfo.getRequired("key")).isEqualTo("value");
assertThat(metaInfo.getAsLongRequired("long")).isEqualTo(123456789012345567L);
assertThat(metaInfo.getAsBooleanRequired("boolean")).isTrue();
try {
metaInfo.getRequired("key2");
failBecauseExceptionWasNotThrown(ObjectNotFoundException.class);
} catch (ObjectNotFoundException e) {
assertThat(e).hasMessageContaining("Could not find MetaInfo-value for key 'key2'.");
}
}

/** Test of {@link MetaInfo#with(String, String)}. */
@Test
public void testWithSingle() {
Expand Down

0 comments on commit 30fbded

Please sign in to comment.