Java Iterable getLongestCommonToken(Iterable iterable, char tokenSeparatorChar)

Here you can find the source of getLongestCommonToken(Iterable iterable, char tokenSeparatorChar)

Description

get Longest Common Token

License

Open Source License

Declaration

public static String getLongestCommonToken(Iterable<String> iterable, char tokenSeparatorChar) 

Method Source Code

//package com.java2s;
/**//  w w w  . j  av  a 2 s. co  m
 * eobjects.org AnalyzerBeans
 * Copyright (C) 2010 eobjects.org
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */

import java.util.Iterator;

public class Main {
    public static String getLongestCommonToken(Iterable<String> iterable, char tokenSeparatorChar) {
        Iterator<String> it = iterable.iterator();
        String commonToken = it.next();
        while (it.hasNext()) {
            if (commonToken == "") {
                return null;
            }
            String name = it.next();
            if (!name.startsWith(commonToken)) {
                commonToken = getLongestCommonToken(commonToken, name, tokenSeparatorChar);
            }
        }
        return commonToken;
    }

    public static String getLongestCommonToken(String str1, String str2, char tokenSeparatorChar) {
        StringBuilder result = new StringBuilder();
        String[] tokens1 = str1.split("\\" + tokenSeparatorChar);
        String[] tokens2 = str2.split("\\" + tokenSeparatorChar);
        for (int i = 0; i < Math.min(tokens1.length, tokens2.length); i++) {
            if (!tokens1[i].equals(tokens2[i])) {
                break;
            }
            if (i != 0) {
                result.append(tokenSeparatorChar);
            }
            result.append(tokens1[i]);
        }
        return result.toString();
    }
}

Related

  1. fillFromIterable(C c, Iterable i)
  2. getAny(Iterable iterable)
  3. getByIndex(Iterable iterable, int index)
  4. getElement(final Iterable iterable, int index)
  5. getIdenticalElement(Iterable iterable)
  6. getMessageString(Iterable msgs)
  7. getUnique(Iterable iterable)
  8. implodeStrings(Iterable strings, String glue)
  9. indexOfSame(Iterable iterable, T obj)