|
/*
* Copyright (c) 2013, Yuriy Nelipovich
*
* If you find this code useful or in case of any questions, suggestions
* bug reports, donation, please email me: dev.yuriy.n@gmail.com
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace LogJoin
{
/// <summary>
/// Sequence of text portions, each portion is composed of a queue of text lines (called frame).
/// When moving forward, the frame is shifted by one line.
/// </summary>
public class MultilineInput : Input
{
private readonly int _frameSize;
private readonly Queue<string> _lines;
/// <summary>
/// Constructs new input
/// </summary>
/// <param name="filePaths">Sequence of files to read</param>
/// <param name="frameSize">Maximum size of text frame (i.e. maximum count of lines)</param>
public MultilineInput(string[] filePaths, int frameSize) : base(filePaths)
{
if (frameSize <= 0)
throw new ArgumentOutOfRangeException("frameSize",
string.Format("Lines count {0} is invalid", frameSize));
this._frameSize = frameSize;
this._lines = new Queue<string>(frameSize);
}
/// <summary>
/// Moves the input to the start position
/// </summary>
public override void ResetPosition()
{
base.ResetPosition();
this._lines.Clear();
}
/// <summary>
/// Rads the next text line, pushes it to the frame and moves to the next position
/// </summary>
/// <returns>Frame contents</returns>
public override string ReadTextPortion()
{
if (this._lines.Count == 0)
this.FillFrameWithInitialText();
else
this.ReadNextLineAndRoll();
if (this._lines.Count == 0)
return null;
var result = new StringBuilder();
foreach (var line in this._lines)
{
result.AppendLine(line);
}
return result.ToString();
}
/// <summary>
/// Fills the frame with initial text
/// </summary>
private void FillFrameWithInitialText()
{
string nextLine;
do
{
nextLine = base.ReadTextPortion();
if (nextLine != null)
this._lines.Enqueue(nextLine);
}
while (nextLine != null && this._lines.Count < this._frameSize);
}
/// <summary>
/// Rads the next text line, pushes it to the frame
/// and ensures that the frame size is not exceeded
/// </summary>
private void ReadNextLineAndRoll()
{
var next = base.ReadTextPortion();
if (next != null)
this._lines.Enqueue(next);
if (this._lines.Count > 0)
this._lines.Dequeue();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.