Record Class NamedParameter<T>

java.lang.Object
java.lang.Record
org.jdrupes.builder.api.NamedParameter<T>
Type Parameters:
T - the generic type
Record Components:
name - the name
value - the value

public record NamedParameter<T>(String name, T value) extends Record

Defines a named parameter. Java doesn't have named parameters, but this comes pretty close. Define the named parameters that you need as static members of your class:

public static NamedParameter<String> name(String name) {
    return new NamedParameter<>("name", name);
}

Defines the method that is to be invoked with named parameters as

public void method(NamedParameter<?>... params) {
    var name = NamedParameter.<String> get(params, "name", null);
}

And invoke it with:

method(name("test"));

Of course, this requires a static import for name. If you have several classes using the pattern, you'll have to use className.paramName(...) to provide the value for the parameter which is, admittedly less elegant.

A possible workaround is to define a hierarchy of classes with NamedParameter as base class and put commonly used names in the base classes.

  • Constructor Details

    • NamedParameter

      public NamedParameter(String name, T value)
      Creates an instance of a NamedParameter record class.
      Parameters:
      name - the value for the name record component
      value - the value for the value record component
  • Method Details

    • get

      public static <T> T get(NamedParameter<?>[] parameters, String name, Supplier<T> fallback)
      Looks up the named parameter with the given name in array of NamedParameters. If it isn't found, return the result from invoking the supplier (or null).
      Type Parameters:
      T - the generic type
      Parameters:
      parameters - the parameters
      name - the name
      fallback - supplier for a fallback value or null
      Returns:
      the value
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • name

      public String name()
      Returns the value of the name record component.
      Returns:
      the value of the name record component
    • value

      public T value()
      Returns the value of the value record component.
      Returns:
      the value of the value record component