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 namevalue- the value
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 Summary
ConstructorsConstructorDescriptionNamedParameter(String name, T value) Creates an instance of aNamedParameterrecord class. -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanIndicates whether some other object is "equal to" this one.static <T> Tget(NamedParameter<?>[] parameters, String name, Supplier<T> fallback) Looks up the named parameter with the givennamein array ofNamedParameters.final inthashCode()Returns a hash code value for this object.name()Returns the value of thenamerecord component.final StringtoString()Returns a string representation of this record class.value()Returns the value of thevaluerecord component.
-
Constructor Details
-
Method Details
-
get
Looks up the named parameter with the givennamein array ofNamedParameters. If it isn't found, return the result from invoking the supplier (ornull).- Type Parameters:
T- the generic type- Parameters:
parameters- the parametersname- the namefallback- supplier for a fallback value ornull- Returns:
- the value
-
toString
-
hashCode
-
equals
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 withObjects::equals(Object,Object). -
name
-
value
-