kiwimix/pkg/calendarops/boolean_difference.go

48 lines
1.4 KiB
Go
Raw Normal View History

2023-06-20 21:44:50 +00:00
package calendarops
import (
"errors"
basetypes "code.apps.glenux.net/glenux/kiwimix/pkg/basetypes"
)
// DifferenceCalendarOperation is an operation that substract a calendar from
// another
type DifferenceCalendarOperation struct{}
// Ensure DifferenceCalendarOperation implements CalendarOperation
var _ CalendarOperation = (*DifferenceCalendarOperation)(nil)
func (op DifferenceCalendarOperation) Execute(calendars ...basetypes.Calendar) (basetypes.Calendar, error) {
// Check that two calendars are provided
if len(calendars) != 2 {
return basetypes.Calendar{}, errors.New("difference operation requires exactly two calendars")
}
// Create a new calendar to store the difference
differenceCalendar := basetypes.NewCalendar()
// Iterate over the events in the first calendar
for _, event1 := range calendars[0].GetEvents() {
// Assume initially that the event is unique to the first calendar
isUnique := true
// Compare with the events in the second calendar
for _, event2 := range calendars[1].GetEvents() {
if event1.Equals(event2) {
// If the event is also in the second calendar, it is not unique
isUnique = false
break
}
}
// If the event is unique to the first calendar, add it to the difference calendar
if isUnique {
differenceCalendar.SetEvent(event1)
}
}
// Return the difference calendar
return differenceCalendar, nil
}