Java List Value Add All addAllWords(String text, List result)

Here you can find the source of addAllWords(String text, List result)

Description

add All Words

License

Apache License

Declaration

private static void addAllWords(String text, List<String> result) 

Method Source Code

//package com.java2s;
/*/*from   w  ww . j  a v a 2s. c o m*/
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.List;

public class Main {
    private static void addAllWords(String text, List<String> result) {
        int start = 0;
        while (start < text.length()) {
            int next = nextWord(text, start);
            result.add(text.substring(start, next));
            start = next;
        }
    }

    static int nextWord(String text, int start) {
        if (!Character.isLetterOrDigit(text.charAt(start))) {
            return start + 1;
        }

        int i = start;
        boolean prevIsLetterOrDigit = i > 0 && Character.isLetterOrDigit(text.charAt(i - 1));
        while (i < text.length()) {
            char c = text.charAt(i);
            if (!isWordStart(c)) {
                if (!Character.isLetterOrDigit(c)) {
                    break;
                }
                if (prevIsLetterOrDigit) {
                    break;
                }
            }
            prevIsLetterOrDigit = true;
            i++;
        }

        if (i > start + 1) {
            if (i == text.length() || !Character.isLetterOrDigit(text.charAt(i))) {
                return i;
            }
            return i - 1;
        }
        /*boolean */prevIsLetterOrDigit = i > 0 && Character.isLetterOrDigit(text.charAt(i - 1));
        while (i < text.length()) {
            char c = text.charAt(i);
            if (!Character.isLetterOrDigit(c))
                break;
            if (isWordStart(c))
                break;
            if (!prevIsLetterOrDigit)
                break;
            prevIsLetterOrDigit = true;
            i++;
        }
        return i;
    }

    static boolean isWordStart(char p) {
        return Character.isUpperCase(p) || Character.isDigit(p);
    }
}

Related

  1. addAllToList(List dest, Collection src)
  2. addAllUnique(List aList, List theObjects)
  3. addAllUnique(List l1, T element)
  4. addAllUniqueId(List aList, List theObjects)
  5. addAllUniqueToList(List objects, List objectsToAdd)
  6. addArrayToList(List list, String as[])
  7. addArrayToList(List list, double[] array)
  8. addArrayToList(List list, T[] array)
  9. addArrayToList(String[] array, List list)