Java List Concatenate concatElementsWithBrackets(List input)

Here you can find the source of concatElementsWithBrackets(List input)

Description

This method concats elements if they contain open brackets so it's not split over multiple elements

License

Apache License

Parameter

Parameter Description
result a parameter

Declaration

public static List<String> concatElementsWithBrackets(List<String> input) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**//  w w w. j  a  v  a 2 s.com
     * This method concats elements if they contain open brackets so it's not split over multiple elements
     * @param result
     * @return
     */
    public static List<String> concatElementsWithBrackets(List<String> input) {
        List<String> result = new ArrayList<String>();
        int openBracketCounter = 0;
        String currentElement = null;
        for (String el : input) {
            openBracketCounter = openBracketCounter + getAmountOfCharInString('(', el)
                    - getAmountOfCharInString(')', el);

            if (currentElement == null) {
                currentElement = el;
            } else {
                currentElement += ", " + el;
            }
            if (openBracketCounter == 0) {
                result.add(currentElement);
                currentElement = null;
            }
        }

        return result;
    }

    /**********************************************
     *  Element counters
     ***********************************************/

    public static int getAmountOfCharInString(char toCount, String string) {
        int result = 0;
        for (int i = 0; i < string.length(); i++) {
            if (string.charAt(i) == toCount) {
                result += 1;
            }
        }
        return result;
    }
}

Related

  1. concatAll(final T[] empty, Iterable arrayList)
  2. concatArrays(List arrays, int start, int size)
  3. concatArraysToList(final T[]... arrays)
  4. concatCsvLine(List values, String fieldSeparator)
  5. concateAliases(List aliases)
  6. concatenate(List aList)
  7. concatenate(List> lists)
  8. concatenate(List list1, List list2)
  9. concatenate(List components)

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