class Stream::ConcatenatedStream

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]

Public Class Methods

new(streamOfStreams) click to toggle source

Creates a new ConcatenatedStream wrapping the stream of streams streamOfStreams.

Calls superclass method Stream::WrappedStream::new
    # File lib/stream.rb
500 def initialize(streamOfStreams)
501   super
502   set_to_begin
503 end

Public Instance Methods

at_beginning?() click to toggle source

Same as at_end? the other way round. @return [Boolean]

    # File lib/stream.rb
533 def at_beginning?
534   # same algorithm as at_end? the other way round.
535   unless @current_stream.at_beginning?
536     return false
537   end
538 
539   until streamOfStreams.at_beginning?
540     dir = @dir_of_last_move
541     @dir_of_last_move = :backward
542     s = streamOfStreams.basic_backward
543     next if dir == :forward
544 
545     s.set_to_end
546     if s.at_beginning?
547       next
548     else
549       @current_stream = s
550       return false
551     end
552   end
553   reached_boundary
554 end
at_end?() click to toggle source

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.

    # File lib/stream.rb
507 def at_end?
508   unless @current_stream.at_end?
509     return false
510   end
511 
512   until streamOfStreams.at_end?
513     dir = @dir_of_last_move
514     @dir_of_last_move = :forward
515     s = streamOfStreams.basic_forward
516     # if last move was backwards, then @current_stream is
517     # equivalent to s. Move to next stream.
518     next if dir == :backward
519 
520     s.set_to_begin
521     if s.at_end? # empty stream?
522       next # skip it
523     else
524       @current_stream = s
525       return false # found non empty stream
526     end
527   end # until
528   reached_boundary # sets @dir_of_last_move and @current_stream
529 end
basic_backward() click to toggle source

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

    # File lib/stream.rb
572 def basic_backward
573   @current_stream.basic_backward
574 end
basic_forward() click to toggle source

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

    # File lib/stream.rb
566 def basic_forward
567   @current_stream.basic_forward
568 end
set_to_begin() click to toggle source
Calls superclass method Stream::WrappedStream#set_to_begin
    # File lib/stream.rb
556 def set_to_begin
557   super; reached_boundary
558 end
set_to_end() click to toggle source
Calls superclass method Stream::WrappedStream#set_to_end
    # File lib/stream.rb
560 def set_to_end
561   super; reached_boundary
562 end

Private Instance Methods

reached_boundary() click to toggle source
    # File lib/stream.rb
578 def reached_boundary
579   @current_stream = EmptyStream.instance
580   @dir_of_last_move = :none # not :forward or :backward
581   true
582 end