Class SystemPropertySupplier<T>

  • All Implemented Interfaces:
    java.util.function.Supplier<T>

    public class SystemPropertySupplier<T>
    extends java.lang.Object
    implements java.util.function.Supplier<T>
    Utility class for consistent handling of system properties.

    It provides for:

    • TRACE level logging of getting the system property
    • ERROR level logging when value does not parse or is invalid (where validity can be checked by a Predicate)
    • (default) INFO level logging when value differs from default (log level and message format can be overridden)

    The supported types are: Boolean, Integer, Long, String

    • Method Detail

      • create

        public static <U> SystemPropertySupplier<U> create​(@NotNull
                                                           @NotNull java.lang.String propName,
                                                           @NotNull
                                                           U defaultValue)
                                                    throws java.lang.IllegalArgumentException
        Create it for a given property name and default value.
        Throws:
        java.lang.IllegalArgumentException
      • validateWith

        public SystemPropertySupplier<T> validateWith​(@NotNull
                                                      @NotNull java.util.function.Predicate<T> validator)
        Specify a validation expression.
      • formatSetMessage

        public SystemPropertySupplier<T> formatSetMessage​(@NotNull
                                                          @NotNull java.util.function.BiFunction<java.lang.String,​T,​java.lang.String> setMessageFormatter)
        Specify a formatter for the "success" log message to be used when the returned property value differs from the default.
      • usingSystemPropertyReader

        protected SystemPropertySupplier<T> usingSystemPropertyReader​(@NotNull
                                                                      @NotNull java.util.function.Function<java.lang.String,​java.lang.String> sysPropReader)
        For unit testing: specify a function to read system properties (overriding default of System.getProperty(String).
      • get

        public T get()
        Obtains the value of a system property, optionally generating diagnostics.
        Specified by:
        get in interface java.util.function.Supplier<T>
        Returns:
        value of system property