Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.s1.objects;
  
  import java.util.*;

Type utils
 
 public class ObjectType {

    
Get class by type name

Supported types are:

  • String
  • Integer
  • Float
  • Long
  • Double
  • BigDecimal
  • BigInteger
  • Boolean
  • Date

Parameters:
type
Returns:
 
     static Class resolveType(String type) {
         Class cls = Object.class;
         if ("String".equals(type))
             cls = String.class;
         else if ("Integer".equals(type))
             cls = Integer.class;
         else if ("Long".equals(type))
             cls = Long.class;
         else if ("Float".equals(type))
             cls = Float.class;
         else if ("Double".equals(type))
             cls = Double.class;
         else if ("Boolean".equals(type))
             cls = Boolean.class;
         else if ("BigDecimal".equals(type))
             cls = BigDecimal.class;
         else if ("BigInteger".equals(type))
             cls = BigInteger.class;
         else if ("Date".equals(type))
             cls = Date.class;
 
         return cls;
     }

    
Cast object to type

Parameters:
obj
type
<T>
Returns:
 
     static <T> T cast(Object objClass<T> type) {
         if (type == String.class) {
             if (obj == null)
                 obj = "";
             obj = obj.toString();
         } else if (type == BigInteger.class) {
             if (obj == null)
                 obj = "0";
             obj = new BigInteger("" + obj);
         } else if (type == BigDecimal.class) {
             if (obj == null)
                 obj = "0";
             obj = new BigDecimal("" + obj);
         } else if (type == Integer.class) {
             if (obj == null)
                 obj = "0";
             if(obj instanceof Number){
                 obj = ((Numberobj).intValue();
             }else
                 obj = Integer.parseInt("" + obj);
         } else if (type == Long.class) {
             if (obj == null)
                 obj = "0";
             if(obj instanceof Number){
                 obj = ((Numberobj).longValue();
             }else
                 obj = Long.parseLong("" + obj);
         } else if (type == Float.class) {
             if (obj == null)
                 obj = "0";
             if(obj instanceof Number){
                 obj = ((Numberobj).floatValue();
             }else
                 obj = Float.parseFloat("" + obj);
         } else if (type == Double.class) {
             if (obj == null)
                 obj = "0";
            if(obj instanceof Number){
                obj = ((Numberobj).doubleValue();
            }else
                obj = Double.parseDouble("" + obj);
        } else if (type == Boolean.class) {
            if (obj == null)
                obj = "false";
            if(obj instanceof Number){
                obj = ((Numberobj).intValue()!=0;
            }else
                obj = Boolean.parseBoolean("" + obj);
        } else if (type == Date.class) {
            if (obj != null) {
                if (obj instanceof String) {
                    obj = new Date(Long.parseLong("" + obj));
                } else if (obj instanceof Number) {
                    obj = new Date((Longobj);
                }
            }
        }
        return (T) obj;
    }
New to GrepCode? Check out our FAQ X