Skip to content

Commit

Permalink
refacotred ServiceHelper and improved MetaInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Dec 19, 2023
1 parent 30fbded commit 0fc822b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.ServiceLoader;

import io.github.mmm.base.config.ServiceHelper;
import io.github.mmm.base.service.ServiceHelper;

/**
* Class giving {@link #get() global access} to the {@link GlobalExceptionHandler} implementation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* 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.config;
package io.github.mmm.base.service;

import java.util.Collection;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.function.Function;

import io.github.mmm.base.exception.DuplicateObjectException;
import io.github.mmm.base.exception.ObjectNotFoundException;

/**
Expand Down Expand Up @@ -65,9 +68,9 @@ public static final <S> S singleton(ServiceLoader<S> serviceLoader, boolean uniq
* and holds the {@code uses} statement in its {@code module-info}.
* @param services the {@link Collection} where to add the services from the given {@link ServiceLoader}.
*/
public static final <S> void add(ServiceLoader<S> serviceLoader, Collection<S> services) {
public static final <S> void all(ServiceLoader<S> serviceLoader, Collection<S> services) {

add(serviceLoader, services, 1);
all(serviceLoader, services, 1);
}

/**
Expand All @@ -77,7 +80,7 @@ public static final <S> void add(ServiceLoader<S> serviceLoader, Collection<S> s
* @param services the {@link Collection} where to add the services from the given {@link ServiceLoader}.
* @param min the minimum number of services required.
*/
public static final <S> void add(ServiceLoader<S> serviceLoader, Collection<S> services, int min) {
public static final <S> void all(ServiceLoader<S> serviceLoader, Collection<S> services, int min) {

int serviceCount = 0;
for (S service : serviceLoader) {
Expand All @@ -89,4 +92,43 @@ public static final <S> void add(ServiceLoader<S> serviceLoader, Collection<S> s
}
}

/**
* @param <S> type of the service.
* @param <K> type of the {@link Map} key.
* @param serviceLoader the {@link ServiceLoader} that has to be provided from the module declaring the service API
* and holds the {@code uses} statement in its {@code module-info}.
* @param services the {@link Collection} where to add the services from the given {@link ServiceLoader}.
* @param keyFunction the {@link Function} to get the {@link Map} key from the service.
*/
public static final <S, K> void all(ServiceLoader<S> serviceLoader, Map<K, S> services, Function<S, K> keyFunction) {

all(serviceLoader, services, keyFunction, 1);
}

/**
* @param <S> type of the service.
* @param <K> type of the {@link Map} key.
* @param serviceLoader the {@link ServiceLoader} that has to be provided from the module declaring the service API
* and holds the {@code uses} statement in its {@code module-info}.
* @param services the {@link Collection} where to add the services from the given {@link ServiceLoader}.
* @param keyFunction the {@link Function} to get the {@link Map} key from the service.
* @param min the minimum number of services required.
*/
public static final <S, K> void all(ServiceLoader<S> serviceLoader, Map<K, S> services, Function<S, K> keyFunction,
int min) {

int serviceCount = 0;
for (S service : serviceLoader) {
K key = keyFunction.apply(service);
S duplicate = services.put(key, service);
if (duplicate != null) {
throw new DuplicateObjectException(duplicate, key, service);
}
serviceCount++;
}
if (serviceCount < min) {
throw new IllegalStateException("Required at least " + min + " service(s) for " + serviceLoader);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import java.util.ServiceLoader;

import io.github.mmm.base.config.ServiceHelper;
import io.github.mmm.base.service.ServiceHelper;
import io.github.mmm.base.temporal.TemporalConverter;

/**
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

exports io.github.mmm.base.compare;

exports io.github.mmm.base.config;

exports io.github.mmm.base.exception;

exports io.github.mmm.base.filter;
Expand All @@ -49,6 +47,8 @@

exports io.github.mmm.base.range;

exports io.github.mmm.base.service;

exports io.github.mmm.base.sort;

exports io.github.mmm.base.temporal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,7 @@ default String get(boolean inherit, String key, String defaultValue) {
* 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;
}
String get(boolean inherit, boolean required, String key);

/**
* @param key the key of the requested meta-information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Set;
import java.util.TreeSet;

import io.github.mmm.base.exception.ObjectNotFoundException;
import io.github.mmm.base.metainfo.MetaInfo;
import io.github.mmm.base.metainfo.MetaInfos;

Expand Down Expand Up @@ -48,6 +49,16 @@ public String get(boolean inherit, String key) {
return getInternal(inherit, key, null);
}

@Override
public String get(boolean inherit, boolean required, String key) {

String value = get(inherit, key);
if (value == null) {
throw new ObjectNotFoundException("MetaInfo-value", qualifyKey(key));
}
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ public void testWithPrefix() {
assertThat(metaInfo.get("version")).isEqualTo("1.0");
assertThat(toString(metaInfo)).isEqualTo("{name=Name, version=1.0}");
assertThat(metaInfo.getParent()).isNull();
try {
metaInfo.getRequired("key2");
failBecauseExceptionWasNotThrown(ObjectNotFoundException.class);
} catch (ObjectNotFoundException e) {
assertThat(e.getNlsMessage().getMessage()).isEqualTo("Could not find MetaInfo-value for key 'prefix.key2'.");
}

// and act
map = new HashMap<>(map);
map.put("version", "1.1");
Expand Down

0 comments on commit 0fc822b

Please sign in to comment.