Class JsEnumBuilder

java.lang.Object
jsonvalues.spec.JsEnumBuilder

public final class JsEnumBuilder extends Object
Builder class for creating instances of JsEnum, which represents an enumeration of string symbols. Enums define a fixed set of allowed values, and each value is associated with a unique symbol. Enums are useful for specifying fields with a predefined set of options.

Example usage:

 
 var enumSpec = JsEnumBuilder.withName("Color")
                             .withAliases(List.of("Colour")) // Optional: Set aliases for the enum
                             .withNamespace("common")
                             .withDoc("Represents a color") // Optional: Set documentation for the enum
                             .withDefaultSymbol("RED") // Optional: Set a default symbol
                             .build("RED", "GREEN", "BLUE"); // Specify the allowed symbols
 
 

Note: If you are not creating specs specifically for Avro schemas, consider using the method JsSpecs.oneStringOf(String, String...) to create string specs instead.

  • Method Details

    • withName

      public static JsEnumBuilder withName(String name)
      Sets the name of the JSON object specification. The name must follow the Avro naming conventions, adhering to the regex pattern: [A-Za-z_][A-Za-z0-9_]*.
      Parameters:
      name - The name of the enum.
      Returns:
      A new JsEnumBuilder instance.
      Throws:
      IllegalArgumentException - If the provided name does not follow the specified pattern.
    • withAliases

      public JsEnumBuilder withAliases(List<String> aliases)
      Sets aliases for the enum. Must follow the avro naming conventions and, adhering to the regex pattern: [A-Za-z_][A-Za-z0-9_.]+

      Aliases provide alternative names for the enum, and they can be used interchangeably when referring to the same specification.

      Parameters:
      aliases - A list of alternative names (aliases) for the enum.
      Returns:
      This JsEnumBuilder for method chaining.
      Throws:
      IllegalArgumentException - If any of the provided aliases does not follow the naming pattern.
    • withNamespace

      public JsEnumBuilder withNamespace(String nameSpace)
      Sets the namespace of the JSON object specification. The namespace must follow the Avro naming conventions, adhering to the regex pattern: [A-Za-z_][A-Za-z0-9_.]+. It should start with a letter or an underscore, followed by letters, numbers, underscores, or dots.
      Parameters:
      nameSpace - The namespace of the enum specification.
      Returns:
      A reference to this JsEnumBuilder instance for method chaining.
      Throws:
      IllegalArgumentException - If the provided namespace does not follow the Avro naming conventions.
    • withDoc

      public JsEnumBuilder withDoc(String doc)
      Sets the documentation for the enum.
      Parameters:
      doc - The documentation for the enum.
      Returns:
      This JsEnumBuilder for method chaining.
    • withDefaultSymbol

      public JsEnumBuilder withDefaultSymbol(String symbol)
      Sets the default symbol for the enum. The default symbol is the value used if no specific value is provided.
      Parameters:
      symbol - The default symbol for the enum.
      Returns:
      This JsEnumBuilder instance for method chaining.
    • build

      public JsSpec build(String... symbols)
      Builds and returns a JsEnum specification with the specified symbols. The symbols represent the allowed values for the enum.
      Parameters:
      symbols - The symbols allowed in the enum.
      Returns:
      The constructed JsEnum specification.
      Throws:
      IllegalArgumentException - If the default symbol is specified and is not contained in the list of symbols.
    • build

      public JsSpec build(List<String> symbols)
      Builds and returns a JsEnum specification with the specified symbols. The symbols represent the allowed values for the enum.
      Parameters:
      symbols - The symbols allowed in the enum.
      Returns:
      The constructed JsEnum specification.
      Throws:
      IllegalArgumentException - If the default symbol is specified and is not contained in the list of symbols.
    • build

      public JsSpec build(JsArray symbols)
      Builds and returns a JsEnum specification with the specified symbols. The symbols represent the allowed values for the enum.
      Parameters:
      symbols - The symbols allowed in the enum.
      Returns:
      The constructed JsEnum specification.
      Throws:
      IllegalArgumentException - If the default symbol is specified and is not contained in the list of symbols.