class TaskJuggler::Interval

This is the based class used to store several kinds of intervals in derived classes.

Attributes

end[R]
start[R]

Public Class Methods

new(s, e) click to toggle source

Create a new Interval object. s is the interval start, e the interval end (not included).

# File lib/taskjuggler/Interval.rb, line 25
def initialize(s, e)
  @start = s
  @end = e
  # The end must not be before the start.
  if @end < @start
    raise ArgumentError, "Invalid interval (#{s} - #{e})"
  end
end

Public Instance Methods

<=>(iv) click to toggle source

Compare self with Interval iv. This function only works for non-overlapping Interval objects.

# File lib/taskjuggler/Interval.rb, line 84
def <=>(iv)
  raise ArgumentError, "Class mismatch" if self.class != iv.class
  if @end < iv.start
    -1
  elsif iv.end < @start
    1
  end
  0
end
==(iv) click to toggle source

Return true if the Interval iv describes an identical time period.

# File lib/taskjuggler/Interval.rb, line 95
def ==(iv)
  raise ArgumentError, "Class mismatch" if self.class != iv.class
  @start == iv.start && @end == iv.end
end
combine(iv) click to toggle source

Append or prepend the Interval iv to self. If iv does not directly attach to self, just return self.

# File lib/taskjuggler/Interval.rb, line 69
def combine(iv)
  raise ArgumentError, "Class mismatch" if self.class != iv.class
  if iv.end == @start
    # Prepend iv
    Array.new self.class.new(iv.start, @end)
  elsif @end == iv.start
    # Append iv
    Array.new self.class.new(@start, iv.end)
  else
    self
  end
end
contains?(arg) click to toggle source

Return true if arg is contained within the Interval. It can either be a single TjTime or another Interval.

# File lib/taskjuggler/Interval.rb, line 36
def contains?(arg)
  if arg.is_a?(Interval)
    raise ArgumentError, "Class mismatch" if self.class != arg.class
    return @start <= arg.start && arg.end <= @end
  else
    raise ArgumentError, "Class mismatch" if @start.class != arg.class
    return @start <= arg && arg < @end
  end
end
intersection(iv) click to toggle source

Return a new Interval that contains the overlap of self and the Interval iv. In case there is no overlap, nil is returned.

# File lib/taskjuggler/Interval.rb, line 60
def intersection(iv)
  raise ArgumentError, "Class mismatch" if self.class != iv.class
  newStart = @start > iv.start ? @start : iv.start
  newEnd = @end < iv.end ? @end : iv.end
  newStart < newEnd ? self.class.new(newStart, newEnd) : nil
end
overlaps?(arg) click to toggle source

Check whether the Interval arg overlaps with this Interval.

# File lib/taskjuggler/Interval.rb, line 47
def overlaps?(arg)
  if arg.is_a?(Interval)
    raise ArgumentError, "Class mismatch" if self.class != arg.class
    return (@start <= arg.start && arg.start < @end) ||
           (arg.start <= @start && @start < arg.end)
  else
    raise ArgumentError, "Class mismatch" if @start.class != arg.class
    return @start <= arg && arg < @end
  end
end