본문 바로가기
Java

자바 순차키 만들기

by NaHyungMin 2020. 8. 7.

1. UUID를 사용할 수 없을 때.

2. uuid를 가져와서 uuid.hashcode * -1로 양수를 만들고 처리하려 했지만, 20만건에서 한 6개를 중복된다.(다시 생성할 순 있지만...)

3. AAAA0001 -> AAAA0002 -> AAAA9999 -> AAAB0001순으로 처리하기로 함.

 

package com.packege.commonlib.util;

import com.packege.commonlib.util.response.CustomErrorCode;
import com.packege.commonlib.util.response.CustomErrorMessgae;
import com.packege.commonlib.util.response.exception.RestapiInvalidRequestException;

public class UserDataUtil {
    private static final String defaultInvitationCode = "AAAA0001";
    private static final int invitationCodeLength = (defaultInvitationCode.length() / 2);
    private static final int startCode = 65;
    private static final int endCode = 90;
    private static final int maxValue = 9999;
    private static final String minValue = "0001";
    private static final String maxCode = "ZZZZ" + maxValue;

    public static String nextInvitationCode(String invitationCode) throws Exception{
        if(invitationCode.length() != defaultInvitationCode.length()){
            //오류를 낼지 말지. 기본 값으로 보내야 할지. 일단 오류를 내기로 결정.
            throw new RestapiInvalidRequestException(CustomErrorCode.User_Invitation_Code_Create_Failed, CustomErrorMessgae.userInvitationCodeCreateFailed);
        }

        //Max
        if(invitationCode == maxCode){
            throw new RestapiInvalidRequestException(CustomErrorCode.User_Invitation_Code_Max, CustomErrorMessgae.userInvitationCodeMaxOver);
        }

        String numberString = invitationCode.substring(invitationCodeLength);

        if(CustomAssert.isNumeric(numberString) == false){
            throw new RestapiInvalidRequestException(CustomErrorCode.User_Invitation_Code_Create_Failed, CustomErrorMessgae.userInvitationCodeCreateFailed);
        }

        int numbering = Integer.parseInt(numberString);
        String prefixString = invitationCode.substring(0, invitationCodeLength);

        if(numbering == maxValue){
            //0001 -> 1
            numberString = minValue;
            boolean isPrefixUpdate = false;

            //기존 문자열 체크 AAAA -> ZZZZ
            for(int i = invitationCodeLength - 1; i > -1;  i--){
                char code = prefixString.charAt(i);

                //Z
                //endCode
                if(isPrefixUpdate == true){
                    if(code != endCode) {
                        prefixString = getPrefix(prefixString, i, (code + 1));
                        break;
                    }

                    continue;
                }

                if(code == endCode) {
                    prefixString = getPrefix(prefixString, i, startCode);
                    isPrefixUpdate = true;
                }else{
                    prefixString =  getPrefix(prefixString, i, (code + 1));
                    break;
                }
            }
        }else{
            numberString = String.format("%04d", numbering + 1);
        }

        return prefixString.concat(numberString);
    }

    private static String getPrefix(String prefixString, int index, int code){
        char nextCode = (char)code;
        return prefixString.substring(0, index) + nextCode + prefixString.substring(index + 1);
    }
}

 

  public static boolean isNumeric(String string) {
        if(string.equals("")){
            //문자열이 공백인지 확인
            return false;
        }

        return string.matches("-?\\d+(\\.\\d+)?");
    }

 

'Java' 카테고리의 다른 글

자바 mybatis <>, > 등 쓰기  (0) 2020.08.21
자바 mybatis Like  (0) 2020.08.21
자바 제네릭  (0) 2020.08.06
자바 비밀번호 암호화  (0) 2020.07.23
자바 이메일 검사  (0) 2020.07.23