⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 role.java

📁 TestDataBuilder是一个采用Java编写的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    //System.out.println(complex);
                    this.getTempDBObj().add(complex);
                }
                this.min = 0;
                this.max = getTempDBObj().size();
             } finally {
                if(this.max < 1.0){
                    logger.info("sql[" + this.getSQL() + "]no any result!");
                }
                if(!this.isNullable() && this.max < 1.0){
                    throw new BaseException(String.format("SQL[%s]returned null", getSQL()));
                }
            	try {
	            	if(result != null){
						result.close();					
	            	}
	            	if(statement != null){
	            		statement.close();
	            	}
            	} catch (SQLException e) {
					logger.error("close", e);
				}
            }
        }
        
        this.current = this.min;
    }
   
    
    private double current = 0;    
    
    private JavaSource javaSource = null;
    
    public Object getValueByRole() throws BaseException, SQLException  {
        // 生成NULL值。
        if (Math.random() < this.nullPercent) {
            return null;
        }

        Object retObj = null;

        if (isEnumMethod()) {
            retObj = this.getEnumerate().randomByPercent();
        } else if (isSQLQueryMethod()) {
            int dbSize = this.getTempDBObj().size();
            if (dbSize == 0) {
                return null;
            } else {
            	if(current < 0){
            		current = 0;
            	}
            	
                //int r = RandomUtil.randomInt(0, dbSize);
                retObj = this.getTempDBObj().get(Double.valueOf(current).intValue());
                current++;
                current %= (dbSize);
            }
        }else if(isSQLFuncMethod()){
        	retObj = this.getSQL();
        }else if(isJavaMethod()){
        	retObj = getJavaSource().getObjectByJava();
        } else if (isIncreamentMethod()) {
            if (current < min) {
                current = min;
            }
            if (this.type.equals(JavaTypes.STRING)) {
                retObj = random();
            } else if (IType.class.isAssignableFrom(type)) {
                retObj = getIncValue(current);
            } else if (this.type.equals(JavaTypes.DOUBLE) || type.equals(JavaTypes.FLOAT)) {
                retObj = Double.valueOf(current);
            } else if (type.equals(JavaTypes.BOOLEAN)) {
                retObj = Boolean.valueOf(current % 2 == 0);
                step = 1;
            } else if (type.equals(JavaTypes.DATE)) {
                retObj = new java.sql.Date(Double.valueOf(current).longValue());
            } else {
                retObj = Long.valueOf((long) current);
            }
            current += step;
            if (max != 0) {
                current %= (max + 1);
            }

        } else {
            retObj = random();
        }

        if (!(retObj instanceof ComplexObj) &&  retObj != null) {
            if (StringUtils.isNotEmpty(prefix)) {
                retObj = prefix + retObj.toString();
            }
            if (StringUtils.isNotEmpty(suffix)) {
                retObj = retObj.toString() + suffix;
            }
        }
        return retObj;
    }
    
   //[start]
    
    public boolean isRandomMethod(){
    	return this.getMethod().equalsIgnoreCase(Role.METHOD_RANDOM);
    }
    
    public boolean isIncreamentMethod(){
    	return this.getMethod().equalsIgnoreCase(Role.METHOD_INCREMENT);
    }
    
    public boolean isEnumMethod(){
        return this.getMethod().equalsIgnoreCase(Role.METHOD_ENUM);
    }
    
    public boolean isSQLQueryMethod(){
        return this.getMethod().equalsIgnoreCase(Role.METHOD_SQL_QUERY);
    }
    
    public boolean isSQLFuncMethod(){
    	return this.getMethod().equalsIgnoreCase(Role.METHOD_SQL_FUNC);
    }
    
    public boolean isJavaMethod(){
    	return this.getMethod().equalsIgnoreCase(Role.METHOD_JAVA);
    }
    
	public String getSQL(){
        return (String) this.atts.get(TAG_SQL);
    }
    
    public String getSourceName(){
        return (String)this.atts.get(TAG_SOURCE_NAME);
    }
    
    public void setSourceName(String sourceName){
        this.atts.put(TAG_SOURCE_NAME, sourceName);
    }
    
    public Integer getSize(){
        return (Integer) this.atts.get(TAG_SIZE);        
    }
    
    public void setSize(Integer size){
        this.atts.put(TAG_SIZE, size);
    }
    
    public Integer getDecimalDigits(){
        return (Integer)this.atts.get(TAG_DECIMAL_DIGITS);
    }
    
    public void setDecimalDigits(Integer decimalDigits){
        this.atts.put(TAG_DECIMAL_DIGITS, decimalDigits);
    }
    
    public void setSQL(String sql){
        this.atts.put(TAG_SQL, sql);
    }
    
    public int getDistinctCount() {
        return distinctCount;
    }

    public void setDistinctCount(int distinctCount) {
        this.distinctCount = distinctCount;
    }

    public void addEnum(Object obj, Integer percent){
        EnumObj e = new EnumObj(obj, percent);
        this.getEnumerate().add(e);
    }
    
    public EnumList<EnumObj> getEnumerate() {        
        return enumerate;
    }
    
    public int getEnumSize(){
    	return this.getEnumerate().size();
    }

    public static EnumList<EnumObj> cloneEnumList(EnumList<EnumObj> enumList){
        if(enumList != null){
            EnumList<EnumObj> tempEnumList = new EnumList<EnumObj>();
            for(EnumObj enumObj : enumList){
                tempEnumList.add(enumObj.clone());
            }
            return tempEnumList;
        }
        return enumList;
    }
    
    public void setEnumerate(EnumList enumerate) {
        this.enumerate = enumerate;
    }

    public double getMax() {
        return max;
    }

    public String getStringMax() {
        String strValue = null;
    	if(this.type != null && !type.equals(JavaTypes.OBJECT)){
    		strValue = JavaTypes.getInstance().getStringValue(this.getType(), this.getMax());
    	}
    	return strValue;
    }

    public void setMax(double max) {
        this.max = max;
    }

    public void setMaxDate(Date maxDate){
    	this.max = maxDate.getTime();
    }
    
    public void setMinDate(Date minDate){
    	this.min = minDate.getTime();
    }
    
    public Date getMaxDate(){
    	return new Date((long) this.max);
    }


    public Date getMinDate(){
    	return new Date((long) this.min);
    }
    
    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
    	this.method = method;
    }

    public double getMin() {
        return min;
    }

    public String getStringMin() {
    	String strValue = null;
    	if(this.type != null && !type.equals(JavaTypes.OBJECT)){
    		strValue = JavaTypes.getInstance().getStringValue(this.getType(), this.getMin());
    	}
    	return strValue;
    }

    public void setMin(double min) {
        this.min = min;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Class getType() {
        return type;
    }

    public void setType(Class type) {
        this.type = type;
    }

    public int getStep() {
        return step;
    }

    public void setStep(int step) {
        this.step = step;
    }
    /**
     * 设置时间的增长方式,
     * 
     * <p><code>setDateStep</code></p>
     * @param part 取值可为: TDate.YEAR,TDate.MONTH,TDate.DAY,TDate.HOUR,
     * 						TDate.MINUTE,TDate.SECOND.
     * @param num 数量级.
     * @author LiuXiaojie 2007-6-16
     */
    public void setDateStep(int part, int num){
    	this.step = part * num;
    }
//  [end]
    
    public String toString(){
    	return this.getName();
    }

	public int compareTo(Object arg0) {
       if (arg0 instanceof Role) {
           Role role = (Role) arg0;
           if(this.getName() != null && role.getName() != null){
               return this.getName().compareToIgnoreCase(role.getName());
           }
       } 
       return 0;
    }

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Role other = (Role) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

    public float getNullPercent() {
        return nullPercent;
    }

    public void setNullPercent(float nullPercent) {
        this.nullPercent = nullPercent;
    }	
    
    public static void main(String[] args) throws SQLException, BaseException {
        Role role = new Role();
        role.setName("xxxxx");
        role.setType(Integer.class);
        role.setMin(1000);
        role.setMax(500000);
        role.setPrefix("usr");
        role.setSuffix("@abc.com");
        
        for(int i=0;i < 10000; i++){
            System.out.println(role.getValueByRole());
        }
    }


    public boolean isNullable() {
        return nullable;
    }


    public void setNullable(boolean nullable) {
        this.nullable = nullable;
    }


	public RoleFactory getRoleFactory() {
		return roleFactory;
	}


	public void setRoleFactory(RoleFactory roleFactory) {
		this.roleFactory = roleFactory;
	}


	public void setJavaSource(JavaSource javaSource) {
		this.javaSource = javaSource;
		if(this.javaSource != null){
			this.javaSource.setRole(this);
		}
	}



	public JavaSource getJavaSource() {
		return javaSource;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -