org.richie.codeGen.core.util.StringUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.richie.codeGen.core.util.StringUtil.java

Source

/*
 * Copyright 2013  elfkingw
 *
 * 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.
 */
// Created on 2013-6-29
package org.richie.codeGen.core.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.richie.codeGen.core.model.Column;

/**
 * ?  commons-lang-2.4.jarStringUtils<br>
 *  stringUtils <br>
 * ? $stringUtils.getClassName(**)
 * 
 * @author elfkingw 2013-6-29
 */
public class StringUtil extends StringUtils {

    /**
     * change the String to javabean class name<br>
     * eg: CAR_ORDER_LIST return carOrderList
     * 
     * @param name
     * @return
     */
    public String getClassName(String name) {
        if (name == null)
            return null;
        StringBuilder sb = new StringBuilder("");
        if (name.indexOf("_") > 0) {
            String[] strs = name.split("_");
            for (String str : strs) {
                sb.append(getCapitalName(str));
            }
        } else {
            sb.append(getCapitalName(name));
        }
        return sb.toString();
    }

    /**
     * Capitalize first letter
     * 
     * @param name
     * @return
     */
    private String getCapitalName(String name) {
        if (name == null)
            return null;
        StringBuilder sb = new StringBuilder("");
        if (name.length() > 0) {
            String firstLetter = name.substring(0, 1);
            sb.append(firstLetter.toUpperCase());
            sb.append(name.substring(1).toLowerCase());
        }
        return sb.toString();
    }

    /**
     * ????<br>
     * eg: SYSTEM_CONFIG  systemConfig
     * 
     * @param name
     * @return
     */
    public String getUncapName(String name) {
        if (name == null || name.length() == 0)
            return null;
        StringBuilder sb = new StringBuilder("");
        if (name.indexOf("_") > 0) {
            String[] strs = name.split("_");
            for (int i = 0; i < strs.length; i++) {
                if (i == 0) {
                    sb.append(strs[i].toLowerCase());
                } else {
                    sb.append(getCapitalName(strs[i]));
                }
            }
        } else {
            String firstLetter = name.substring(0, 1);
            sb.append(firstLetter.toLowerCase());
            sb.append(name.substring(1).toLowerCase());
        }
        return sb.toString();
    }

    /**
     * ??java.lang ?import?<br>
     * eg import java.math.BigDecimal; import java.sql.Date;
     * 
     * @param columns 
     * @return
     */
    public String getImportClass(List<Column> columns) {
        StringBuilder sb = new StringBuilder();
        //??
        Map<String, String> map = new HashMap<String, String>();
        for (Column column : columns) {
            String codeType = column.getCodeType();
            if (codeType.indexOf(".") > 0 && !codeType.contains("java.lang") && !map.containsKey(codeType)) {
                sb.append("import " + codeType + ";\n");
                map.put(codeType, null);
            }
        }
        return sb.toString();
    }

    /**
     * ???? eg  arg1 elfkingw arg2 ing  true
     * 
     * @param str
     * @param contantStr
     * @return
     */
    public boolean isContants(String str, String contantStr) {
        if (str == null || contantStr == null) {
            return false;
        }
        return str.contains(contantStr);
    }

    /**
     *  eg arg1elf erg2 kingw  elfkingw
     * 
     * @param str
     * @param appendStr
     * @return
     */
    public String append(String str, String appendStr) {
        if (appendStr == null) {
            return str;
        }
        return str + appendStr;
    }

    /**
     * ? eg arg1elfkingw1 arg2 1 return elfkingw
     * 
     * @param str ??
     * @param len ??
     * @return
     */
    public String subStrigTail(String str, int len) {
        if (str == null || str.length() < len) {
            return str;
        }
        return str.substring(0, str.length() - len);
    }

    /**
     * ??
     * 
     * @param str
     * @return
     */
    public String toLowerCase(String str) {
        if (str == null) {
            return null;
        }
        return str.toLowerCase();
    }

    /**
     * ?
     * 
     * @param str
     * @return
     */
    public String toUpperCase(String str) {
        if (str == null) {
            return null;
        }
        return str.toUpperCase();
    }

    /**
     * 16
     * 
     * @param s
     * @return
     */
    public String toHex(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            String s4 = Integer.toHexString(ch);
            str = str + s4;
        }
        return "\\u00" + str;
    }

    public static void main(String[] args) {
        StringUtil util = new StringUtil();
        System.out.println(util.toHex("\n"));
    }
}