protected final TR getOrCreateTimeRange ( long timestamp ) { TR timeRange ; if ( lastTimeRange == null || timestamp > lastTimeRange . getEndTimestamp ( ) ) { // New time range long roundedTimestamp = timestamp - timestamp % timeRangeWidth ; timeRange = createTimeRange ( roundedTimestamp , roundedTimestamp + timeRangeWidth ) ; timeRanges . add ( timeRange ) ; lastTimeRange = timeRange ; } else if ( timestamp >= lastTimeRange . getStartTimestamp ( ) ) { // Current time range timeRange = lastTimeRange ; } else { // Old time range timeRange = null ; // Not very good from performance point of view // iterating from end till start would be a better solution for ( TR oldTimeRange : timeRanges ) { if ( oldTimeRange . containsTimestamp ( timestamp ) ) { timeRange = oldTimeRange ; break ; } } } return timeRange ; }
Returns existing time range if it already exists or create a new one .