Showing posts with label Generics and Type Casting. Show all posts
Showing posts with label Generics and Type Casting. Show all posts

Monday, June 30, 2014

Comparison of generics with type casting

public class ObjectSpecificCondition {
    private Class classType;

    public boolean check(Object obj) {
        boolean check = ((DateObject) obj).getLocalizationdate();
return check;
    }
    public static void main(String[] args) throws Exception {
           DateObject mockdateObject = new DateObject();
           // caseI: No generics used caseI works fine no exception is generated but more lines of code to write
           ObjectSpecificCondition mockanalysis = new ObjectSpecificCondition(DateObject.class);
           ObjectSpecificCondition1 mockanalysis2 = new ObjectSpecificCondition1();

           if (mockanalysis.checkTypeSpecific(mockdateObject)) {
               mockanalysis.check(mockdateObject);

           }
        // caseII:No generics used caseII throws exception in run time .More lines of code to write.It can not capture incompatible type at compile time
           ObjectSpecificCondition mockanalysis1 = new ObjectSpecificCondition(String .class);
           DateObject mockdateObject1 = new DateObject();
           if (mockanalysis.checkTypeSpecific(mockdateObject1)) {
               mockanalysis.check(mockdateObject1);

           }
          // caseIII;Generics used and line of code is reduced to less
           ObjectSpecificConditionGenerics mockgenerics=new ObjectSpecificConditionGenerics() ;
           mockgenerics.check(mockdateObject);

           // caseIV;Generics used and line of code is reduced to less and error for compataible object is generated at compile time
           ObjectSpecificConditionGenerics mockgenerics1=new ObjectSpecificConditionGenerics() ;
           String mockstring=new String();
//           mockgenerics.check(mockstring); // it is captured at compile time ,i think its good to catch at compile time then to pass it at run time

       }
 

    public ObjectSpecificCondition(Class classType) {
        this.classType = classType;
    }

    boolean checkTypeSpecific(Object busineesObject) {
        if (classType.isAssignableFrom(busineesObject.getClass())) {
            return true;
        }
        else{
            try {
throw new Exception();
} catch (Exception e) {
}
        }
return false;

    }
}
 class ObjectSpecificCondition1 {
// Note:The business object of which type is very important .It might be object implementing collection Or Array object Or a simple object  

    public boolean check(Object busineesObject) {
    boolean check ;
       
        if(busineesObject instanceof DateObject|| busineesObject instanceof Action || busineesObject instanceof DateObject ){
        check= ((DateObject) busineesObject).getLocalizationdate();
        Action action=((Action) busineesObject);
        DateObjectInternalization action1=((DateObjectInternalization) busineesObject);
       
        action.distinguish(new Object());
       
        }
        else{
        throw new IllegalArgumentException();
        }
        return check;
    }

    }
 interface Action{
 boolean distinguish(Object object);


 }

   

 
   
   
    class ObjectSpecificConditionGenerics<T extends DateObject> {
        private T classTypeGenerics;

        public boolean check(T genericsobj) {
            return genericsobj.getLocalizationdate();

        }

    }
    class DateObject implements Action{
        boolean getLocalizationdate() {
        return true;
            // return true Or False according to business logic
        }

public boolean distinguish(Object object) {

return false;
}
    }
    class DateObject1 implements Action{
     
public boolean distinguish(Object object) {

return false;
}
    }
    class DateObjectInternalization extends DateObject{
        boolean getLocalizationdate() {
        return true;
            // return true Or False according to business logic
        }

@Override
public boolean distinguish(Object object) {

return false;
}
public boolean isTrue(){
return true;

}
    }