Class Parameter

java.lang.Object
org.jboss.elemento.router.Parameter

public class Parameter extends Object
The Parameter class is used to represent parameters and their values in a route path. A parameter is a part of the path that starts with a colon (e.g. ":id"). Parameters can be required or optional. Optional parameters use a trailing question mark (e.g. ":id?") and must be at the end of the route. A route like "/users/:id?" matches both "/users" and "/users/123".

Multiple trailing optional parameters are allowed: "/a/:b?/:c?" matches "/a", "/a/1", and "/a/1/2". Optional parameters after required ones are also valid: "/a/:b/:c?" matches "/a/1" and "/a/1/2".

Parameter values are automatically decoded when retrieved via get(String) or getOrDefault(String, String). Use getRaw(String) to access the raw/encoded value as it appears in the URL.

When an optional parameter is not present in the path, get(String) returns null and has(String) returns false. Use getOrDefault(String, String) to provide a fallback value.

To build URLs with encoded parameter values, use encodePath(String, String...) or encode(String).

  • Field Details

    • EMPTY

      public static final Parameter EMPTY
  • Method Details

    • encode

      public static String encode(String value)
      Encodes a single parameter value for safe inclusion in a URL path segment. Special characters like /, ?, #, &, =, and spaces are percent-encoded.

      Pass raw/decoded values to this method. Do not pass already-encoded values, as this will result in double encoding.

      Parameters:
      value - the raw parameter value to encode
      Returns:
      the percent-encoded value
      Throws:
      IllegalArgumentException - if value is null
    • decode

      public static String decode(String value)
      Decodes a URL-encoded parameter value back to its original form.
      Parameters:
      value - the percent-encoded value to decode
      Returns:
      the decoded value
      Throws:
      IllegalArgumentException - if value is null
    • encodePath

      public static String encodePath(String route, String... values)
      Builds a URL path from a route template and raw parameter values. Values are encoded automatically using encode(String) and substituted into the template in order of parameter appearance.

      Optional parameters (e.g. ":name?") can be omitted. When fewer values are provided than the total number of parameters, trailing optional parameter segments are omitted from the output path.

      Example:

      Parameter.encodePath("/resource/:name", "my/file")
      // returns "/resource/my%2Ffile"
      
      Parameter.encodePath("/time/:area/:location", "America", "New_York")
      // returns "/time/America/New_York"
      
      Parameter.encodePath("/users/:id?")
      // returns "/users"
      
      Parameter.encodePath("/users/:id?", "123")
      // returns "/users/123"
      
      Parameters:
      route - the route template with :param and :param? placeholders
      values - the raw parameter values in the order they appear in the route. Required parameters must all be provided; optional parameters can be omitted from the end.
      Returns:
      the complete URL path with encoded parameter values
      Throws:
      IllegalArgumentException - if the number of values is less than the number of required parameters or greater than the total number of parameters, or if any required value is null
    • isEmpty

      public boolean isEmpty()
    • has

      public boolean has(String name)
      Returns whether a parameter with the given name exists.
      Parameters:
      name - the parameter name
      Returns:
      true if the parameter exists
    • get

      public String get(String name)
      Returns the decoded parameter value. Special characters that were percent-encoded in the URL are decoded back to their original form.
      Parameters:
      name - the parameter name
      Returns:
      the decoded parameter value, or null if not found
    • getOrDefault

      public String getOrDefault(String name, String defaultValue)
      Returns the decoded parameter value, or the given default if the parameter doesn't exist.
      Parameters:
      name - the parameter name
      defaultValue - the default value to return if the parameter doesn't exist
      Returns:
      the decoded parameter value, or the default value
    • getRaw

      public String getRaw(String name)
      Returns the raw/encoded parameter value as it appears in the URL. Use this when you need the percent-encoded form of the value.
      Parameters:
      name - the parameter name
      Returns:
      the raw/encoded parameter value, or null if not found
    • path

      public String path()
      Returns the path this parameter was parsed from.
      Returns:
      the original path