Class: TaskJuggler::Interval

Inherits:
Object
  • Object
show all
Defined in:
lib/taskjuggler/Interval.rb

Overview

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

Direct Known Subclasses

ScoreboardInterval, TimeInterval

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s, e) ⇒ Interval

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



26
27
28
29
30
31
32
33
# File 'lib/taskjuggler/Interval.rb', line 26

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

Instance Attribute Details

#endObject (readonly)

Returns the value of attribute end.



22
23
24
# File 'lib/taskjuggler/Interval.rb', line 22

def end
  @end
end

#startObject (readonly)

Returns the value of attribute start.



22
23
24
# File 'lib/taskjuggler/Interval.rb', line 22

def start
  @start
end

Instance Method Details

#<=>(iv) ⇒ Object

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

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
# File 'lib/taskjuggler/Interval.rb', line 85

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) ⇒ Object

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

Raises:

  • (ArgumentError)


96
97
98
99
# File 'lib/taskjuggler/Interval.rb', line 96

def ==(iv)
  raise ArgumentError, "Class mismatch" if self.class != iv.class
  @start == iv.start && @end == iv.end
end

#combine(iv) ⇒ Object

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

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/taskjuggler/Interval.rb', line 70

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) ⇒ Boolean

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

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
# File 'lib/taskjuggler/Interval.rb', line 37

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) ⇒ Object

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

Raises:

  • (ArgumentError)


61
62
63
64
65
66
# File 'lib/taskjuggler/Interval.rb', line 61

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) ⇒ Boolean

Check whether the Interval arg overlaps with this Interval.

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
# File 'lib/taskjuggler/Interval.rb', line 48

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