廣告

2015年2月26日 星期四

[java] Check primitive data type

you can modify this method to what the type you need. at line 45~55
package test_fb_api;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class testCheckPrimitive {

 public static void main(String[] args) {

  java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(".*[^0-9].*");
  
  int a =0 ;
  double id = 0.0;
  String s = "111";
  Double d = 1.0;
  Map map = new HashMap();
  
  asignVar(a);
  asignVar(id);
  asignVar(s);
  asignVar(d);
  asignVar(map);
  
 }

 public static void asignVar(Object obj){
  
  Object o = obj;
  System.out.println(o.getClass());
  System.out.println(isWrapperType(o.getClass()));
  System.out.println("=========================");
  
 }
 private static final Set <Class<?>> WRAPPER_TYPES = getWrapperTypes();
 
  public static boolean isWrapperType(Class<?> clazz)
     {
         return WRAPPER_TYPES.contains(clazz);
     }
  
  private static Set<Class<?>> getWrapperTypes()
     {
         Set<Class<?>> ret = new HashSet<Class<?>>();
         ret.add(Boolean.class);
         ret.add(Character.class);
         ret.add(Byte.class);
         ret.add(Short.class);
         ret.add(Integer.class);
         ret.add(Long.class);
         ret.add(Float.class);
         ret.add(Double.class);
         ret.add(Void.class);
         ret.add(String.class);
         return ret;
     }
  
}

沒有留言:

張貼留言