Monday, September 23, 2013

A typical "Generics" problem and the "Pee Delegator" in Java.

<div class="paragraph">
Quite interesting, and problematic are Generics in Java. Something took my mind for hours. And again, such thing created on Java called "Generics".
</div>
<div class="paragraph">
Look how interesting is the case of the of an attempt to use a "Generic Info Type" to define on specialized class (which means Man<male>, Man<female>, Woman<male>, Woman<female>, Man<other>, Woman<other>).
</other></other></female></male></female></male></div>
//consider G "gender".
//some code stolen from javaranch.

public class Human<g>{
public Class getGender()
 {
    Class clazz = getClass();
           ParameterizedType superclass = (ParameterizedType)                 clazz.getGenericSuperclass();
        Type[] types = superclass.getActualTypeArguments();
        Class actualdataType = null;
        if(types != null &amp;&amp; types.length &gt;0 &amp;&amp; (types[0] instanceof Class) ) {
            actualdataType = (Class) types[0];
        }
        System.out.println("actualdataType = " + actualdataType);
 }
    return actualdataType;
}
public Urine pee()
 {
   //do sothing....
   if (getGender().equals(Male))
      peeLikeAMan();
   // or if you prefer a "Pee Delegator"
   peeDelegator.delegatePee(this); //someone is handling you pee machine... for example.
   return urine;
 }
}
public class Man <male>{

//functions to use the Male generic....

public void storeTrash(Trash t)
{

}

public class Woman<female>
{
   public void spendMoney(Money ammount)
    {
    }

}  </female></male></g>