Class QueryString

java.lang.Object
cloud.opencode.base.web.url.QueryString

public final class QueryString extends Object
Query String - URL Query String Parser/Builder 查询字符串 - URL 查询字符串解析器/构建器

This class handles URL query string parsing and construction with support for multi-value parameters.

此类处理 URL 查询字符串的解析和构建,支持多值参数。

Example | 示例:

// Parse query string
QueryString qs = QueryString.parse("name=John&age=30");
String name = qs.get("name"); // "John"

// Build query string
String query = QueryString.builder()
    .add("name", "John")
    .add("age", "30")
    .build()
    .toString(); // "name=John&age=30"

Features | 主要功能:

  • Query string parsing and building - 查询字符串解析和构建
  • Multi-value parameter support - 多值参数支持
  • Immutable with functional modification - 不可变带函数式修改

Usage Examples | 使用示例:

QueryString qs = QueryString.parse("name=John&age=30");
String name = qs.get("name");
QueryString added = qs.with("page", "1");

Security | 安全性:

  • Thread-safe: No - 否
  • Null-safe: Yes (parse handles null) - 是(parse处理null)
Since:
JDK 25, opencode-base-web V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • empty

      public static QueryString empty()
      Creates an empty query string. 创建空的查询字符串。
      Returns:
      the query string - 查询字符串
    • parse

      public static QueryString parse(String queryString)
      Parses a query string. 解析查询字符串。
      Parameters:
      queryString - the query string (without leading '?') - 查询字符串
      Returns:
      the parsed query string - 解析的查询字符串
    • of

      public static QueryString of(Map<String,String> map)
      Creates from a map. 从 Map 创建。
      Parameters:
      map - the parameter map - 参数 Map
      Returns:
      the query string - 查询字符串
    • builder

      public static QueryString.Builder builder()
      Creates a builder. 创建构建器。
      Returns:
      the builder - 构建器
    • get

      public String get(String name)
      Gets a parameter value. 获取参数值。
      Parameters:
      name - the parameter name - 参数名
      Returns:
      the value or null - 值或 null
    • get

      public String get(String name, String defaultValue)
      Gets a parameter value with default. 获取参数值(带默认值)。
      Parameters:
      name - the parameter name - 参数名
      defaultValue - the default value - 默认值
      Returns:
      the value or default - 值或默认值
    • getAll

      public List<String> getAll(String name)
      Gets all values for a parameter. 获取参数的所有值。
      Parameters:
      name - the parameter name - 参数名
      Returns:
      the values - 值列表
    • has

      public boolean has(String name)
      Checks if parameter exists. 检查参数是否存在。
      Parameters:
      name - the parameter name - 参数名
      Returns:
      true if exists - 如果存在返回 true
    • names

      public Set<String> names()
      Gets all parameter names. 获取所有参数名。
      Returns:
      the parameter names - 参数名集合
    • size

      public int size()
      Gets the number of parameters. 获取参数数量。
      Returns:
      the size - 大小
    • isEmpty

      public boolean isEmpty()
      Checks if empty. 检查是否为空。
      Returns:
      true if empty - 如果为空返回 true
    • toMap

      public Map<String,String> toMap()
      Converts to map (first value only). 转换为 Map(仅第一个值)。
      Returns:
      the map - Map
    • toMultiMap

      public Map<String, List<String>> toMultiMap()
      Converts to multi-value map. 转换为多值 Map。
      Returns:
      the multi-value map - 多值 Map
    • with

      public QueryString with(String name, String value)
      Creates a new query string with added parameter. 创建添加参数后的新查询字符串。
      Parameters:
      name - the parameter name - 参数名
      value - the parameter value - 参数值
      Returns:
      the new query string - 新查询字符串
    • without

      public QueryString without(String name)
      Creates a new query string without specified parameter. 创建删除指定参数后的新查询字符串。
      Parameters:
      name - the parameter name - 参数名
      Returns:
      the new query string - 新查询字符串
    • toString

      public String toString()
      Returns the encoded query string. 返回编码的查询字符串。
      Overrides:
      toString in class Object
      Returns:
      the query string - 查询字符串
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object