private List<String> getReformatedPieces(String in, PieceMarkerSpec[] markers) {
    CodeReformatorKernel kernel =
        new CodeReformatorKernel(_statementSeparator, markers, _commentSpecs);
    String[] pieces = kernel.toPieces(in);
    ArrayList<String> piecesBuf = new ArrayList<String>();

    for (int i = 0; i < pieces.length; ++i) {
      if (TRY_SPLIT_LINE_LEN < pieces[i].length()) {
        String[] splitPieces = trySplit(pieces[i], 0, TRY_SPLIT_LINE_LEN);
        piecesBuf.addAll(Arrays.asList(splitPieces));
      } else {
        piecesBuf.add(pieces[i]);
      }
    }
    return piecesBuf;
  }
  boolean hasCommentEndingWithLineFeed(String in) {
    CodeReformatorKernel dum =
        new CodeReformatorKernel(_statementSeparator, new PieceMarkerSpec[0], _commentSpecs);
    StateOfPosition[] sops = dum.getStatesOfPosition(in);

    boolean inComment = false;
    for (int i = 0; i < sops.length; ++i) {
      if (!inComment && -1 < sops[i].commentIndex) {
        if (-1 < _commentSpecs[sops[i].commentIndex].commentEnd.indexOf('\n')) {
          return true;
        }
        inComment = true;
      }
      if (-1 == sops[i].commentIndex) {
        inComment = false;
      }
    }
    return false;
  }
  private int[] getTopLevelBraketIndexes(String piece, CodeReformatorKernel crk) {
    int[] ret = new int[2];
    ret[0] = -1;
    ret[1] = -1;

    StateOfPosition[] stateOfPositions = crk.getStatesOfPosition(piece);

    int bra = piece.indexOf("(");
    while (-1 != bra) {
      crk.getStatesOfPosition(piece);

      if (0 == bra || stateOfPositions[bra - 1].isTopLevel) {
        ret[0] = bra;
        break; // break when first braket found
      }
      if (bra < piece.length() - 1) {
        bra = piece.indexOf("(", bra + 1);
      } else {
        break;
      }
    }

    if (-1 == ret[0]) {
      return ret;
    }

    int ket = piece.indexOf(")", bra);
    while (-1 != ket) {
      if (ket == piece.length() - 1 || stateOfPositions[ket].isTopLevel) {
        // the next top level ket is the counterpart to bra
        ret[1] = ket;
        break;
      }
      if (ket < piece.length() - 1) {
        ket = piece.indexOf(")", ket + 1);
      } else {
        break;
      }
    }
    return ret;
  }
  private boolean hasTopLevelColon(String piece, CodeReformatorKernel crk) {
    int ix = piece.indexOf(",");
    StateOfPosition[] stateOfPositions = crk.getStatesOfPosition(piece);

    while (-1 != ix) {
      if (stateOfPositions[ix].isTopLevel) {
        return true;
      }
      if (ix < piece.length() - 1) {
        ix = piece.indexOf(",", ix + 1);
      } else {
        break;
      }
    }

    return false;
  }
  private String[] trySplit(String piece, int braketDepth, int trySplitLineLen) {
    String trimmedPiece = piece.trim();
    CodeReformatorKernel dum =
        new CodeReformatorKernel(_statementSeparator, new PieceMarkerSpec[0], _commentSpecs);

    if (hasTopLevelColon(trimmedPiece, dum)) {
      PieceMarkerSpec[] pms = createPieceMarkerSpecIncludeColon();
      CodeReformatorKernel crk = new CodeReformatorKernel(_statementSeparator, pms, _commentSpecs);
      String[] splitPieces1 = crk.toPieces(trimmedPiece);
      if (1 == splitPieces1.length) {
        return splitPieces1;
      }

      ArrayList<String> ret = new ArrayList<String>();

      for (int i = 0; i < splitPieces1.length; ++i) {
        if (trySplitLineLen < splitPieces1[i].length() + braketDepth * INDENT.length()) {
          String[] splitPieces2 = trySplit(splitPieces1[i], braketDepth, trySplitLineLen);
          for (int j = 0; j < splitPieces2.length; ++j) {
            ret.add(splitPieces2[j].trim());
          }
        } else {
          ret.add(splitPieces1[i].trim());
        }
      }
      return (purgeEmptyStrings(ret)).toArray(new String[0]);
    } else {
      int[] tlbi = getTopLevelBraketIndexes(trimmedPiece, dum);
      if (-1 != tlbi[0] && tlbi[0] < tlbi[1]) {
        // ////////////////////////////////////////////////////////////////////////
        // Split the first two matching toplevel brakets here
        PieceMarkerSpec[] pms = createPieceMarkerSpecExcludeColon();
        CodeReformatorKernel crk =
            new CodeReformatorKernel(_statementSeparator, pms, _commentSpecs);
        String[] splitPieces1 = crk.toPieces(trimmedPiece.substring(tlbi[0] + 1, tlbi[1]));

        ArrayList<String> buf = new ArrayList<String>();
        buf.add(trimmedPiece.substring(0, tlbi[0]).trim());
        buf.add("(");
        for (int i = 0; i < splitPieces1.length; ++i) {
          buf.add(splitPieces1[i]);
        }
        buf.add(")");
        if (tlbi[1] + 1 < trimmedPiece.length()) {
          buf.add(trimmedPiece.substring(tlbi[1] + 1, trimmedPiece.length()).trim());
        }
        splitPieces1 = buf.toArray(new String[0]);
        //
        // ////////////////////////////////////////////////////////////////////

        // ///////////////////////////////////////////////////////////////////
        // Now check length of Strings in splitPieces1 again
        ArrayList<String> ret = new ArrayList<String>();
        for (int i = 0; i < splitPieces1.length; ++i) {
          if (trySplitLineLen < splitPieces1[i].length() + braketDepth * INDENT.length()) {
            String[] splitPieces2 = trySplit(splitPieces1[i], braketDepth + 1, trySplitLineLen);
            for (int j = 0; j < splitPieces2.length; ++j) {
              ret.add(splitPieces2[j]);
            }
          } else {
            ret.add(splitPieces1[i]);
          }
        }
        //
        // ///////////////////////////////////////////////////////////////////

        return (purgeEmptyStrings(ret)).toArray(new String[0]);
      } else {
        return new String[] {piece};
      }
    }
  }