Skip to content

Commit

Permalink
Feature #2 - New support for property identifier lookup and validatio…
Browse files Browse the repository at this point in the history
…n. This allows fields to reference a data object instead of just a property name as a string. Updated tests. Updated string interpolator for property ids
  • Loading branch information
Carl Dea committed Jun 18, 2024
1 parent 8466d5f commit 0cb4fb8
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 81 deletions.
76 changes: 34 additions & 42 deletions src/main/java/org/carlfx/cognitive/viewmodel/IdSimpleViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,6 @@ public <T> T getValue(Object propId) {
);
}

@Override
public IdSimpleViewModel addProperty(String name, Property property) {
return this.addProperty((Object) name, property);
}
public IdSimpleViewModel addProperty(Object propId, Property property) {
return ifNotPresentAddPropertyId(propId, (propName) -> super.addProperty(propName, property));
}
private void addKeysToPropertyIdMap(PropertyIdentifier propertyIdentifier) {
Map<?, PropertyIdentifier> map = Map.of(
propertyIdentifier, propertyIdentifier,
Expand All @@ -127,13 +120,6 @@ private void addKeysToPropertyIdMap(PropertyIdentifier propertyIdentifier) {
);
getPropertyIdLookupMap().putAll(map);
}
@Override
public IdSimpleViewModel addProperty(String name, String value) {
return this.addProperty((Object) name, value);
}
public IdSimpleViewModel addProperty(Object propId, String value) {
return ifNotPresentAddPropertyId(propId, (propName) -> super.addProperty(propName, value));
}

/**
* Generic way to check property identifiers, if it exists call consumer else call the standard property name.
Expand All @@ -150,16 +136,12 @@ protected IdSimpleViewModel ifPresentOrElse(Object propId,
()-> propName.accept(String.valueOf(propId))); /* else */
return this;
}
protected IdSimpleViewModel ifNotPresentAddPropertyId(Object propId,
protected IdSimpleViewModel ifNotPresentAddPropertyId(PropertyIdentifier propId,
Consumer<String> consumer) {
Optional<PropertyIdentifier> propertyIdentifier = findPropertyIdentifierByKey(propId);
if (propertyIdentifier.isEmpty()) {
if (propId instanceof PropertyIdentifier propertyIdentifier1) {
addKeysToPropertyIdMap(propertyIdentifier1);
consumer.accept(propertyIdentifier1.idToString());
} else {
consumer.accept(String.valueOf(propId));
}
addKeysToPropertyIdMap(propId);
consumer.accept(propId.idToString());
} else {
consumer.accept(propertyIdentifier.get().idToString());
}
Expand All @@ -175,71 +157,81 @@ private <R> R returnIfPresentOrElse(Object propId,
return propName.apply(String.valueOf(propId));
}

@Override
public IdSimpleViewModel addProperty(String name, Property property) {
return super.addProperty(name, property);
}
public IdSimpleViewModel addProperty(PropertyIdentifier propId, Property property) {
return ifNotPresentAddPropertyId(propId, (propName) -> super.addProperty(propName, property));
}
@Override
public IdSimpleViewModel addProperty(String name, String value) {
return super.addProperty(name, value);
}
public IdSimpleViewModel addProperty(PropertyIdentifier propId, String value) {
return ifNotPresentAddPropertyId(propId, (propName) -> super.addProperty(propName, value));
}

@Override
public IdSimpleViewModel addProperty(String name, int value) {
this.addProperty((Object) name, value);
return this;
return super.addProperty(name, value);
}
public IdSimpleViewModel addProperty(Object propId, int value) {
public IdSimpleViewModel addProperty(PropertyIdentifier propId, int value) {
return ifNotPresentAddPropertyId(propId, (propName) -> super.addProperty(propName, value));
}

@Override
public IdSimpleViewModel addProperty(String name, long value) {
this.addProperty((Object) name, value);
return this;
return super.addProperty(name, value);
}
public IdSimpleViewModel addProperty(Object propId, long value) {
public IdSimpleViewModel addProperty(PropertyIdentifier propId, long value) {
return ifNotPresentAddPropertyId(propId, (propName) -> super.addProperty(propName, value));
}

@Override
public IdSimpleViewModel addProperty(String name, float value) {
this.addProperty((Object) name, value);
return this;
return super.addProperty(name, value);
}
public IdSimpleViewModel addProperty(Object propId, float value) {
public IdSimpleViewModel addProperty(PropertyIdentifier propId, float value) {
return ifNotPresentAddPropertyId(propId, (propName) -> super.addProperty(propName, value));
}

@Override
public IdSimpleViewModel addProperty(String name, double value) {
this.addProperty((Object) name, value);
return this;
return super.addProperty(name, value);
}
public IdSimpleViewModel addProperty(Object propId, double value) {
public IdSimpleViewModel addProperty(PropertyIdentifier propId, double value) {
return ifNotPresentAddPropertyId(propId, (propName) -> super.addProperty(propName, value));
}

@Override
public IdSimpleViewModel addProperty(String name, boolean value) {
return this.addProperty((Object)name, value);
return super.addProperty(name, value);
}
public IdSimpleViewModel addProperty(Object propId, boolean value) {
public IdSimpleViewModel addProperty(PropertyIdentifier propId, boolean value) {
return ifNotPresentAddPropertyId(propId, (propName) -> super.addProperty(propName, value));
}

@Override
public IdSimpleViewModel addProperty(String name, Collection value) {
return this.addProperty((Object) name, value);
return super.addProperty(name, value);
}
public IdSimpleViewModel addProperty(Object propId, Collection value) {
public IdSimpleViewModel addProperty(PropertyIdentifier propId, Collection value) {
return ifNotPresentAddPropertyId(propId, (propName) -> super.addProperty(propName, value));
}


@Override
public IdSimpleViewModel addProperty(String name, Collection value, boolean skip) {
return this.addProperty((Object) name, value, skip);
return super.addProperty(name, value, skip);
}
public IdSimpleViewModel addProperty(Object propId, Collection value, boolean skip) {
public IdSimpleViewModel addProperty(PropertyIdentifier propId, Collection value, boolean skip) {
return ifNotPresentAddPropertyId(propId, (propName) -> super.addProperty(propName, value, skip));
}
@Override
public IdSimpleViewModel addProperty(String name, Object value) {
return this.addProperty((Object) name, value);
return super.addProperty(name, value);
}
public IdSimpleViewModel addProperty(Object propId, Object value) {
public IdSimpleViewModel addProperty(PropertyIdentifier propId, Object value) {
return ifNotPresentAddPropertyId(propId, (propName) -> super.addProperty(propName, value));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.Optional;
import java.util.stream.Collectors;

public class IdValidationViewModel extends IdSimpleViewModel implements Validatable {
Expand All @@ -28,8 +28,8 @@ public IdValidationViewModel addProperty(String name, Property property) {
}

@Override
public IdValidationViewModel addProperty(Object name, Property property) {
super.addProperty(name, property);
public IdValidationViewModel addProperty(PropertyIdentifier propertyIdentifier, Property property) {
super.addProperty(propertyIdentifier, property);
return this;
}
/**
Expand All @@ -45,7 +45,7 @@ public IdValidationViewModel addProperty(String name, String value) {
}

@Override
public IdValidationViewModel addProperty(Object name, String value) {
public IdValidationViewModel addProperty(PropertyIdentifier name, String value) {
super.addProperty(name, value);
return this;
}
Expand All @@ -63,7 +63,7 @@ public IdValidationViewModel addProperty(String name, int value) {
}

@Override
public IdValidationViewModel addProperty(Object name, int value) {
public IdValidationViewModel addProperty(PropertyIdentifier name, int value) {
super.addProperty(name, value);
return this;
}
Expand All @@ -80,7 +80,7 @@ public IdValidationViewModel addProperty(String name, long value) {
}

@Override
public IdValidationViewModel addProperty(Object name, long value) {
public IdValidationViewModel addProperty(PropertyIdentifier name, long value) {
super.addProperty(name, value);
return this;
}
Expand All @@ -96,7 +96,7 @@ public IdValidationViewModel addProperty(String name, float value) {
return this;
}
@Override
public IdValidationViewModel addProperty(Object name, float value) {
public IdValidationViewModel addProperty(PropertyIdentifier name, float value) {
super.addProperty(name, value);
return this;
}
Expand All @@ -111,7 +111,7 @@ public IdValidationViewModel addProperty(Object name, float value) {
public IdValidationViewModel addProperty(String name, double value) {
return this.addProperty(name, value);
}
public IdValidationViewModel addProperty(Object name, double value) {
public IdValidationViewModel addProperty(PropertyIdentifier name, double value) {
super.addProperty(name, value);
return this;
}
Expand All @@ -129,7 +129,7 @@ public IdValidationViewModel addProperty(String name, boolean value) {
}

@Override
public IdValidationViewModel addProperty(Object name, boolean value) {
public IdValidationViewModel addProperty(PropertyIdentifier name, boolean value) {
super.addProperty(name, value);
return this;
}
Expand All @@ -146,7 +146,7 @@ public IdValidationViewModel addProperty(String name, Collection value) {
}

@Override
public IdValidationViewModel addProperty(Object name, Collection value) {
public IdValidationViewModel addProperty(PropertyIdentifier name, Collection value) {
super.addProperty(name, value);
return this;
}
Expand All @@ -163,8 +163,8 @@ public IdValidationViewModel addProperty(String name, Object value) {
}

@Override
public IdValidationViewModel addProperty(Object name, Object value) {
super.addProperty(name, value);
public IdValidationViewModel addProperty(PropertyIdentifier propertyIdentifier, Object value) {
super.addProperty(propertyIdentifier, value);
return this;
}

Expand Down Expand Up @@ -255,7 +255,7 @@ public IdValidationViewModel addValidator(String name, String friendlyName, Stri
public <V extends ViewModel> IdValidationViewModel addValidator(Object name, String friendlyName, StringValidator validator) {
ifPresentOrElse(name,
propertyIdentifier -> getValidationManager().createFieldValidator(propertyIdentifier.idToString(), friendlyName, validator),
propName -> getValidationManager().createFieldValidator(String.valueOf(name), friendlyName, validator)
propName -> getValidationManager().createFieldValidator(propName, friendlyName, validator)
);
return this;
}
Expand All @@ -268,7 +268,7 @@ public IdValidationViewModel addValidator(String name, String friendlyName, Bool
public IdValidationViewModel addValidator(Object name, String friendlyName, BooleanValidator validator) {
ifPresentOrElse(name,
propertyIdentifier -> getValidationManager().createFieldValidator(propertyIdentifier.idToString(), friendlyName, validator),
propName -> getValidationManager().createFieldValidator(String.valueOf(name), friendlyName, validator)
propName -> getValidationManager().createFieldValidator(propName, friendlyName, validator)
);
return this;
}
Expand All @@ -280,7 +280,7 @@ public IdValidationViewModel addValidator(String name, String friendlyName, Inte
public IdValidationViewModel addValidator(Object name, String friendlyName, IntegerValidator validator) {
ifPresentOrElse(name,
propertyIdentifier -> getValidationManager().createFieldValidator(propertyIdentifier.idToString(), friendlyName, validator),
propName -> getValidationManager().createFieldValidator(String.valueOf(name), friendlyName, validator)
propName -> getValidationManager().createFieldValidator(propName, friendlyName, validator)
);
return this;
}
Expand All @@ -292,7 +292,7 @@ public IdValidationViewModel addValidator(String name, String friendlyName, Long
public IdValidationViewModel addValidator(Object name, String friendlyName, LongValidator validator) {
ifPresentOrElse(name,
propertyIdentifier -> getValidationManager().createFieldValidator(propertyIdentifier.idToString(), friendlyName, validator),
propName -> getValidationManager().createFieldValidator(String.valueOf(name), friendlyName, validator)
propName -> getValidationManager().createFieldValidator(propName, friendlyName, validator)
);
return this;
}
Expand All @@ -304,7 +304,7 @@ public IdValidationViewModel addValidator(String name, String friendlyName, Floa
public IdValidationViewModel addValidator(Object name, String friendlyName, FloatValidator validator) {
ifPresentOrElse(name,
propertyIdentifier -> getValidationManager().createFieldValidator(propertyIdentifier.idToString(), friendlyName, validator),
propName -> getValidationManager().createFieldValidator(String.valueOf(name), friendlyName, validator)
propName -> getValidationManager().createFieldValidator(propName, friendlyName, validator)
);
return this;
}
Expand All @@ -316,7 +316,7 @@ public IdValidationViewModel addValidator(String name, String friendlyName, Doub
public IdValidationViewModel addValidator(Object name, String friendlyName, DoubleValidator validator) {
ifPresentOrElse(name,
propertyIdentifier -> getValidationManager().createFieldValidator(propertyIdentifier.idToString(), friendlyName, validator),
propName -> getValidationManager().createFieldValidator(String.valueOf(name), friendlyName, validator)
propName -> getValidationManager().createFieldValidator(propName, friendlyName, validator)
);
return this;
}
Expand All @@ -328,7 +328,7 @@ public IdValidationViewModel addValidator(String name, String friendlyName, Obje
public IdValidationViewModel addValidator(Object name, String friendlyName, ObjectValidator validator) {
ifPresentOrElse(name,
propertyIdentifier -> getValidationManager().createFieldValidator(propertyIdentifier.idToString(), friendlyName, validator),
propName -> getValidationManager().createFieldValidator(String.valueOf(name), friendlyName, validator)
propName -> getValidationManager().createFieldValidator(propName, friendlyName, validator)
);
return this;
}
Expand All @@ -340,7 +340,7 @@ public IdValidationViewModel addValidator(String name, String friendlyName, List
public IdValidationViewModel addValidator(Object name, String friendlyName, ListValidator validator) {
ifPresentOrElse(name,
propertyIdentifier -> getValidationManager().createFieldValidator(propertyIdentifier.idToString(), friendlyName, validator),
propName -> getValidationManager().createFieldValidator(String.valueOf(name), friendlyName, validator)
propName -> getValidationManager().createFieldValidator(propName, friendlyName, validator)
);
return this;
}
Expand All @@ -352,7 +352,7 @@ public IdValidationViewModel addValidator(String name, String friendlyName, SetV
public IdValidationViewModel addValidator(Object name, String friendlyName, SetValidator validator) {
ifPresentOrElse(name,
propertyIdentifier -> getValidationManager().createFieldValidator(propertyIdentifier.idToString(), friendlyName, validator),
propName -> getValidationManager().createFieldValidator(String.valueOf(name), friendlyName, validator)
propName -> getValidationManager().createFieldValidator(propName, friendlyName, validator)
);
return this;
}
Expand All @@ -364,15 +364,11 @@ public IdValidationViewModel addValidator(String name, String friendlyName, Cust
public IdValidationViewModel addValidator(Object name, String friendlyName, CustomValidator validator) {
ifPresentOrElse(name,
propertyIdentifier -> getValidationManager().createFieldValidator(propertyIdentifier.idToString(), friendlyName, validator),
propName -> getValidationManager().createFieldValidator(String.valueOf(name), friendlyName, validator)
propName -> getValidationManager().createFieldValidator(propName, friendlyName, validator)
);
return this;
}

// public IdSimpleViewModel addValidator(PropertyIdentifier propId, String friendlyName, Validator validator) {
// getValidationManager().createFieldValidator(propId.idToString(), friendlyName, validator);
// return this;
// }
@Override
public IdValidationViewModel validate() {
validationManager.validate(this);
Expand Down Expand Up @@ -416,6 +412,17 @@ public IdValidationViewModel save() {
return (IdValidationViewModel) super.save();
}

@Override
public String getFriendlyName(String propertName) {
return this.getFriendlyName((Object) propertName);
}
public String getFriendlyName(Object propertName) {
Optional<PropertyIdentifier> propertyIdentifier = findPropertyIdentifierByKey(propertName);
if (propertyIdentifier.isPresent()) {
return Validatable.super.getFriendlyName(propertyIdentifier.get().idToString());
}
return Validatable.super.getFriendlyName(String.valueOf(propertName));
}

/**
* Overridden toString() to output data for debugging.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@

import java.util.function.Function;

/**
* A simple property identifier has a unique id and a user data type object (domain or dto).
* @param <T> The type as the unique id's type.
* @param <U> The user data type object's type.
*/
public class SimplePropertyIdentifier<T, U> implements PropertyIdentifier {
private final String propertyName;
private final U userData;
private final Function<U, T> propertyIdFunctor;

/**
* A constructor of a SimplePropertyIdentifier.
* @param propertyName The property name.
* @param propertyIdFunctor A Function or functional interface by passing in user data and return unique id.
* @param userData The user data or domain object.
*/
public SimplePropertyIdentifier (String propertyName, Function<U, T> propertyIdFunctor, U userData) {
this.propertyName = propertyName;
this.userData = userData;
Expand Down
Loading

0 comments on commit 0cb4fb8

Please sign in to comment.