Class TieredValue
- All Implemented Interfaces:
Serializable
TieredValue.Tier target/value pairs, for example volume breakpoints and the discount or surcharge that applies from each breakpoint.
A multi-tier input is created via createMultiTierEntryInputBuilder. Reading it with input.<name> returns the raw Map<String, String> of targets to values, not a TieredValue. Keys and values must be strings (as the UI stores them); set default values as strings, as in the example below, because a number causes a ClassCastException in fromMap. Convert the map with fromMap(Map, TieredValueSortType, TieredValueValidationType), passing the same sort type as the input, or with fromMap(Map) for ascending order. The deprecated api.multiTierEntry() returns a TieredValue directly. The class is available in logics without an import.
multiplyTargets(BigDecimal) and multiplyValues(BigDecimal) return new instances and leave this one unchanged.
Example, defining a multi-tier input and reading the value of the tier that applies to a volume of 15:
import net.pricefx.common.api.chart.TieredValueSortType
import net.pricefx.common.api.chart.TieredValueValidationType
if (api.isInputGenerationExecution()) {
return api.inputBuilderFactory()
.createMultiTierEntryInputBuilder("multiTier")
.setLabel("Multi Tier")
.setValue(["10": "20", "20": "40"])
.setSortType(TieredValueSortType.ASC)
.setValidationType(TieredValueValidationType.NO_VALIDATION)
.getInput()
}
def tiers = TieredValue.fromMap(input.multiTier)
return tiers?.getTierForTarget(15.0)?.value
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classOne target/value pair of aTieredValue, for example one volume breakpoint and the discount or surcharge that applies from it. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionall()Returns all tiers of thisTieredValue, in sorted order.asMap()Converts thisTieredValueback into a map of targets to values, in sorted order.clone()Returns a newTieredValuewith the same tiers, sort type and validation type as this one, rebuilt viaasMap()andfromMap(Map, TieredValueSortType, TieredValueValidationType).static TieredValueCreates aTieredValuefrom a map of targets to values, with the tiers in ascending order.static TieredValuefromMap(Map<String, String> map, TieredValueSortType sortType, TieredValueValidationType validationType) Creates aTieredValuefrom a map of targets to values, parsing each key and value as aBigDecimaland sorting the tiers by target in the given order.get(int idx) Returns the tier at the given 0-based position in thisTieredValue's sorted tier list.getTierForTarget(BigDecimal target) Returns the tier that applies to the given target: the last tier, in stored order, whose target is less than or equal to it.booleanisValid()Returns whether every entry of the map thisTieredValuewas created from was parsed as a number.multiplyTargets(BigDecimal factor) multiplyValues(BigDecimal factor) Returns a copy of thisTieredValue(seeclone()) with every tier's value multiplied byfactor, for example0.01to turn percentage points into fractions.intsize()Returns the number of tiers in thisTieredValue.toString()Returns thisTieredValue's tiers joined into a single string, each formatted byTieredValue.Tier.toString()and separated by", ", for example"1. 10=20, 2. 20=40".
-
Constructor Details
-
TieredValue
public TieredValue()
-
-
Method Details
-
clone
Returns a newTieredValuewith the same tiers, sort type and validation type as this one, rebuilt viaasMap()andfromMap(Map, TieredValueSortType, TieredValueValidationType). Entries that were skipped when thisTieredValuewas parsed are not part of it, soisValid()returnstrueon the copy. -
get
Returns the tier at the given 0-based position in thisTieredValue's sorted tier list.- Parameters:
idx- The 0-based tier position.- Returns:
- The tier at that position, or
nullifidxis out of range.
-
all
Returns all tiers of thisTieredValue, in sorted order. The returned list is this object's own internal list, not a copy; modifying it modifies thisTieredValue.- Returns:
- The tiers, in sorted order. Never
null.
-
getTierForTarget
Returns the tier that applies to the given target: the last tier, in stored order, whose target is less than or equal to it. For tiers 10=20 and 20=40, a target of 15 returns the 10=20 tier.This method assumes the tiers are sorted in ascending order. For a
TieredValuecreated withTieredValueSortType.DESC, the tiers are stored in descending order, so the result is not reliable; create theTieredValuewithTieredValueSortType.ASCbefore calling it.- Parameters:
target- The target to look up, for example a volume.- Returns:
- The matching tier, or
nulliftargetisnullor below every tier's target.
-
multiplyTargets
Returns a copy of thisTieredValue(seeclone()) with every tier's target multiplied byfactor. ThisTieredValueis left unchanged.- Parameters:
factor- The multiplier to apply to every target.nullis treated asBigDecimal.ZERO, which sets every target to zero.- Returns:
- The new, scaled
TieredValue.
-
multiplyValues
Returns a copy of thisTieredValue(seeclone()) with every tier's value multiplied byfactor, for example0.01to turn percentage points into fractions. ThisTieredValueis left unchanged.- Parameters:
factor- The multiplier to apply to every value.nullis treated asBigDecimal.ZERO, which sets every value to zero.- Returns:
- The new, scaled
TieredValue.
-
size
public int size()Returns the number of tiers in thisTieredValue.- Returns:
- The number of tiers.
-
isValid
public boolean isValid()Returns whether every entry of the map thisTieredValuewas created from was parsed as a number.falsemeans at least one entry could not be parsed and was skipped (seefromMap(Map, TieredValueSortType, TieredValueValidationType)).- Returns:
trueif all entries were parsed,falseotherwise.
-
toString
Returns thisTieredValue's tiers joined into a single string, each formatted byTieredValue.Tier.toString()and separated by", ", for example"1. 10=20, 2. 20=40". -
asMap
Converts thisTieredValueback into a map of targets to values, in sorted order. This is the shape of a multi-tier input's raw value and of the argument offromMap(Map). Keys and values are thetoString()form of the parsed numbers, so they are not necessarily the exact text entered.- Returns:
- The target/value pairs as a
Map<String, String>. Nevernull.
-
fromMap
Creates aTieredValuefrom a map of targets to values, with the tiers in ascending order. Equivalent tofromMap(Map, TieredValueSortType, TieredValueValidationType)withTieredValueSortType.ASCandTieredValueValidationType.VALIDATE.- Parameters:
map- The targets mapped to their values, as strings, for example the raw value of a multi-tier input (input.<name>) or the result ofasMap().- Returns:
- The parsed
TieredValue, ornullifmapisnull.
-
fromMap
public static TieredValue fromMap(Map<String, String> map, TieredValueSortType sortType, TieredValueValidationType validationType) Creates aTieredValuefrom a map of targets to values, parsing each key and value as aBigDecimaland sorting the tiers by target in the given order.- An entry whose key or value is not a number is skipped, and
isValid()returnsfalseon the result. - Keys that are numerically equal (for example
"10"and"10.0") produce a single tier with the value of the entry iterated last. - A
nullkey or value is not skipped: it causes aNullPointerException.
validationTypeis stored on the result and carried over byclone(), but this method does not apply it.- Parameters:
map- The targets mapped to their values, as strings, for example the raw value of a multi-tier input (input.<name>) or the result ofasMap().sortType- The order of the tiers:TieredValueSortType.ASCorTieredValueSortType.DESC. Use the sort type of the input the map comes from.validationType- The validation type to store on the result.- Returns:
- The parsed
TieredValue, ornullifmapisnull.
- An entry whose key or value is not a number is skipped, and
-