java:驼峰转下划线

如果你不相信努力和时光,那么成果就会是第一个选择辜负你的。不要去否定你自己的过去,也不要用你的过去牵扯你现在的努力和对未来的展望。不是因为拥有希望你才去努力,而是去努力了,你才有可能看到希望的光芒。java:驼峰转下划线,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

java:驼峰转下划线

1 前言

实体类属性,一般命名规范为小驼峰格式,将属性名转数据库字段下划线样式,工具方法如下。

2 使用

package com.xiaoxu.crawler.utils;

import com.google.common.collect.Lists;
import com.xiaoxu.crawler.excp.CrawlerForJException;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.text.MessageFormat;
import java.util.List;

/**
 * @author xiaoxu
 * @date 2022-11-19 21:52
 * crawlerJ:com.xiaoxu.crawler.utils.StringUtils
 */
public class StrUtils {

    private static final String underLineMark = "_";

    private static final String EMPTY_STRING = "";

    public static boolean equals(String a, String b){
        if(null == a){
            return null == b;
        }
        return a.equals(b);
    }

    public static int getMatchCount(String str, String subStr){
        if(!StringUtils.hasLength(str) || !StringUtils.hasLength(subStr)){
            ExcpUtils.throwExp(
                    MessageFormat.format("getMatchCount's str and subStr should not be null or empty:{0},{1}.",
                            str, subStr));
        }
        return StringUtils.countOccurrencesOf(str, subStr);
    }

    /**
     * @param name 小驼峰命名Str
     * @return 下划线
     */
    @SuppressWarnings("all")
    public static String humpTransferUnderline(String name){
        // null or empty throw exp
        ExcpUtils.throwExpIfFalse(StringUtils.hasLength(name), "when hump transfer to underline, name should not be empty.");
        CharSequence cs = name;
        List<CharSequence> charSequenceList = Lists.newArrayList();

        int temI = 0, i = 0, csLen = 0;

        for (; i < (csLen = cs.length()); i++) {
            char c = cs.charAt(i);
            if(Character.isUpperCase(c)){
                CharSequence csq = cs.subSequence(temI, i);
                if(csq.length() > 0){
                    addCharSequence(charSequenceList, csq);
                    temI = i;
                }
            }
        }

        CharSequence lastSequence = cs.subSequence(temI, csLen);
        if(lastSequence.length() > 0){
            addCharSequence(charSequenceList, lastSequence);
        }

        // actual could not execute this
        if(CollectionUtils.isEmpty(charSequenceList)) return EMPTY_STRING;

        return String.join(underLineMark, charSequenceList);
    }

    private static void addCharSequence(List<CharSequence> charSequenceList, CharSequence charSequence) {
        if(null == charSequenceList){
            throw new CrawlerForJException("charSequenceList could not be null");
        }

        if(null == charSequence || charSequence.length() <= 0){
            throw new CrawlerForJException("charSequence need non empty");
        }
        char[] csqChars = charSequence.toString().toCharArray();
        char[] initialLowerCsqChar = new char[csqChars.length];
        initialLowerTransfer(initialLowerCsqChar, csqChars);
        charSequenceList.add(new String(initialLowerCsqChar));
    }

    private static void initialLowerTransfer(char[] targetChar, char[] originChar){
        ExcpUtils.throwExpIfFalse(ArrayUtils.isNotEmpty(targetChar), "targetChar is empty");
        ExcpUtils.throwExpIfFalse(ArrayUtils.isNotEmpty(originChar), "originChar is empty");
        int totalLength;
        ExcpUtils.throwExpIfFalse((totalLength = originChar.length) == targetChar.length, "targetChar'length not equals to originChar's length");

        char[] temp;int tempSize;
        System.arraycopy((temp = new char[]{Character.toLowerCase(originChar[0])}), 0 , targetChar, 0,  (tempSize = temp.length));

        if(totalLength > tempSize){
            System.arraycopy(originChar, tempSize, targetChar, tempSize, totalLength - tempSize);
        }
    }

    public static void main(String[] args) {
        System.out.println(humpTransferUnderline("a"));
        System.out.println(humpTransferUnderline("A"));
        System.out.println(humpTransferUnderline("age"));
        System.out.println(humpTransferUnderline("Age"));
        System.out.println(humpTransferUnderline("ageABBadA"));
        System.out.println(humpTransferUnderline("AddressFrom"));
        System.out.println(humpTransferUnderline("gmt_create"));
        System.out.println(humpTransferUnderline("gmtCreate"));
    }

}

执行效果如下:

a
a
age
age
age_a_b_bad_a
address_from
gmt_create
gmt_create

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/192085.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!