001    /*
002     * Copyright (C) 2010 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.util;
021    
022    import java.util.ArrayList;
023    import java.util.Arrays;
024    import java.util.List;
025    import java.util.regex.Matcher;
026    import java.util.regex.Pattern;
027    
028    /**
029     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
030     * @version $Revision$
031     */
032    public class Strings {
033    
034      /** . */
035      private static final Pattern p = Pattern.compile("\\S+");
036    
037      public static List<String> chunks(CharSequence s) {
038        List<String> chunks = new ArrayList<String>();
039        Matcher m = p.matcher(s);
040        while (m.find()) {
041          chunks.add(m.group());
042        }
043        return chunks;
044      }
045    
046      /**
047       * @see #findLongestCommonPrefix(Iterable)
048       */
049      public static String findLongestCommonPrefix(CharSequence... seqs) {
050        return findLongestCommonPrefix(Arrays.asList(seqs));
051      }
052    
053      /**
054       * Find the longest possible common prefix of the provided char sequence.
055       *
056       * @param seqs the sequences
057       * @return the longest possible prefix
058       */
059      public static String findLongestCommonPrefix(Iterable<? extends CharSequence> seqs) {
060        String common = "";
061        out:
062        while (true) {
063          String candidate = null;
064          for (CharSequence s : seqs) {
065            if (common.length() + 1 > s.length()) {
066              break out;
067            } else {
068              if (candidate == null) {
069                candidate = s.subSequence(0, common.length() + 1).toString();
070              } else if (s.subSequence(0, common.length() + 1).toString().equals(candidate)) {
071                // Ok it is a prefix
072              } else {
073                break out;
074              }
075            }
076          }
077          if (candidate == null) {
078            break;
079          } else {
080            common = candidate;
081          }
082        }
083        return common;
084      }
085    }