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

com.tsc9526.monalisa.tools.clazz.MelpJavaBeans Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
/*******************************************************************************************
 *	Copyright (c) 2016, zzg.zhou([email protected])
 * 
 *  Monalisa is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Lesser General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.

 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Lesser General Public License for more details.

 *	You should have received a copy of the GNU Lesser General Public License
 *	along with this program.  If not, see .
 *******************************************************************************************/
package com.tsc9526.monalisa.tools.clazz;

import java.util.Date;
import java.util.HashMap;

import com.tsc9526.monalisa.tools.string.MelpTypes;


/**
 * 
 * @author zzg.zhou([email protected])
 */  
public class MelpJavaBeans { 
    private MelpJavaBeans() {
        super();
    }
   
    private static HashMap hDefaultValue=new HashMap(){    	 
		private static final long serialVersionUID = 7570819176794474057L;
		{
    		put(String.class.getSimpleName(), "");
    		put(Integer.class.getSimpleName(),new Integer(0)); 
    		put(Long.class.getSimpleName(),   new Long(0));
    		put(Float.class.getSimpleName(),  new Float(0));
    		put(Double.class.getSimpleName(), new Double(0));
    		put(Byte.class.getSimpleName(),   new Byte((byte)0));    		    		 
    	}
    };
    
    public static Object getDefaultValue(int jdbcType,Class fieldType){
    	String type=MelpTypes.getJavaType(jdbcType);
    	
    	Object v=hDefaultValue.get(type);
    	if(v==null){
    		if(type.equals(Date.class.getName())){
    			v=new Date();
    		}
    	}
    	
    	return v;
    }
    
    public static String getGetterMethodName(String property,String javaType) {
        StringBuilder sb = new StringBuilder();

        sb.append(property);
        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
         
        if (javaType.equals("boolean") || javaType.equals("Boolean") || javaType.equals("java.lang.Boolean")) {
            sb.insert(0, "is");
        } else {
            sb.insert(0, "get");
        }

        return sb.toString();
    }

    /**
     * JavaBeans rules:
* * eMail -> setEMail()
* firstName -> setFirstName()
* URL -> setURL()
* XAxis -> setXAxis()
* a -> setA()
* B -> setB()
* @param property property * @return the setter method name */ public static String getSetterMethodName(String property) { StringBuilder sb = new StringBuilder(); sb.append(property); sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); sb.insert(0, "set"); return sb.toString(); } public static String getJavaName(String name,boolean firstCharacterUppercase){ if(name==null || name.trim().length()==0){ return "_"; } String javaName=MelpJavaBeans.getCamelCaseString(name, firstCharacterUppercase); if(MelpTypes.isJavaKeyword(javaName) || javaName.length()==0){ javaName=javaName+"_"; } return javaName; } public static String getCamelCaseString(String inputString,boolean firstCharacterUppercase) { StringBuilder sb = new StringBuilder(); boolean nextUpperCase = false; for (int i = 0; i < inputString.length(); i++) { char c = inputString.charAt(i); if( (c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z') ){ if(c>='0' && c<='9' && sb.length()==0){ sb.append("N"); } if (nextUpperCase) { sb.append(Character.toUpperCase(c)); nextUpperCase = false; } else { sb.append(Character.toLowerCase(c)); } }else if ((c >= 0x4e00)&&(c <= 0x9fbb)){ //中文字 sb.append(c); }else{ if (sb.length() > 0) { nextUpperCase = true; } } } if (firstCharacterUppercase) { sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); } return sb.toString(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy