In one of my domain classes I need to store a set of symbols chosen from a finite set. In my case it's a choice of one or more weekdays, say Monday plus Tuesday.
Generally speaking I would approach this problem with a bitset (bit 0 = Monday, bit 1 = Tuesday...)
So my first version looks like this:
class SomeDomainClass {
// available elements
static final MON = 1, TUE = 2, WED = 4, THU = 8, FRI = 16, SAT = 32, SUN = 64
// the persistent field and its default value
int businessDays = MON | TUE | WED | THU | FRI
// constraint: not null, not empty and not outside bit set
static constraints = {
businessDays nullable: false, min: 1, max: 127
}
// list getter, returns a list such as [MON, TUE]
def getBusinessDaysList() {
return [MON, TUE, WED, THU, FRI, SAT, SUN].findAll { businessDays & it }
}
// list setter, accepts a list such as [MON, TUE]
void setBusinessDaysList(list) {
businessDays = list.inject { a,b -> a|b }
}
}
But something tells me there's a better way to handle this kind of field in Grails / Hibernate, including automatic data binding for checkboxes and such.