Skip to content

Commit

Permalink
ServiceHelper refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Dec 19, 2023
1 parent 08c9dea commit 1b6237a
Showing 1 changed file with 8 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import io.github.mmm.base.collection.ReadOnlyIterator;
import io.github.mmm.base.exception.ObjectNotFoundException;
import io.github.mmm.base.service.ServiceHelper;
import io.github.mmm.entity.Entity;
import io.github.mmm.entity.repository.EntityRepository;
import io.github.mmm.entity.repository.EntityRepositoryManager;
Expand All @@ -18,40 +19,35 @@
*
* @since 1.0.0
*/
@SuppressWarnings("rawtypes")
public class EntityRepositoryManagerImpl implements EntityRepositoryManager {

/** The singleton instance. */
public static final EntityRepositoryManagerImpl INSTANCE = new EntityRepositoryManagerImpl();

private final Map<Class, EntityRepository<?>> repositoryMap;
private final Map<Class<?>, EntityRepository<?>> repositoryMap;

/**
* The constructor.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public EntityRepositoryManagerImpl() {

super();
this.repositoryMap = new HashMap<>();
ServiceLoader<EntityRepository> serviceLoader = ServiceLoader.load(EntityRepository.class);
for (EntityRepository<?> repository : serviceLoader) {
registerRepository(repository);
}
ServiceLoader<EntityRepository<?>> serviceLoader = (ServiceLoader) ServiceLoader.load(EntityRepository.class);
ServiceHelper.all(serviceLoader, this.repositoryMap, EntityRepositoryManagerImpl::getEntityClass);
}

/**
* @param repository the {@link EntityRepository} to register.
*/
private void registerRepository(EntityRepository<?> repository) {
private static Class<?> getEntityClass(EntityRepository<?> repository) {

Class<?> entityClass = repository.getEntityClass();
if (entityClass == null) {
throw new ObjectNotFoundException("EntityClass", repository);
}
this.repositoryMap.put(entityClass, repository);
return entityClass;
}

@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public <E extends Entity> EntityRepository<E> getRepository(Class<E> entityClass) {

Expand Down

0 comments on commit 1b6237a

Please sign in to comment.