com.memority.toolkit.rule.api.ChoicesRuleResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of toolkit-rule-api Show documentation
Show all versions of toolkit-rule-api Show documentation
This artifact provides the API classes that are necessary to implement the contracts of Memority configuration Rules.
/*
* Copyright (c) 2016-2023 Memority. All Rights Reserved.
*
* This file is part of Memority Toolkit API , a Memority project.
*
* This file is released under the Memority Public Artifacts End-User License Agreement,
* see
* Unauthorized copying of this file, via any medium is strictly prohibited.
*/
package com.memority.toolkit.rule.api;
import lombok.Getter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.*;
import static java.util.Objects.requireNonNull;
/**
* The result type of a ChoicesRule. This is composed of either:
*
* - First representation
*
* - An i18n prefix, to be used for translations of the possible choices
* - A list of possible choice values
*
*
* - Second representation
*
* - An i18n key, to be used for translations of the possible choices
* - A map of possible choice values with their corresponding i18nParams and i18nValues
*
*
*
*
* In the 1st representation, the i18n labels are composed through the following convention:
*
* <i18nPrefix>.<choiceValue>.label
*
* No variables can be used in the translations
*
* In the 2nd representation, all the labels are computed from the given i18n key.
* Only the parameters allow to produce a distinct translation for each choice values.
* For example, with the following map
* { value1: [i18nParam1: 'foo', i18nParam2: 'bar'], value2: [i18nParam1: 'baz', i18nParam2: 'qux'] }
* the translation could be done with the following key
* {value} - Param 1 is '{i18nParam1}' and Param2 is '{i18nParam2}'
* which results for the first value to value1 - Param 1 is 'foo' and Param2 is 'bar'
*/
@Getter
public class ChoicesRuleResult {
private final String i18nPrefix;
private final List choices;
private final String i18nKey;
private final LinkedHashMap> choicesWithParams;
private ChoicesRuleResult(String i18nPrefix, List choices) {
this.i18nPrefix = i18nPrefix;
this.choices = choices;
this.i18nKey = null;
this.choicesWithParams = new LinkedHashMap<>();
}
private ChoicesRuleResult(String i18nKey, LinkedHashMap> choicesWithParams) {
this.i18nPrefix = null;
this.choices = Collections.emptyList();
this.i18nKey = i18nKey;
this.choicesWithParams = choicesWithParams;
}
public static ChoicesRuleResult of(String i18nPrefix, List choices) {
requireNonNull(i18nPrefix, "The i18n prefix cannot be NULL.");
requireNonNull(choices, "The choices list cannot be NULL.");
return new ChoicesRuleResult(i18nPrefix, choices);
}
public static ChoicesRuleResult of(String i18nPrefix, String... choices) {
return of(i18nPrefix, Arrays.asList(choices));
}
public static ChoicesRuleResult ofFixed(String i18nKey, LinkedHashMap> choicesWithParams) {
requireNonNull(i18nKey, "The i18n key cannot be NULL.");
requireNonNull(choicesWithParams, "The choices cannot be NULL.");
return new ChoicesRuleResult(i18nKey, choicesWithParams);
}
@JsonIgnore
public List getAvailableChoices() {
return !choicesWithParams.isEmpty() ? new ArrayList<>(choicesWithParams.keySet()) : choices;
}
}