Java String Split splitForIndexMatching(String string)

Here you can find the source of splitForIndexMatching(String string)

Description

Split so that we can create multiple WildcardQuery.

License

Open Source License

Declaration

public static List<String> splitForIndexMatching(String string) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (C) 2012-2013  Fabio Zadrozny and others
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*w w  w . jav  a  2 s .co m*/
 *     Fabio Zadrozny <fabiofz@gmail.com>    - initial API and implementation
 *     Jonah Graham <jonah@kichwacoders.com> - ongoing maintenance
 ******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Split so that we can create multiple WildcardQuery.
     *
     * Note that it accepts wildcards (such as * or ? but if an entry would contain
     * only wildcards it'd be ignored).
     *
     * Also, anything which Character.isJavaIdentifierPart does not match is considered
     * to be a separator and will be ignored.
     */
    public static List<String> splitForIndexMatching(String string) {
        int len = string.length();
        if (len == 0) {
            return new ArrayList<>(0);
        }
        ArrayList<String> ret = new ArrayList<String>();

        int last = 0;

        char c = 0;

        for (int i = 0; i < len; i++) {
            c = string.charAt(i);
            if (!Character.isJavaIdentifierPart(c) && c != '*' && c != '?') {
                if (last != i) {
                    String substring = string.substring(last, i);
                    if (!containsOnlyWildCards(substring)) {
                        ret.add(substring);
                    }
                }
                while (!Character.isJavaIdentifierPart(c) && c != '*'
                        && c != '?' && i < len - 1) {
                    i++;
                    c = string.charAt(i);
                }
                last = i;
            }
        }
        if (Character.isJavaIdentifierPart(c) || c == '*' || c == '?') {
            if (last == 0 && len > 0) {
                if (!containsOnlyWildCards(string)) {
                    ret.add(string); //it is equal to the original (no char to split)
                }

            } else if (last < len) {
                String substring = string.substring(last, len);
                if (!containsOnlyWildCards(substring)) {
                    //Don't add if it has only wildcards in it.
                    ret.add(substring);
                }
            }
        }
        return ret;
    }

    public static boolean containsOnlyWildCards(String string) {
        boolean onlyWildCardsInPart = true;
        int length = string.length();
        for (int i = 0; i < length; i++) {
            char c = string.charAt(i);
            if (c != '*' && c != '?') {
                onlyWildCardsInPart = false;
                break; //break inner for
            }
        }
        return onlyWildCardsInPart;
    }
}

Related

  1. splitDigits(long input)
  2. splitDomains(String domains)
  3. splitElements(String values)
  4. splitEx(String str, String spilter)
  5. splitFields(String fieldsString, int minNum)
  6. splitGenericIfNeeded(String name)
  7. splitHelperName(String name)
  8. splitHistory(String history)
  9. splitHTMLTags(final String string)