Class Helpers

java.lang.Object
net.ygbstudio.powerwp4j.utils.Helpers

public final class Helpers extends Object
Utility class for reusable logic in the project.

This class contains methods that can be used throughout the application to perform common tasks. It is designed to be a utility class, so it should not be instantiated.

  • Method Details

    • getPropertiesFromResources

      public static Optional<Properties> getPropertiesFromResources(String propertyFileName)
      Loads properties from a specified property file located in the resources' directory.
      Parameters:
      propertyFileName - The name of the property file to load.
      Returns:
      A Properties object containing the loaded properties.
      Throws:
      RuntimeException - if an error occurs while reading the property file.
    • writePropertyFile

      public static void writePropertyFile(String propertyFileName, List<String> properties, List<String> values, String fileComment) throws IOException, URISyntaxException
      Writes properties to a specified property file located in the resources directory, updating existing properties or adding new ones.

      Exceptions are relayed to the caller for handling.

      Parameters:
      propertyFileName - The name of the property file to write to.
      properties - An array of property names to write.
      values - An array of values corresponding to the properties.
      fileComment - A comment to include in the property file.
      Throws:
      IOException - if an error occurs while writing to the property file.
      URISyntaxException - if the URI of the property file cannot be resolved.
    • zip

      public static <K, V> Stream<Map.Entry<K,V>> zip(List<? extends K> elementOne, List<? extends V> elementTwo)
      Zips two lists into a stream of map entries, pairing elements from both lists by their indices. Contents are returned as a stream to allow for further processing or collection strategies.

      The resulting stream will contain entries where the key is from the first list and the value is from the second list.

      if the lists are of different lengths, the resulting stream will only contain entries up to the length of the shorter list.

      Type Parameters:
      K - type parameter for the key element
      V - type parameter for the value element
      Parameters:
      elementOne - A list of elements to be used as keys in the map entries.
      elementTwo - A list of elements to be used as values in the map entries.
      Returns:
      A stream of map entries where each entry pairs an element from the first list with an element from the second list.
    • isInEnum

      public static <T extends Enum<T>, R> boolean isInEnum(@NotNull @NotNull Class<T> enumType, Function<? super T,? extends R> transformer, Predicate<? super R> condition)
      Checks if any enum constant's transformed value matches a given condition.
      Type Parameters:
      T - The type of the enum.
      R - The type of the transformed value.
      Parameters:
      enumType - The class of the enum to check.
      transformer - A function that transforms an enum constant to a value of type R.
      condition - A predicate that tests the transformed value.
      Returns:
      true if any transformed value matches the condition, false otherwise.
    • enumFromValue

      public static <T extends FriendlyEnum> Optional<T> enumFromValue(Class<T> enumType, @Nullable @Nullable String strEnumKey, boolean ignoreCase)
      Retrieves an enum constant from a string value, with an option to match irrespectively of case. This method uses a regex pattern to match the string representation of enum constants.

      It can be used as validator for enums in a similar way as isInEnum(java.lang.Class<T>, java.util.function.Function<? super T, ? extends R>, java.util.function.Predicate<? super R>), however, this helper not only checks for existence but also returns the enum constant and provides more handling opportunities available for Optional values.

      Type Parameters:
      T - The type of the enum.
      Parameters:
      enumType - The class of the enum to search.
      strEnumKey - The string value to match against the enum constants.
      ignoreCase - If true, the match is case-insensitive; otherwise, it is case-sensitive.
      Returns:
      An Optional containing the matching enum constant, or empty if no match is found.
      See Also:
    • specialCharCleanJoin

      public static String specialCharCleanJoin(String toBeCleaned, String delimiter)
      Cleans a string by removing special characters and underscores and joining the remaining words with a delimiter.

      As part of the process, excess whitespace is removed and each word is trimmed. Useful for tag cleaning and slug generation.

      Parameters:
      toBeCleaned - the string of text to be inspected and cleaned
      delimiter - the delimiter to use for joining the remaining words
      Returns:
      the cleaned string joined by your delimiter