Java tutorial
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you 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. */ package com.huawei.streaming.util; import java.util.ArrayList; import org.apache.commons.lang.ArrayUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * ?? */ public class StreamingUtils { /** * ? */ private static final Logger LOG = LoggerFactory.getLogger(StreamingUtils.class); /** * , */ private static final String EMPTY_STR = ""; /** * * @param statements ? */ public static void close(AutoCloseable... statements) { for (AutoCloseable sts : statements) { if (sts == null) { continue; } try { sts.close(); } catch (Exception e) { LOG.warn("Failed to close statement.", e); } } } /** * * * * jdk??String.split() * 10 String.split() 250 split 45 * * @param strOriginal ? * @param strSeparatorChars * @return ???? */ public static String[] split(String strOriginal, String strSeparatorChars) { return splitByWholeSeparator(strOriginal, strSeparatorChars, -1, false); } /** * * * * jdk??String.split() * 10 String.split() 250 split 45 * * @param strOriginal ? * @param strSeparatorChars * @param iMax ? * @return ???? */ public static String[] split(String strOriginal, String strSeparatorChars, int iMax) { return splitByWholeSeparator(strOriginal, strSeparatorChars, iMax, false); } /** * * * * jdk??String.split() * 10 String.split() 250 split 45 * * @param strOriginal ? * @param strSeparatorChars * @param iMax ?-1 * @param blPreserveAllTokens ?? * @return ???? */ public static String[] splitByWholeSeparator(String strOriginal, String strSeparatorChars, int iMax, boolean blPreserveAllTokens) { //??? if (strOriginal == null) { LOG.warn("strOriginal is null!"); return ArrayUtils.EMPTY_STRING_ARRAY; } //? int iLength = strOriginal.length(); if (iLength == 0) { LOG.warn("strOriginal is empty!"); return ArrayUtils.EMPTY_STRING_ARRAY; } //?? if ((strSeparatorChars == null) || (EMPTY_STR.equals(strSeparatorChars))) { LOG.warn("strSeparatorChars is invalid!"); return ArrayUtils.EMPTY_STRING_ARRAY; } // int iSeparatorLength = strSeparatorChars.length(); //??list ArrayList<String> substrList = new ArrayList<String>(); // int iNumberOfSubstrings = 0; //? int iBegin = 0; //?? int iEnd = 0; while (iEnd < iLength) { //? iEnd = strOriginal.indexOf(strSeparatorChars, iBegin); // if (iEnd > -1) { if (iEnd > iBegin) { iNumberOfSubstrings += 1; //??? if (iNumberOfSubstrings == iMax) { iEnd = iLength; substrList.add(strOriginal.substring(iBegin)); } else { //?? substrList.add(strOriginal.substring(iBegin, iEnd)); //? iBegin = iEnd + iSeparatorLength; } } else { //? if (blPreserveAllTokens) { iNumberOfSubstrings += 1; //??? if (iNumberOfSubstrings == iMax) { iEnd = iLength; substrList.add(strOriginal.substring(iBegin)); } //? else { substrList.add(EMPTY_STR); } } //? iBegin = iEnd + iSeparatorLength; } } //???? else { //???? substrList.add(strOriginal.substring(iBegin)); iEnd = iLength; } } //? return (String[]) substrList.toArray(new String[substrList.size()]); } }