All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nutz.lang.random.StringGenerator Maven / Gradle / Ivy

Go to download

Nutz, which is a collections of lightweight frameworks, each of them can be used independently

There is a newer version: 1.r.72
Show newest version
package org.nutz.lang.random;

/**
 * @author zozohtnt
 * @author wendal([email protected])
 */
public class StringGenerator {

	/**
	 * 
	 * @param max
	 *            必须大于0
	 */
	public StringGenerator(int max) {
		maxLen = max;
		minLen = 1;
	}

	/**
	 * 
	 * @param min
	 *            必须大于0
	 * @param max
	 *            必须不小于min
	 */
	public StringGenerator(int min, int max) {
		maxLen = max;
		minLen = min;
	}

	/**
	 * min length of the string
	 */
	private int maxLen;

	/**
	 * max length of the string
	 */
	private int minLen;

	/**
	 * 
	 * @param min
	 *            必须大于0
	 * @param max
	 *            必须不小于min
	 */
	public void setup(int min, int max) {
		minLen = min;
		maxLen = max;
	}

	/**
	 * 根据设置的max和min的长度,生成随机字符串.
	 * 

* 若max或min小于0,则返回null * * @return 生成的字符串 */ public String next() { if (maxLen <= 0 || minLen <= 0 || minLen > maxLen) return null; char[] buf = new char[R.random(minLen, maxLen)]; for (int i = 0; i < buf.length; i++) buf[i] = CharGenerator.next(); return new String(buf); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy