Class: Stream::ConcatenatedStream

Inherits:
WrappedStream show all
Defined in:
lib/stream.rb

Overview

Given a stream of streams. Than a ConcatenatedStream is obtained by concatenating these in the given order. A ConcatenatedStream is created by the methods Stream#concatenate or Stream#concatenate_collected send to a stream of streams or by the method + which concatenats two streams:

((1..3).create_stream + [4,5].create_stream).to_a ==> [1, 2, 3, 4, 5]

Instance Method Summary collapse

Constructor Details

#initialize(streamOfStreams) ⇒ ConcatenatedStream

Creates a new ConcatenatedStream wrapping the stream of streams streamOfStreams.



510
511
512
513
# File 'lib/stream.rb', line 510

def initialize(streamOfStreams)
  super
  set_to_begin
end

Instance Method Details

#at_beginning?Boolean

Same as at_end? the other way round.



543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'lib/stream.rb', line 543

def at_beginning?
  # same algorithm as at_end? the other way round.
  unless @current_stream.at_beginning?
    return false
  end

  until streamOfStreams.at_beginning?
    dir = @dir_of_last_move
    @dir_of_last_move = :backward
    s = streamOfStreams.basic_backward
    next if dir == :forward

    s.set_to_end
    if s.at_beginning?
      next
    else
      @current_stream = s
      return false
    end
  end
  reached_boundary
end

#at_end?Boolean

If the current stream is at end, than at_end? has to look ahead to find a non empty in the stream of streams, which than gets the current stream.



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/stream.rb', line 517

def at_end?
  unless @current_stream.at_end?
    return false
  end

  until streamOfStreams.at_end?
    dir = @dir_of_last_move
    @dir_of_last_move = :forward
    s = streamOfStreams.basic_forward
    # if last move was backwards, then @current_stream is
    # equivalent to s. Move to next stream.
    next if dir == :backward

    s.set_to_begin
    if s.at_end? # empty stream?
      next # skip it
    else
      @current_stream = s
      return false # found non empty stream
    end
  end # until
  reached_boundary # sets @dir_of_last_move and @current_stream
end

#basic_backwardObject

Returns the previous element of @current_stream. at_beginning? ensured that there is one.



582
583
584
# File 'lib/stream.rb', line 582

def basic_backward
  @current_stream.basic_backward
end

#basic_forwardObject

Returns the next element of @current_stream. at_end? ensured that there is one.



576
577
578
# File 'lib/stream.rb', line 576

def basic_forward
  @current_stream.basic_forward
end

#set_to_beginObject



566
567
568
# File 'lib/stream.rb', line 566

def set_to_begin
  super; reached_boundary
end

#set_to_endObject



570
571
572
# File 'lib/stream.rb', line 570

def set_to_end
  super; reached_boundary
end