Class NamedStringTemplate

java.lang.Object
org.miaixz.bus.core.text.placeholder.StringTemplate
org.miaixz.bus.core.text.placeholder.template.NamedStringTemplate

public class NamedStringTemplate extends StringTemplate
有前后缀的字符串模板

例如,"{1}", "{name}", "#{data}"

Since:
Java 17+
Author:
Kimi Liu
  • Field Details

  • Constructor Details

  • Method Details

    • builder

      public static NamedStringTemplate.Builder builder(String template)
      创建 builder
      Parameters:
      template - 字符串模板,不能为 null
      Returns:
      builder实例
    • parseSegments

      protected List<StringSegment> parseSegments(String template)
      Description copied from class: StringTemplate
      将 模板 解析为 SectionBuffer 列表
      Specified by:
      parseSegments in class StringTemplate
      Parameters:
      template - 字符串模板
      Returns:
      Segment列表
    • formatSequence

      public String formatSequence(Object... args)
      按顺序使用数组元素替换占位符
      Parameters:
      args - 可变参数
      Returns:
      格式化字符串
    • formatArraySequence

      public String formatArraySequence(Object array)
      按顺序使用原始数组元素替换占位符
      Parameters:
      array - 原始类型数组,例如: int[]
      Returns:
      格式化字符串
    • formatArraySequence

      public String formatArraySequence(Object[] array)
      按顺序使用数组元素替换占位符
      Parameters:
      array - 数组
      Returns:
      格式化字符串
    • formatSequence

      public String formatSequence(Iterable<?> iterable)
      按顺序使用迭代器元素替换占位符
      Overrides:
      formatSequence in class StringTemplate
      Parameters:
      iterable - iterable
      Returns:
      格式化字符串
    • formatIndexed

      public String formatIndexed(Object... args)
      按下标使用数组元素替换占位符
      Parameters:
      args - 可变参数
      Returns:
      格式化字符串
    • formatArrayIndexed

      public String formatArrayIndexed(Object array)
      按下标使用原始数组元素替换占位符
      Parameters:
      array - 原始类型数组
      Returns:
      格式化字符串
    • formatArrayIndexed

      public String formatArrayIndexed(Object[] array)
      按 下标 使用 数组元素 替换 占位符
      Parameters:
      array - 数组
      Returns:
      格式化字符串
    • formatIndexed

      public String formatIndexed(Collection<?> collection)
      按下标使用集合元素替换占位符
      Parameters:
      collection - 集合元素
      Returns:
      格式化字符串
    • formatIndexed

      public String formatIndexed(Collection<?> collection, IntFunction<String> missingIndexHandler)
      按 下标 使用 集合元素 替换 占位符
      Parameters:
      collection - 集合元素
      missingIndexHandler - 集合中不存在下标位置时的处理器,根据 下标 返回 代替值
      Returns:
      格式化字符串
    • format

      public String format(Object beanOrMap)
      使用占位变量名称,从 Bean 或 Map 中查询值来替换占位符
      Parameters:
      beanOrMap - Bean 或 Map 实例
      Returns:
      格式化字符串
    • format

      public String format(Map<String,?> map)
      使用占位变量名称,从 Map 中查询值来替换占位符
      Parameters:
      map - map
      Returns:
      格式化字符串
    • format

      public String format(Function<String,?> valueSupplier)
      使用占位变量名称,从 valueSupplier 中查询值来替换占位符
      Parameters:
      valueSupplier - 根据 占位变量名称 返回 值
      Returns:
      格式化字符串
    • format

      public String format(Function<String,?> valueSupplier, Predicate<String> containsKey)
      使用占位变量名称,从 valueSupplier 中查询值来替换占位符
      Parameters:
      valueSupplier - 根据 占位变量名称 返回 值
      containsKey - 占位变量名称 是否存在,例如:map.containsKey(data)
      Returns:
      格式化字符串
    • matchesSequenceToArray

      public String[] matchesSequenceToArray(String text)
      将占位符位置的值,按顺序解析为字符串数组
      Parameters:
      text - 待解析的字符串,一般是格式化方法的返回值
      Returns:
      字符串数组
    • matchesSequence

      public List<String> matchesSequence(String text)
      将占位符位置的值,按顺序解析为字符串列表
      Overrides:
      matchesSequence in class StringTemplate
      Parameters:
      text - 待解析的字符串,一般是格式化方法的返回值
      Returns:
      字符串列表
    • matchesIndexedToArray

      public String[] matchesIndexedToArray(String text)
      将占位符位置的值,按占位符下标值解析为字符串数组
      Parameters:
      text - 待解析的字符串,一般是格式化方法的返回值
      Returns:
      字符串数组
      See Also:
    • matchesIndexedToArray

      public String[] matchesIndexedToArray(String text, IntFunction<String> missingIndexHandler)
      将占位符位置的值,按占位符下标值解析为字符串数组
      Parameters:
      text - 待解析的字符串,一般是格式化方法的返回值
      missingIndexHandler - 根据 下标 返回 默认值,该参数可以为 null,仅在 StringTemplate.Feature.MATCH_EMPTY_VALUE_TO_DEFAULT_VALUE 策略时生效
      Returns:
      字符串数组
      See Also:
    • matchesIndexed

      public List<String> matchesIndexed(String text)
      将占位符位置的值,按占位符下标值解析为字符串列表
      Parameters:
      text - 待解析的字符串,一般是格式化方法的返回值
      Returns:
      字符串列表
      See Also:
    • matchesIndexed

      public List<String> matchesIndexed(String text, IntFunction<String> missingIndexHandler)
      将占位符位置的值,按占位符下标值解析为字符串列表

      例如,模板中为 "This is between {1} and {2}",格式化结果为 "This is between 666 and 999", 由于其最大下标为 2, 则解析结果中固定有 3 个元素,解析结果为 [null, "666", "999"]

      Parameters:
      text - 待解析的字符串,一般是格式化方法的返回值
      missingIndexHandler - 根据 下标 返回 默认值,该参数可以为 null,仅在 StringTemplate.Feature.MATCH_EMPTY_VALUE_TO_DEFAULT_VALUE 策略时生效
      Returns:
      字符串列表
    • matchesIndexed

      public void matchesIndexed(String text, BiConsumer<Integer,String> idxValueConsumer, IntFunction<String> missingIndexHandler)
      根据下标和下标占位符位置的值,自行提取结果值

      例如,模板中为 "This is between {1} and {2}",格式化结果为 "This is between 666 and 999", 由于其最大下标为 2, 则解析结果中固定有 3 个元素,解析结果为 [null, "666", "999"]

      Parameters:
      text - 待解析的字符串,一般是格式化方法的返回值
      idxValueConsumer - 处理 下标 和 下标占位符位置的值 的消费者,例如:(idx, value) -> list.set(idx, value)
      missingIndexHandler - 根据 下标 返回 默认值,该参数可以为 null,仅在 StringTemplate.Feature.MATCH_EMPTY_VALUE_TO_DEFAULT_VALUE 策略时生效
    • matches

      public Map<String,String> matches(String text)
      根据占位变量和对应位置解析值,构造 Map
      Parameters:
      text - 待解析的字符串,一般是格式化方法的返回值
      Returns:
      Map
    • matches

      public <T> T matches(String text, Supplier<T> beanOrMapSupplier)
      根据占位变量和对应位置解析值,构造 map 或者 beans 实例
      Type Parameters:
      T - 返回结果对象类型
      Parameters:
      text - 待解析的字符串,一般是格式化方法的返回值
      beanOrMapSupplier - 提供一个 beans 或者 map,例如:HashMap::new
      Returns:
      map 或者 beans 实例