Record Class PropertyProvider<T>

java.lang.Object
java.lang.Record
io.github.kaktushose.jdac.configuration.PropertyProvider<T>
Record Components:
type - the Property for which this PropertyProvider creates values.
priority - the priority of this provider
referenceClass - The Class to which this PropertyProvider 'belongs'. It's only used for logging purposes. For example, all fallback/default values have JDACBuilder as their reference class.
supplier - the Function returning the properties value, provides access to PropertyProvider.Context
All Implemented Interfaces:
Comparable<PropertyProvider<T>>

public record PropertyProvider<T>(Property<T> type, int priority, Class<?> referenceClass, Function<PropertyProvider.Context, @Nullable T> supplier) extends Record implements Comparable<PropertyProvider<T>>

PropertyProviders

An PropertyProvider provides values for the Property associated with it.

Dependencies

Because many properties especially services depend on other properties, you can get the values of these properties through the PropertyProvider.Context.get(Property) that is available in the supplier().

If somehow recursive calls to the same Property are created (called cycling dependencies), then a ConfigurationException is thrown providing information on how this recursion occurs.

Priorities

To decide which value is taken at the end for a property, each PropertyProvider defines a priority between 0 and Integer.MAX_VALUE. The provider with the highest priority will be taken, thus its value is set for the property.

It's important to note that the priorities 0 to 100 and Integer.MAX_VALUE are reserved by JDA-Commands:

  • priority = 0 are all fallback/default value provided by JDA-Commands
  • priority = Integer.MAX_VALUE are all values set by the user manually in JDACBuilder

Usage of reserved priorities will result in an exception causing JDA-Commands to stop itself.

Example

An example usage of this class would be:

class Foo {
    public bar(...) {
        var provider = new PropertyProvider(
            Property.INSTANTIATOR,
            2000, // just some random priority
            Foo.class, // used as a waypoint for debugging
            ctx -> new MyInstantiator(ctx.get(Property.I18N)) // needs the I18n instance
        )
    }
}
  • Constructor Details

    • PropertyProvider

      public PropertyProvider(Property<T> type, int priority, Class<?> referenceClass, Function<PropertyProvider.Context, @Nullable T> supplier)
      Creates an instance of a PropertyProvider record class.
      Parameters:
      type - the value for the type record component
      priority - the value for the priority record component
      referenceClass - the value for the referenceClass record component
      supplier - the value for the supplier record component
  • Method Details

    • create

      public static <T> PropertyProvider<T> create(Property<T> type, int priority, Function<PropertyProvider.Context, @Nullable T> supplier)
      Creates a new PropertyProvider where its referenceClass() is the caller of this method.
      Parameters:
      type - the Property for which this PropertyProvider creates values
      priority - the priority of this provider
      supplier - the Function returning the properties value, provides access to PropertyProvider.Context
      Returns:
      the PropertyProvider instance
      See Also:
    • compareTo

      public int compareTo(PropertyProvider<T> o)
      Compares two PropertyProvider by their priority() in reverse order. The PropertyProvider with the higher priority comes first.
      Specified by:
      compareTo in interface Comparable<T>
      Returns:
    • 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. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      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.
    • type

      public Property<T> type()
      Returns the value of the type record component.
      Returns:
      the value of the type record component
    • priority

      public int priority()
      Returns the value of the priority record component.
      Returns:
      the value of the priority record component
    • referenceClass

      public Class<?> referenceClass()
      Returns the value of the referenceClass record component.
      Returns:
      the value of the referenceClass record component
    • supplier

      public Function<PropertyProvider.Context, @Nullable T> supplier()
      Returns the value of the supplier record component.
      Returns:
      the value of the supplier record component