Class AvatarManager

java.lang.Object
no.ntnu.principes.util.images.AvatarManager

public class AvatarManager extends Object
Manages avatar images using the DiceBear API. Provides caching and thread-safe access to avatar images.
  • Constructor Details

    • AvatarManager

      public AvatarManager()
  • Method Details

    • getAvatarForName

      public static javafx.scene.image.Image getAvatarForName(String name)
      Gets an avatar image for a name, creating it if not already cached. Hashes the name first for privacy and deterministic image generation.
      Parameters:
      name - The name to get an avatar for
      Returns:
      The avatar image, either from cache or newly created
    • getCachedAvatarForName

      public static Optional<javafx.scene.image.Image> getCachedAvatarForName(String name)
      Gets a cached avatar for a name without creating a new one if not found.
      Parameters:
      name - The name to get a cached avatar for
      Returns:
      An Optional containing the cached avatar, or empty if not cached
    • getAvatarForHashedName

      public static javafx.scene.image.Image getAvatarForHashedName(String hashedName)
      Gets an avatar for a pre-hashed name, creating it if not already cached. Logs whether the avatar was fetched or retrieved from cache.
      Parameters:
      hashedName - The pre-hashed name to get an avatar for
      Returns:
      The avatar image, either from cache or newly created
    • preloadAvatars

      public static void preloadAvatars(String... hashedNames)
      Preloads avatars for multiple hashed names. Ensures avatars are available in the cache, loading any that aren't already cached.
      Parameters:
      hashedNames - Array of pre-hashed names to preload avatars for
    • clearCache

      public static void clearCache()
      Clears the avatar cache. Removes all cached avatars, freeing memory.
    • getHashFor

      public static String getHashFor(String name)
      Gets an MD5 hash for a name. Normalizes the name by trimming and converting to lowercase before hashing.
      Parameters:
      name - The name to hash
      Returns:
      The hexadecimal string representation of the MD5 hash
      Throws:
      RuntimeException - If the MD5 algorithm is not available