-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathFactoryProvider.java
More file actions
29 lines (21 loc) · 1.04 KB
/
FactoryProvider.java
File metadata and controls
29 lines (21 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package solutions.bellatrix.data.configuration;
import solutions.bellatrix.core.utilities.SingletonFactory;
import solutions.bellatrix.data.http.contracts.EntityFactory;
import solutions.bellatrix.data.http.infrastructure.Entity;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
public enum FactoryProvider {
INSTANCE;
private final Map<Class<? extends Entity>, Class<? extends EntityFactory>> factories = new ConcurrentHashMap<>();
public <T extends Entity> void register(Class<T> entityClass, Class<? extends EntityFactory<T>> factoryClass) {
factories.put(entityClass, factoryClass);
}
public <T extends Entity> EntityFactory<T> get(Class<T> entityClass) {
var factoryClassType = factories.get(entityClass);
if (Objects.isNull(factoryClassType)) {
throw new IllegalArgumentException("No factory registered for entity class: " + entityClass.getName());
}
return (EntityFactory<T>)SingletonFactory.getInstance(factoryClassType);
}
}