net.duckling.common.util.CommonUtils.java Source code

Java tutorial

Introduction

Here is the source code for net.duckling.common.util.CommonUtils.java

Source

/*
 * Copyright (c) 2008-2016 Computer Network Information Center (CNIC), Chinese Academy of Sciences.
 * 
 * This file is part of Duckling project.
 *
 * Licensed 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 net.duckling.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

/**
 * @author lvly
 * @since 2012-6-18
 * */
public final class CommonUtils {
    private CommonUtils() {
    }

    /**?.split?
     * @param array 
     * */
    public static boolean isNull(String[] array) {
        return (array == null || array.length == 0 || (array.length == 1 && "".equals(array[0])));
    }

    /**int
     * @param array int
     * */
    public static boolean isNull(int[] array) {
        return (array == null || array.length == 0);
    }

    /**?
     * @param list 
     * */
    public static boolean isNull(List<?> list) {
        return (list == null) || (list.size() == 0);
    }

    /**
     * ??
     * @param map Map
     * */
    public static boolean isNull(Map<?, ?> map) {
        return (map == null || (map.isEmpty()));
    }

    /**???
     * @param k ?
     * @return List<K>
     * */
    public static <K> List<K> getList(K k) {
        List<K> ks = new ArrayList<K>();
        ks.add(k);
        return ks;
    }

    /**??
     * @param List<K> 
     * @return K 
     * */
    public static <K> K first(List<K> list) {
        if (isNull(list)) {
            return null;
        }
        return list.get(0);
    }

    /**?
     * @param str the String what need to judice
     * @param boolean isNull?
     * */
    public static boolean isNull(String str) {
        return str == null || "".equals(str.trim()) || "null".equals(str.toLowerCase().trim());
    }

    /**?
     * @param str the String what need to judice
     * @param str never return null
     * */
    public static String killNull(String str) {
        return CommonUtils.isNull(str) ? "" : str.trim();
    }

    /**??List
     * @param array 
     * @return list ArrayList
     * */
    public static <K> List<K> array2List(K[] array) {
        List<K> list = new ArrayList<K>();
        if (array != null && array.length > 0) {
            for (K k : array) {
                list.add(k);
            }
        }
        return list;
    }

    /**
      * <p>String??Integer?id</p>
      *
      * <pre>
      * CommonUtils.string2Int(null)   = 0
      * CommonUtils.string2Int("")     = 0
      * CommonUtils.string2Int("12.3") = 0
      * CommonUtils.string2Int("a")    = 0
      * CommonUtils.string2Int("123")  = 123
      * </pre>
      *
     * @param sid 
     * @return Integer
      */

    public static int string2Id(String sid) {
        return string2Id(sid, 0);
    }

    /**
      * <p>String??Integer?id</p>
      *
      * <pre>
      * CommonUtils.string2Int(null)   = setDefault
      * CommonUtils.string2Int("")     = setDefault
      * CommonUtils.string2Int("12.3") = setDefault
      * CommonUtils.string2Int("a")    = setDefault
      * CommonUtils.string2Int("123")  = 123
      * </pre>
      *
     * @param sid 
     * @param setDefault ?,?setDefault
     * @return Integer
      */

    public static int string2Id(String sid, int setDefault) {
        return StringUtils.isEmpty(sid) ? setDefault
                : StringUtils.isNumeric(sid) ? Integer.parseInt(sid) : setDefault;
    }

    /**String??Integer
     * @param array 
     * @return array 
     * */
    public static Integer[] stringArray2IntArray(String[] array) {
        Integer[] result = new Integer[0];
        if (array != null) {
            result = new Integer[array.length];
            for (int i = 0; i < array.length; i++) {
                result[i] = Integer.parseInt(array[i]);
            }
        }
        return result;
    }

    /**??"," 
     * @param str ??
     * @param flag ","","
     * @return ??
     * */
    public static String format(String str, String flag) {
        if (CommonUtils.isNull(str)) {
            return "";
        }
        StringBuilder sb = new StringBuilder(str);
        if (sb.indexOf(flag) > 0) {
            return sb.substring(0, sb.lastIndexOf(flag));
        }
        return sb.toString();
    }

    /**??"," 
     * @param str ??,","
     * @return ??
     * */
    public static String format(String str) {
        return format(str, ",");
    }

}