#864: CalendarStyleMap.kt

projectforge-business/src/main/kotlin/org/projectforge/business/calendar/CalendarStyleMap.kt Type: Kotlin class (style registry)
Purpose: Maps calendar IDs to their display styles (colors), with built-in defaults for birthday calendars
Source path: projectforge-business/src/main/kotlin/org/projectforge/business/calendar/CalendarStyleMap.kt 69 lines · 30 code · 31 comments · 8 blank

CalendarStyleMap is a user-scoped registry that associates each calendar (by its database ID) with a CalendarStyle object defining its colors. It provides convenience accessors for the two built-in birthday calendars and automatically initializes their styles with hardcoded defaults (#06790e green for favorites birthdays, #ffffff white for all birthdays) when first accessed if not already configured.

Fields

styles: MutableMap<Long, CalendarStyle> (private)

Internal map storing the CalendarStyle per calendar ID. Not exposed directly; accessed through get(), add(), and contains().

Convenience Accessors

birthdaysFavoritesStyle: CalendarStyle

Returns the style for the "favorite birthdays" calendar (BIRTHDAYS_FAVS_CAL_ID). Uses !! because the get() method guarantees non-null for this ID (auto-creates if absent).

birthdaysAllStyle: CalendarStyle

Returns the style for the "all birthdays" calendar (BIRTHDAYS_ALL_CAL_ID). Similarly non-null-guaranteed.

Methods

contains(calendarId: Long): Boolean

Checks whether a style exists for the given calendar ID.

add(calendarId: Long, style: CalendarStyle)

Adds or replaces a style for a given calendar ID.

get(calendarId: Long?): CalendarStyle?

Retrieves the style for a calendar ID. Returns null if the ID itself is null. Special-cases the two built-in birthday calendar IDs (BIRTHDAYS_FAVS_CAL_ID and BIRTHDAYS_ALL_CAL_ID): if no style has been explicitly added for them, lazily creates and caches a default style ("#06790e" for favorites, "#ffffff" for all). This lazy initialization ensures the birthday calendars always have a visible style without requiring explicit configuration.

Git History

Design Rationale

The lazy initialization pattern for birthday calendars avoids requiring an explicit configuration step. Instead of needing an installer or migration script to pre-seed birthday styles, the first call to get() with either birthday ID transparently creates the default style. This also means that if a user's stored preferences are missing these entries (e.g., after an upgrade), they are regenerated on-the-fly. The MutableMap is kept private to prevent direct mutation from outside the class, ensuring the lazy defaults are always triggered through get() rather than via map access.