Class TieredValue

Object
TieredValue
All Implemented Interfaces:
Serializable

public class TieredValue extends Object implements Serializable
The tiers entered in a multi-tier input: an ordered list of 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:
  • Constructor Details

    • TieredValue

      public TieredValue()
  • Method Details

    • clone

      public TieredValue clone()
      Returns a new TieredValue with the same tiers, sort type and validation type as this one, rebuilt via asMap() and fromMap(Map, TieredValueSortType, TieredValueValidationType). Entries that were skipped when this TieredValue was parsed are not part of it, so isValid() returns true on the copy.
      Overrides:
      clone in class Object
      Returns:
      The copy.
    • get

      public TieredValue.Tier get(int idx)
      Returns the tier at the given 0-based position in this TieredValue's sorted tier list.
      Parameters:
      idx - The 0-based tier position.
      Returns:
      The tier at that position, or null if idx is out of range.
    • all

      public List<TieredValue.Tier> all()
      Returns all tiers of this TieredValue, in sorted order. The returned list is this object's own internal list, not a copy; modifying it modifies this TieredValue.
      Returns:
      The tiers, in sorted order. Never null.
    • getTierForTarget

      public TieredValue.Tier 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. 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 TieredValue created with TieredValueSortType.DESC, the tiers are stored in descending order, so the result is not reliable; create the TieredValue with TieredValueSortType.ASC before calling it.

      Parameters:
      target - The target to look up, for example a volume.
      Returns:
      The matching tier, or null if target is null or below every tier's target.
    • multiplyTargets

      public TieredValue multiplyTargets(BigDecimal factor)
      Returns a copy of this TieredValue (see clone()) with every tier's target multiplied by factor. This TieredValue is left unchanged.
      Parameters:
      factor - The multiplier to apply to every target. null is treated as BigDecimal.ZERO, which sets every target to zero.
      Returns:
      The new, scaled TieredValue.
    • multiplyValues

      public TieredValue multiplyValues(BigDecimal factor)
      Returns a copy of this TieredValue (see clone()) with every tier's value multiplied by factor, for example 0.01 to turn percentage points into fractions. This TieredValue is left unchanged.
      Parameters:
      factor - The multiplier to apply to every value. null is treated as BigDecimal.ZERO, which sets every value to zero.
      Returns:
      The new, scaled TieredValue.
    • size

      public int size()
      Returns the number of tiers in this TieredValue.
      Returns:
      The number of tiers.
    • isValid

      public boolean isValid()
      Returns whether every entry of the map this TieredValue was created from was parsed as a number. false means at least one entry could not be parsed and was skipped (see fromMap(Map, TieredValueSortType, TieredValueValidationType)).
      Returns:
      true if all entries were parsed, false otherwise.
    • toString

      public String toString()
      Returns this TieredValue's tiers joined into a single string, each formatted by TieredValue.Tier.toString() and separated by ", ", for example "1. 10=20, 2. 20=40".
      Overrides:
      toString in class Object
      Returns:
      The formatted tiers, or an empty string if there are none.
    • asMap

      public Map<String,String> asMap()
      Converts this TieredValue back 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 of fromMap(Map). Keys and values are the toString() form of the parsed numbers, so they are not necessarily the exact text entered.
      Returns:
      The target/value pairs as a Map<String, String>. Never null.
    • fromMap

      public static TieredValue fromMap(Map<String,String> map)
      Creates a TieredValue from a map of targets to values, with the tiers in ascending order. Equivalent to fromMap(Map, TieredValueSortType, TieredValueValidationType) with TieredValueSortType.ASC and TieredValueValidationType.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 of asMap().
      Returns:
      The parsed TieredValue, or null if map is null.
    • fromMap

      public static TieredValue fromMap(Map<String,String> map, TieredValueSortType sortType, TieredValueValidationType validationType)
      Creates a TieredValue from a map of targets to values, parsing each key and value as a BigDecimal and sorting the tiers by target in the given order.
      • An entry whose key or value is not a number is skipped, and isValid() returns false on 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 null key or value is not skipped: it causes a NullPointerException.
      validationType is stored on the result and carried over by clone(), 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 of asMap().
      sortType - The order of the tiers: TieredValueSortType.ASC or TieredValueSortType.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, or null if map is null.