egovframework.oe1.utl.fcc.service.EgovNumberUtil.java Source code

Java tutorial

Introduction

Here is the source code for egovframework.oe1.utl.fcc.service.EgovNumberUtil.java

Source

/*
 * Copyright 2010 MOPAS(Ministry of Public Administration and Security).
 *
 * 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 egovframework.oe1.utl.fcc.service;

import java.security.SecureRandom;
import java.util.*;
import java.text.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Class Name : EgovNumberUtil.java
 * @Description : ? ??   
 * @author    ?
 * @since 2009. 02. 13
 * @version 1.0
 * @see <pre>
 *  == ?(Modification Information) ==
 *   
 *   ?      ?           
 *  -------    --------    ---------------------------
 *   2009.02.13       ?                   ?
 * 
 * </pre>
 */
public class EgovNumberUtil {

    /**
     * ? ? ? ?   ? ? ??  ?
     * ? 
     * @param startNum
     *        - ?
     * @param endNum
     *        - ?
     * @return ??
     * @exception MyException
     * @see
     */

    /** LOG */
    static protected Log log = LogFactory.getLog(EgovNumberUtil.class);

    public static int getRandomNum(int startNum, int endNum) {

        int randomNum = 0;

        try {

            // ? ? ?
            SecureRandom rnd = new SecureRandom();

            do {

                // ?? ? ? ?.
                randomNum = rnd.nextInt(endNum + 1);

            } while (randomNum < startNum); // ? ?
                                            // ?
                                            // ? 
                                            // ??
                                            // ?.

        } catch (Exception e) {

            //log.debug(e.getMessage());
            log.trace(e.getMessage());

        }

        return randomNum;

    }

    /**
     *  ? ?  ?  ?  12345678? 7? 
     *  ? ? 
     * @param sourceInt
     *        - ?
     * @param searchInt
     *        - ?
     * @return 
     * @exception MyException
     * @see
     */
    public static Boolean getNumSearchCheck(int sourceInt, int searchInt) {

        String sourceStr = String.valueOf(sourceInt);
        String searchStr = String.valueOf(searchInt);

        // ?   ? . ?  -1
        if (sourceStr.indexOf(searchStr) == -1) {

            return false;
        } else {

            return true;
        }

    }

    /**
     * ?? ?   ? 20081212 ? '20081212'
     *  
     * @param srcNumber
     *        - ?
     * @return ?
     * @exception MyException
     * @see
     */
    public static String getNumToStrCnvr(int srcNumber) {

        String rtnStr = null;

        try {

            rtnStr = String.valueOf(srcNumber);

        } catch (Exception e) {

            //log.debug(e.getMessage());
            log.trace(e.getMessage());

        }

        return rtnStr;
    }

    /**
     * ?? ??    ? 20081212 ??
     * '2008-12-12'  
     * @param srcNumber
     *        - ?
     * @return String
     * @exception MyException
     * @see
     */
    public static String getNumToDateCnvr(int srcNumber) {

        String pattern = null;
        String cnvrStr = null;

        String srcStr = String.valueOf(srcNumber);

        // Date ? 8? ? 14? ?
        if (srcStr.length() != 8 && srcStr.length() != 14) {
            throw new IllegalArgumentException("Invalid Number: " + srcStr + " Length=" + srcStr.trim().length());
        }

        if (srcStr.length() == 8) {

            pattern = "yyyyMMdd";

        } else if (srcStr.length() == 14) {

            pattern = "yyyyMMddhhmmss";
        }

        SimpleDateFormat dateFormatter = new SimpleDateFormat(pattern, Locale.KOREA);

        Date cnvrDate = null;

        try {
            cnvrDate = dateFormatter.parse(srcStr);

        } catch (ParseException e) {

            //log.debug(e.getMessage()); 
            log.trace(e.getMessage());
        }

        cnvrStr = String.format("%1$tY-%1$tm-%1$td", cnvrDate);

        return cnvrStr;

    }

    /**
     * ? ? ? ??  ?  ?? True, 
     * False 
     * @param checkStr
     *        - ??
     * @return ?
     * @exception MyException
     * @see
     */
    public static Boolean getNumberValidCheck(String checkStr) {

        int i;
        // String sourceStr =
        // String.valueOf(sourceInt);

        int checkStrLt = checkStr.length();

        try {

            for (i = 0; i < checkStrLt; i++) {

                // ( '0'-> 48, '9' -> 57)
                if (checkStr.charAt(i) > 47 && checkStr.charAt(i) < 58)
                    continue;
                else
                    return false;

            }

        } catch (Exception e) {

            //log.debug(e.getMessage());
            log.trace(e.getMessage());

        }

        return true;
    }

    /**
     * ?  ?   ? 12345678? 123 999
     *  ? (99945678)
     * @param srcNumber
     *        - ?
     * @param cnvrSrcNumber
     *        - ??
     * @param cnvrTrgtNumber
     *        - ?
     * @return ?
     * @exception MyException
     * @see
     */

    public static int getNumberCnvr(int srcNumber, int cnvrSrcNumber, int cnvrTrgtNumber) {

        // ? ? ? 
        String source = String.valueOf(srcNumber);
        String subject = String.valueOf(cnvrSrcNumber);
        String object = String.valueOf(cnvrTrgtNumber);

        StringBuffer rtnStr = new StringBuffer();
        String preStr = "";
        String nextStr = source;

        try {

            // ??? ???  .
            while (source.indexOf(subject) >= 0) {

                preStr = source.substring(0, source.indexOf(subject)); // ??
                                                                       // 
                                                                       // ?
                                                                       // ?

                nextStr = source.substring(source.indexOf(subject) + subject.length(), source.length());

                source = nextStr;

                rtnStr.append(preStr).append(object); // ?
                                                      // ??
                                                      // 
                                                      // ?
                                                      // .

            }

            rtnStr.append(nextStr); // ? ? ? ?
                                    // .

        } catch (Exception e) {

            //log.debug(e.getMessage());
            log.trace(e.getMessage());

        }

        return Integer.parseInt(rtnStr.toString());
    }

    /**
     * ? ?, ?, ?? ?  123? ?, ?,
     * ?? ? ? 
     * @param srcNumber
     *        - ?
     * @return -1(?), 0(), 1()
     * @exception MyException
     * @see
     */
    public static int checkRlnoInteger(double srcNumber) {

        // byte 1? ??  ?,  -2^7 ~ 2^7 -1
        // short 2? ??  ?,  -2^15 ~ 2^15 -1
        // int 4? ??  ?,  -2^31 ~ 2^31 - 1
        // long 8? ??  ?,  -2^63 ~ 2^63-1

        // float 4? ??  ?, ??? F ? f   ?
        // (:3.14f)
        // double 8? ??  ?, ??? ?   ?
        // (:3.14)
        // ??  ?, ??? D ? d   ?(:3.14d)

        String cnvrString = null;

        if (srcNumber < 0) {

            return -1;

        } else {

            cnvrString = String.valueOf(srcNumber);

            if (cnvrString.indexOf(".") == -1) {

                return 0;
            } else {

                return 1;

            }
        }

    }

}