io.manasobi.utils.RandomUtils.java Source code

Java tutorial

Introduction

Here is the source code for io.manasobi.utils.RandomUtils.java

Source

/*
 * Copyright 2002-2012 the original author or authors.
 *
 * 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 io.manasobi.utils;

import java.util.Random;

import org.apache.commons.lang3.CharUtils;

/**
 * ? ?(?, )? ? ? .<br>
 * ? ?? ? ,  ? ?? ? ? ?? ? .
 * 
 * @author manasobi
 * @since 1.0.0
 */
public final class RandomUtils {

    private RandomUtils() {
    }

    private static final char[] ALPHAS = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
            'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'X', 'Y', 'V', 'W', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
            'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'x', 'y', 'v', 'w', 'z' };

    private static final Random GENERATOR = new Random(System.currentTimeMillis());

    /** ? ? ??  INT */
    private static final int RANDOM_ALPHABETIC_INT = 52;

    /** ? ? ??  INT */
    private static final int RANDOM_STR_KR_INT = 11172;

    /** ? ? ??  HEX */
    private static final int RANDOM_STR_KR_HEX = 0xAC00;

    /**
     * ,  ? ?? ? ?? .<br><br>
     *
     * RandomUtils.getString(10, 15) = "jRTwRnLzSsOWC"
     *
     * @param minSize  ?
     * @param maxSize  ?
     * @return ,  ? ?? ? ?
     */
    public static String getString(int minSize, int maxSize) {
        Random generator = new Random(System.currentTimeMillis());
        int randomLength = generator.nextInt(maxSize - minSize) + minSize;

        return randomAlphabetic(randomLength);
    }

    /**
     *  ? ?? ? ?? .<br><br>
     *
     * RandomUtils.getString(8) = "ikwblpTL"
     *
     * @param count ? ? ?
     * @return  ? ?? ? ?
     */
    public static String getString(int count) {
        return randomAlphabetic(count);
    }

    private static String randomAlphabetic(int randomLength) {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < randomLength; i++) {
            buf.append(ALPHAS[GENERATOR.nextInt(RANDOM_ALPHABETIC_INT)]);
        }
        return buf.toString();
    }

    /**
     *   ?? ? ??? ? ?? .<br>
     * ASCII CODE  startChar ?? endChar? ??.<br><br>
     *
     * RandomUtils.getString(10, 'B', 'r') = FoOXjRmmMr
     *
     * @param count ? ? ?
     * @param startChar  
     * @param endChar ?? 
     * @return   ?? ? ?
     */
    public static String getString(int count, String startChar, String endChar) {

        int startInt = Integer.valueOf(CharUtils.toChar(startChar));
        int endInt = Integer.valueOf(CharUtils.toChar(endChar));

        int gap = endInt - startInt;
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < count; i++) {
            int chInt;
            do {
                chInt = GENERATOR.nextInt(gap + 1) + startInt;
            } while (!Character.toString((char) chInt).matches("^[a-zA-Z]$"));
            buf.append((char) chInt);
        }
        return buf.toString();
    }

    /**
     *  ???  ?? .<br><br>
     *
     * RandomUtils.getKorString(20) = ?????
     *
     * @param count ? ? ?
     * @return  ? ?? ?  ?
     */
    public static String getKorString(int count) {
        StringBuilder buf = new StringBuilder();
        for (int i = 0; i < count; i++) {
            buf.append((char) (GENERATOR.nextInt(RANDOM_STR_KR_INT) + RANDOM_STR_KR_HEX));
        }
        return buf.toString();
    }

    /**
     *  ???  ?  ?? .<br><br>
     *
     * RandomUtils.getStringByCharset(20, "UTF-8") = lBiwHUIoFqweOFrtDokI
     *
     * @param count ? ? ?
     * @param charset ? 
     * @return  ??? ?  ?
     */
    public static String getStringByCharset(int count, String charset) {
        String randomStr = getString(count);
        return DigestUtils.encodeCharset(randomStr, charset);
    }

}