public static Collection splitByLength()

in src/main/java/com/amazonaws/services/neptune/io/RecordSplitter.java [49:90]


    public static Collection<String> splitByLength(String s, int length, int wordBoundaryMargin) {

        int startIndex = 0;

        Collection<String> results = new ArrayList<>();

        while (startIndex < s.length()) {

            boolean foundWordBoundary = false;

            int endIndex = Math.min(startIndex + length, s.length());
            int minCandidateEndIndex = Math.max(startIndex +1, endIndex - wordBoundaryMargin);

            for (int actualEndIndex = endIndex; actualEndIndex >= minCandidateEndIndex; actualEndIndex--){

                if (!StringUtils.isAlphanumeric( s.substring(actualEndIndex - 1, actualEndIndex))){

                    String result = s.substring(startIndex, actualEndIndex);
                    String trimmedResult = result.trim();
                    if (StringUtils.isNotEmpty(trimmedResult)){
                        results.add(trimmedResult);
                    }
                    startIndex = actualEndIndex;
                    foundWordBoundary = true;
                    break;
                }
            }

            if (!foundWordBoundary){
                String result = s.substring(startIndex, endIndex);
                String trimmedResult = result.trim();
                if (StringUtils.isNotEmpty(trimmedResult)){
                    results.add(trimmedResult);
                }
                startIndex = endIndex;
            }

        }

        return results;

    }