java.lang.Object
no.ntnu.principes.util.images.AvatarManager
Manages avatar images using the DiceBear API.
Provides caching and thread-safe access to avatar images.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidClears the avatar cache.static javafx.scene.image.ImagegetAvatarForHashedName(String hashedName) Gets an avatar for a pre-hashed name, creating it if not already cached.static javafx.scene.image.ImagegetAvatarForName(String name) Gets an avatar image for a name, creating it if not already cached.static Optional<javafx.scene.image.Image> getCachedAvatarForName(String name) Gets a cached avatar for a name without creating a new one if not found.static StringgetHashFor(String name) Gets an MD5 hash for a name.static voidpreloadAvatars(String... hashedNames) Preloads avatars for multiple hashed names.
-
Constructor Details
-
AvatarManager
public AvatarManager()
-
-
Method Details
-
getAvatarForName
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
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
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
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
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
-