Skip to content

Commit

Permalink
allow initialize the recipe multiple times (#3696)
Browse files Browse the repository at this point in the history
* allow initialize the recipe multiple times to properly load the recipes from YamlResourceLoader

* fixed test

* fixed lazy validation
  • Loading branch information
Joan Viladrosa authored Nov 21, 2023
1 parent c83adee commit 04c79ff
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ public boolean causesAnotherCycle() {
private final List<Recipe> preconditions = new ArrayList<>();

public void addPrecondition(Recipe recipe) {
preconditions.add(recipe);
uninitializedPreconditions.add(recipe);
}

@JsonIgnore
private Validated<Object> validation = Validated.test("initialization",
"initialize(..) must be called on DeclarativeRecipe prior to use.",
this, r -> uninitializedRecipes.isEmpty());
private Validated<Object> validation = Validated.none();
@JsonIgnore
private Validated<Object> initValidation = null;

@Override
public Duration getEstimatedEffortPerOccurrence() {
Expand All @@ -85,11 +85,13 @@ public Duration getEstimatedEffortPerOccurrence() {
}

public void initialize(Collection<Recipe> availableRecipes, Map<String, List<Contributor>> recipeToContributors) {
initValidation = Validated.none();
initialize(uninitializedRecipes, recipeList, availableRecipes, recipeToContributors);
initialize(uninitializedPreconditions, preconditions, availableRecipes, recipeToContributors);
}

private void initialize(List<Recipe> uninitialized, List<Recipe> initialized, Collection<Recipe> availableRecipes, Map<String, List<Contributor>> recipeToContributors) {
initialized.clear();
for (int i = 0; i < uninitialized.size(); i++) {
Recipe recipe = uninitialized.get(i);
if (recipe instanceof LazyLoadedRecipe) {
Expand All @@ -103,7 +105,7 @@ private void initialize(List<Recipe> uninitialized, List<Recipe> initialized, Co
}
initialized.add(subRecipe);
} else {
validation = validation.and(
initValidation = initValidation.and(
invalid(name + ".recipeList" +
"[" + i + "] (in " + source + ")",
recipeFqn,
Expand All @@ -118,7 +120,6 @@ private void initialize(List<Recipe> uninitialized, List<Recipe> initialized, Co
initialized.add(recipe);
}
}
uninitialized.clear();
}

@Value
Expand Down Expand Up @@ -304,7 +305,11 @@ public void addValidation(Validated<Object> validated) {

@Override
public Validated<Object> validate() {
return validation;
return Validated.<Object>test("initialization",
"initialize(..) must be called on DeclarativeRecipe prior to use.",
this, r -> initValidation != null)
.and(validation)
.and(initValidation);
}

@Value
Expand Down

0 comments on commit 04c79ff

Please sign in to comment.