Removed old functional interface and added new isValid method
This commit is contained in:
@@ -19,10 +19,10 @@ package org.fuin.dddcqrs4jexample.shared.app;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.fuin.ddd4j.ddd.EntityId;
|
||||
import org.fuin.ddd4j.ddd.EntityIdFactory;
|
||||
import org.fuin.ddd4j.ddd.SingleEntityIdFactory;
|
||||
import org.fuin.dddcqrs4jexample.shared.domain.PersonId;
|
||||
|
||||
/**
|
||||
@@ -30,29 +30,42 @@ import org.fuin.dddcqrs4jexample.shared.domain.PersonId;
|
||||
*/
|
||||
public final class SharedEntityIdFactory implements EntityIdFactory {
|
||||
|
||||
private Map<String, SingleEntityIdFactory> map;
|
||||
private Map<String, Function<String, EntityId>> valueOfMap;
|
||||
|
||||
private Map<String, Function<String, Boolean>> isValidMap;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
*/
|
||||
public SharedEntityIdFactory() {
|
||||
super();
|
||||
map = new HashMap<String, SingleEntityIdFactory>();
|
||||
map.put(PersonId.TYPE.asString(), PersonId::valueOf);
|
||||
valueOfMap = new HashMap<>();
|
||||
isValidMap = new HashMap<>();
|
||||
valueOfMap.put(PersonId.TYPE.asString(), PersonId::valueOf);
|
||||
isValidMap.put(PersonId.TYPE.asString(), PersonId::isValid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityId createEntityId(final String type, final String id) {
|
||||
final SingleEntityIdFactory factory = map.get(type);
|
||||
final Function<String, EntityId> factory = valueOfMap.get(type);
|
||||
if (factory == null) {
|
||||
throw new IllegalArgumentException("Unknown type: " + type);
|
||||
}
|
||||
return factory.createEntityId(id);
|
||||
return factory.apply(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsType(final String type) {
|
||||
return map.containsKey(type);
|
||||
return valueOfMap.containsKey(type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(String type, String id) {
|
||||
final Function<String, Boolean> func = isValidMap.get(type);
|
||||
if (func == null) {
|
||||
return false;
|
||||
}
|
||||
return func.apply(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -55,17 +55,33 @@ public final class PersonId extends AggregateRootUuid {
|
||||
/**
|
||||
* Constructor with all data.
|
||||
*
|
||||
* @param value Persistent value.
|
||||
* @param value
|
||||
* Persistent value.
|
||||
*/
|
||||
public PersonId(@NotNull final UUID value) {
|
||||
super(PersonId.TYPE, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies if the given string can be converted into a Person ID.
|
||||
*
|
||||
* @param value
|
||||
* String with valid UUID string. A <code>null</code> value ris also valid.
|
||||
*
|
||||
* @return {@literal true} if the string is a valid UUID.
|
||||
*/
|
||||
public static boolean isValid(final String value) {
|
||||
if (value == null) {
|
||||
return true;
|
||||
}
|
||||
return AggregateRootUuid.isValid(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a given string and returns a new instance of PersonId.
|
||||
*
|
||||
* @param value String with valid UUID to convert. A <code>null</code> value
|
||||
* returns <code>null</code>.
|
||||
* @param value
|
||||
* String with valid UUID to convert. A <code>null</code> value returns <code>null</code>.
|
||||
*
|
||||
* @return Converted value.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user