Java List Print printTwoColumns(List left, List right, int leftWidth, int rightWidth, int sep)

Here you can find the source of printTwoColumns(List left, List right, int leftWidth, int rightWidth, int sep)

Description

print Two Columns

License

Open Source License

Declaration

public static String printTwoColumns(List<String> left, List<String> right, int leftWidth, int rightWidth,
            int sep) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    public static String printTwoColumns(List<String> left, List<String> right, int leftWidth, int rightWidth,
            int sep) {
        return printTwoColumns(left, right, leftWidth, rightWidth, sep, "");
    }//  w ww  .  ja va2  s  . c  o m

    public static String printTwoColumns(List<String> left, List<String> right, int leftWidth, int rightWidth,
            int sep, String betweenLines) {
        return printTwoColumns(0, left, right, leftWidth, rightWidth, sep, betweenLines);
    }

    public static String printTwoColumns(int offset, List<String> left, List<String> right, int leftWidth,
            int rightWidth, int sep, String betweenLines) {
        if (left.size() != right.size())
            throw new IllegalArgumentException();
        left = new ArrayList<>(left);
        right = new ArrayList<>(right);
        boolean breakOnNext;
        String spacer = spacer(sep), offsetSpacer = spacer(offset);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < left.size(); ++i) {
            String le = left.get(i), ri = right.get(i);
            breakOnNext = true;
            if (le.length() >= leftWidth && ri.length() >= rightWidth) {
                int leBr = lineBreakPos(le, leftWidth), riBr = lineBreakPos(ri, rightWidth);
                String l = le.substring(0, leBr), r = right.get(i).substring(0, riBr);
                String l1 = le.substring(leBr), r1 = right.get(i).substring(riBr);
                le = l;
                ri = r;
                left.add(i + 1, l1);
                right.add(i + 1, r1);
            } else if (le.length() >= leftWidth) {
                int leBr = lineBreakPos(le, leftWidth);
                String l = le.substring(0, leBr), l1 = le.substring(leBr);
                le = l;
                left.add(i + 1, l1);
                right.add(i + 1, "");
            } else if (ri.length() >= rightWidth) {
                int riBr = lineBreakPos(ri, rightWidth);
                String r = ri.substring(0, riBr), r1 = ri.substring(riBr);
                ri = r;
                right.add(i + 1, r1);
                left.add(i + 1, "");
            } else
                breakOnNext = false;
            sb.append(offsetSpacer).append(le).append(spacer).append(spacer(leftWidth - le.length())).append(ri)
                    .append('\n');
            if (!breakOnNext)
                sb.append(betweenLines);
        }
        assert left.size() == right.size();
        return sb.toString();
    }

    public static String spacer(int sep) {
        StringBuilder sb = new StringBuilder(sep);
        for (int i = 0; i < sep; ++i)
            sb.append(" ");
        return sb.toString();
    }

    private static int lineBreakPos(String str, int width) {
        int i = width - 1;
        for (; i >= 0; --i)
            if (str.charAt(i) == ' ')
                break;
        if (i <= 3)
            return width - 1;
        return i + 1;
    }
}

Related

  1. printStringList(List listToPrint)
  2. printStringList(List strings)
  3. printTable(List data)
  4. printTopNTokens(LinkedList> list, int n)
  5. printTuple(List tuple)
  6. printValues(List values)
  7. printWithBars(int[] maxLength, String[] headings, List rows)

  8. HOME | Copyright © www.java2s.com 2016