首页

关于drools-core源码包中StringUtils字符串工具类对字符串转换及判断处理

标签:StringUtils,字符串工具栏,drools-core源码包     发布时间:2018-04-11   

一、前言

关于drools-core源码包中的org.drools.util.StringUtils字符串工具类,将首先字母转为大写ucFirst、字符串的分隔处理split、字符分隔splitWorker等。

二、源码说明

package org.drools.util;@b@@b@import java.io.IOException;@b@import java.io.Reader;@b@import java.util.ArrayList;@b@import java.util.List;@b@@b@public class StringUtils@b@{@b@  public static final String[] EMPTY_STRING_ARRAY = new String[0];@b@  public static final String EMPTY = "";@b@  public static final int INDEX_NOT_FOUND = -1;@b@  private static final int PAD_LIMIT = 8192;@b@@b@  public static String ucFirst(String name)@b@  {@b@    return name.toUpperCase().charAt(0) + name.substring(1);@b@  }@b@@b@  public static boolean isEmpty(String str)@b@  {@b@    return ((str == null) || (str.length() == 0));@b@  }@b@@b@  public static String repeat(String str, int repeat)@b@  {@b@    if (str == null)@b@      return null;@b@@b@    if (repeat <= 0)@b@      return "";@b@@b@    int inputLength = str.length();@b@    if ((repeat == 1) || (inputLength == 0))@b@      return str;@b@@b@    if ((inputLength == 1) && (repeat <= 8192)) {@b@      return padding(repeat, str.charAt(0));@b@    }@b@@b@    int outputLength = inputLength * repeat;@b@    switch (inputLength)@b@    {@b@    case 1:@b@      char ch = str.charAt(0);@b@      char[] output1 = new char[outputLength];@b@      for (int i = repeat - 1; i >= 0; --i)@b@        output1[i] = ch;@b@@b@      return new String(output1);@b@    case 2:@b@      char ch0 = str.charAt(0);@b@      char ch1 = str.charAt(1);@b@      char[] output2 = new char[outputLength];@b@      for (int i = repeat * 2 - 2; i >= 0; ) {@b@        output2[i] = ch0;@b@        output2[(i + 1)] = ch1;@b@@b@        --i; --i;@b@      }@b@@b@      return new String(output2);@b@    }@b@    StringBuffer buf = new StringBuffer(outputLength);@b@    for (int i = 0; i < repeat; ++i)@b@      buf.append(str);@b@@b@    return buf.toString();@b@  }@b@@b@  public static String[] split(String str)@b@  {@b@    return split(str, null, -1);@b@  }@b@@b@  public static String[] split(String str, char separatorChar)@b@  {@b@    return splitWorker(str, separatorChar, false);@b@  }@b@@b@  public static String[] split(String str, String separatorChars)@b@  {@b@    return splitWorker(str, separatorChars, -1, false);@b@  }@b@@b@  public static String[] split(String str, String separatorChars, int max)@b@  {@b@    return splitWorker(str, separatorChars, max, false);@b@  }@b@@b@  public static String[] splitPreserveAllTokens(String str)@b@  {@b@    return splitWorker(str, null, -1, true);@b@  }@b@@b@  public static String[] splitPreserveAllTokens(String str, char separatorChar)@b@  {@b@    return splitWorker(str, separatorChar, true);@b@  }@b@@b@  private static String[] splitWorker(String str, char separatorChar, boolean preserveAllTokens)@b@  {@b@    if (str == null)@b@      return null;@b@@b@    int len = str.length();@b@    if (len == 0)@b@      return EMPTY_STRING_ARRAY;@b@@b@    List list = new ArrayList();@b@    int i = 0; int start = 0;@b@    boolean match = false;@b@    boolean lastMatch = false;@b@    while (true) { while (true) { if (i >= len) break label109;@b@        if (str.charAt(i) != separatorChar) break;@b@        if ((match) || (preserveAllTokens)) {@b@          list.add(str.substring(start, i));@b@@b@          match = false;@b@          lastMatch = true;@b@        }@b@        start = ++i;@b@      }@b@@b@      lastMatch = false;@b@@b@      match = true;@b@      ++i;@b@    }@b@    if ((match) || ((preserveAllTokens) && (lastMatch))) {@b@      label109: list.add(str.substring(start, i));@b@    }@b@@b@    return ((String[])(String[])list.toArray(new String[list.size()]));@b@  }@b@@b@  public static String[] splitPreserveAllTokens(String str, String separatorChars)@b@  {@b@    return splitWorker(str, separatorChars, -1, true);@b@  }@b@@b@  public static String[] splitPreserveAllTokens(String str, String separatorChars, int max)@b@  {@b@    return splitWorker(str, separatorChars, max, true);@b@  }@b@@b@  private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens)@b@  {@b@    if (str == null)@b@      return null;@b@@b@    int len = str.length();@b@    if (len == 0)@b@      return EMPTY_STRING_ARRAY;@b@@b@    List list = new ArrayList();@b@    int sizePlus1 = 1;@b@    int i = 0; int start = 0;@b@    boolean match = false;@b@    boolean lastMatch = false;@b@    if (separatorChars == null) while (true) {@b@        while (true) {@b@          if (i >= len) break label331;@b@          if (!(Character.isWhitespace(str.charAt(i)))) break;@b@          if ((match) || (preserveAllTokens)) {@b@            lastMatch = true;@b@            if (sizePlus1++ == max) {@b@              i = len;@b@              lastMatch = false;@b@            }@b@            list.add(str.substring(start, i));@b@@b@            match = false;@b@          }@b@          start = ++i;@b@        }@b@@b@        lastMatch = false;@b@@b@        match = true;@b@        ++i;@b@      }@b@    if (separatorChars.length() == 1)@b@    {@b@      char sep = separatorChars.charAt(0);@b@      while (true) { while (true) { if (i >= len) break label239;@b@          if (str.charAt(i) != sep) break;@b@          if ((match) || (preserveAllTokens)) {@b@            lastMatch = true;@b@            if (sizePlus1++ == max) {@b@              i = len;@b@              lastMatch = false;@b@            }@b@            list.add(str.substring(start, i));@b@@b@            match = false;@b@          }@b@          start = ++i;@b@        }@b@@b@        lastMatch = false;@b@@b@        match = true;@b@        label239: ++i; }@b@    } else {@b@      while (true) {@b@        while (true) {@b@          if (i >= len) break label331;@b@          if (separatorChars.indexOf(str.charAt(i)) < 0) break;@b@          if ((match) || (preserveAllTokens)) {@b@            lastMatch = true;@b@            if (sizePlus1++ == max) {@b@              i = len;@b@              lastMatch = false;@b@            }@b@            list.add(str.substring(start, i));@b@@b@            match = false;@b@          }@b@          start = ++i;@b@        }@b@@b@        lastMatch = false;@b@@b@        match = true;@b@        ++i;@b@      }@b@    }@b@    if ((match) || ((preserveAllTokens) && (lastMatch))) {@b@      label331: list.add(str.substring(start, i));@b@    }@b@@b@    return ((String[])(String[])list.toArray(new String[list.size()]));@b@  }@b@@b@  public static String padding(int repeat, char padChar)@b@    throws IndexOutOfBoundsException@b@  {@b@    if (repeat < 0)@b@      throw new IndexOutOfBoundsException("Cannot pad a negative amount: " + repeat);@b@@b@    char[] buf = new char[repeat];@b@    for (int i = 0; i < buf.length; ++i)@b@      buf[i] = padChar;@b@@b@    return new String(buf);@b@  }@b@@b@  public static String readFileAsString(Reader reader)@b@  {@b@    StringBuffer fileData;@b@    try {@b@      fileData = new StringBuffer(1000);@b@      char[] buf = new char[1024];@b@      int numRead = 0;@b@      while ((numRead = reader.read(buf)) != -1) {@b@        String readData = String.valueOf(buf, 0, numRead);@b@@b@        fileData.append(readData);@b@        buf = new char[1024];@b@      }@b@      reader.close();@b@      return fileData.toString();@b@    } catch (IOException e) {@b@      throw new RuntimeException(e);@b@    }@b@  }@b@}