Research Article

Practical Benefits of Aspect-Oriented Programming Paradigm in Discrete Event Simulation

Algorithm 2

Calculation accuracy aspect snippet code for capturing addition operation overflow and underflow.
Object around (Object aa, Object bb):
            call (static public Object add(Object, Object)) && args(aa,bb)
{
            String ca = aa.getClass().getSimpleName();
            String cb = bb.getClass().getSimpleName();
            if(ca.equals(cb)&&ca.equals(Integer)) {
                     int a=((Integer)aa).intvalue();
                     int b=((Integer)bb).intValue();
                     long c=(long)a+b;
                     if(c <Integer.MIN_VALUE)
                              throw new ArithmeticException(int underflow);
                     else if(c> Integer.MAX_VALUE)
                              throw new ArithmeticException(int overflow);
                     return (new Integer((int)c));}
            if(ca.equals(cb)&&ca.equals(Float)) {
                     float a=((Float)aa).floatValue();
                     float b=((Float)bb).floatValue();
                     float c=a+b;
                     if(c==0 && (a!=b))
                              throw new ArithmeticException(float underflow);
                     else if(c==Float.POSITIVE_INFINITY∣∣c==Float.NEGATIVE_INFINITY)
                              throw new ArithmeticException(float overflow);
                     return (new Float(c));
            }
            if(ca.equals(cb)&&ca.equals(Double)) {
                     Double a=((Double) aa).doubleValue();
                     Double b=((Double)bb).doubleValue();
                     double c=a+b;
                     if(c==0  && (a!=−b))
                              throw new ArithmeticException(double underflow);
                     else if(c==Double.POSITIVE_INFINITY  c==Double.NEGATIVE_INFINITY)
                              throw new ArithmeticException(double overflow);
                     return (new Double(c));}
            if (ca.equals(cb)&&ca.equals(Long)) {
                     long a=((Long)aa).longValue();
                     long b=((Long)bb).longValue();
                     long c=a+b;
                     if (a > 0 &&  b > 0 &&  (c < a      c < b))
                              throw new ArithmeticException(long Overflow);
                     else if (a < 0 &&  b < 0 && (c > a      c > b))
                              throw new ArithmeticException(long underflow);
                     return (new Long(c));
            }
            return null;
}