001package org.unix4j.util;
002
003import org.unix4j.line.Line;
004
005import java.util.List;
006
007/**
008 * Contains static utility methods related to {@link Line} objects.
009 */
010public class LineUtil {
011
012        /**
013         * Returns a multi-line representation of the provided {@code lines}. The
014         * last line of the returned string is never terminated, all other lines are
015         * terminated with guarantee even if the line itself has an empty line
016         * ending string.
017         * 
018         * @return a multi-line string of the buffered lines, without line
019         *         termination for the last line
020         */
021        public static String toMultiLineString(List<? extends Line> lines) {
022                final StringBuilder sb = new StringBuilder();
023                Line lastTerminatedLine = Line.EMPTY_LINE;
024                for (int i = 0; i < lines.size(); i++) {
025                        final Line line = lines.get(i);
026                        sb.append(line.getContent());
027                        if (i + 1 < lines.size()) {
028                                if (line.getLineEndingLength() > 0) {
029                                        sb.append(line.getLineEnding());
030                                        lastTerminatedLine = line;
031                                } else {
032                                        sb.append(lastTerminatedLine.getLineEnding());
033                                }
034                        }
035                }
036                return sb.toString();
037        }
038
039        // no instances
040        private LineUtil() {
041                super();
042        }
043}