Hello, I’m having issues with implementation of collection to store continuous events.
The continuous event is something that happened in the past at startTime
and and ended at endTime
(or still continues). It is also possible that the exact time of the event is not known, in which case both startTime
and endTime
are None. The event has associated information of arbitrary type, it can be a string, a sequence of maps, etc. The goal of the collection is to implement logic of insertion of the events in the right order and do it as efficiently as possible.
Here is definition of an example event that extends the base class Event
. You might wonder why there is a need for subclassing, the problem is that type of the additionalInfo
must be known at compile time.
abstract class Event {
def addedOn: Timestamp
def startTime: Option[Timestamp]
def endTime: Option[Timestamp]
def additionalInfo: Any
}
case class StringEvent(
intervalOpenness: Byte,
startTime: Option[Timestamp],
endTime: Option[Timestamp],
additionalInfo: String)
extends Event
Here is definition of the collection.
class Timeline[+A <: Event, +Repr] private (
val history: Vector[A],
val withReplacement: Boolean) extends Iterable[A]{
def append[B >: A <: Event, That](e: B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
// Determine the appropriate position for the event to be inserted
// Find the neighbours and update the events using some rules
// (for ex., when withReplacement = true update endTime of the previous event if it was the last event)
}
override def iterator: Iterator[A] = new AbstractIterator[A] {
...
}
}
I was trying to follow an example from scala collections to define own. But I have a few problems I have no idea how to overcome:
- How to access fields of
StringEvent
insideappend
to determine right position for a new event? I added upper type bound on Event to achieve this, but I’m not sure how good this idea is; - How to create a copy of a generic case class in order to update it? This part is the most frustrating, with abstract base class there is no method
copy
defined, so how to create an instance ofEvent
with proper type is not clear.
I would appreciate any help. Thanks!