home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / OSpace / jgl.exe / jgl_2_0 / src / COM / objectspace / jgl / NumberHelper.java < prev    next >
Encoding:
Java Source  |  1997-03-14  |  8.8 KB  |  257 lines

  1. // Copyright(c) 1996,1997 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package COM.objectspace.jgl;
  5.  
  6. import java.math.BigInteger;
  7. import java.math.BigDecimal;
  8.  
  9. final class NumberHelper
  10.   {
  11.   static private Class classInteger = new Integer( 0 ).getClass();
  12.   static private Class classLong = new Long( 0 ).getClass();
  13.   static private Class classFloat = new Float( 0 ).getClass();
  14.   static private Class classDouble = new Double( 0 ).getClass();
  15.   static private Class classByte = new Byte( (byte)0 ).getClass();
  16.   static private Class classShort = new Short( (short)0 ).getClass();
  17.   static private Class classBigInteger = new BigInteger( "0" ).getClass();
  18.   static private Class classBigDecimal = new BigDecimal( "0" ).getClass();
  19.   static Class classNumber = new Integer( 0 ).getClass().getSuperclass();
  20.  
  21.   private NumberHelper()
  22.     {
  23.     }
  24.  
  25.   static BigDecimal asBigDecimal( Number n )
  26.     {
  27.     return n instanceof java.math.BigDecimal
  28.       ? (BigDecimal)n
  29.       : new BigDecimal( n.toString() );
  30.     }
  31.  
  32.   static BigInteger asBigInteger( Number n )
  33.     {
  34.     // if not already a BigInteger, first convert to a BigDecimal
  35.     // to avoid checking floating point types
  36.     return n instanceof java.math.BigInteger
  37.       ? (BigInteger)n
  38.       : asBigDecimal( n ).toBigInteger();
  39.     }
  40.  
  41.   static Number plus( Number n1, Number n2, Class mode )
  42.     {
  43.     // normal subclasses
  44.     if ( mode.equals( classInteger ) )
  45.       return new Integer( n1.intValue() + n2.intValue() );
  46.     if ( mode.equals( classLong ) )
  47.       return new Long(  n1.longValue() + n2.longValue() );
  48.     if ( mode.equals( classFloat ) )
  49.       return new Float(  n1.floatValue() + n2.floatValue() );
  50.     if ( mode.equals( classDouble ) )
  51.       return new Double(  n1.doubleValue() + n2.doubleValue() );
  52.     if ( mode.equals( classByte ) )
  53.       return new Byte(  (byte)( n1.byteValue() + n2.byteValue() ) );
  54.     if ( mode.equals( classShort ) )
  55.       return new Short(  (short)( n1.shortValue() + n2.shortValue() ) );
  56.  
  57.     // compare as BigIntegers
  58.     if ( mode.equals( classBigInteger ) )
  59.       return asBigInteger( n1 ).add( asBigInteger( n2 ) );
  60.  
  61.     // compare as BigDecimals
  62.     if ( mode.equals( classBigDecimal ) )
  63.       return asBigDecimal( n1 ).add( asBigDecimal( n2 ) );
  64.  
  65.     // don't know how to deal with mode
  66.     throw new IllegalArgumentException
  67.       (
  68.       "unknown subclass of java.lang.Number: " + mode.getClass()
  69.       );
  70.     }
  71.  
  72.   static Number minus( Number n1, Number n2, Class mode )
  73.     {
  74.     // normal subclasses
  75.     if ( mode.equals( classInteger ) )
  76.       return new Integer( n1.intValue() - n2.intValue() );
  77.     if ( mode.equals( classLong ) )
  78.       return new Long(  n1.longValue() - n2.longValue() );
  79.     if ( mode.equals( classFloat ) )
  80.       return new Float(  n1.floatValue() - n2.floatValue() );
  81.     if ( mode.equals( classDouble ) )
  82.       return new Double(  n1.doubleValue() - n2.doubleValue() );
  83.     if ( mode.equals( classByte ) )
  84.       return new Byte(  (byte)( n1.byteValue() - n2.byteValue() ) );
  85.     if ( mode.equals( classShort ) )
  86.       return new Short(  (short)( n1.shortValue() - n2.shortValue() ) );
  87.  
  88.     // compare as BigIntegers
  89.     if ( mode.equals( classBigInteger ) )
  90.       return asBigInteger( n1 ).subtract( asBigInteger( n2 ) );
  91.  
  92.     // compare as BigDecimals
  93.     if ( mode.equals( classBigDecimal ) )
  94.       return asBigDecimal( n1 ).subtract( asBigDecimal( n2 ) );
  95.  
  96.     // don't know how to deal with mode
  97.     throw new IllegalArgumentException
  98.       (
  99.       "unknown subclass of java.lang.Number: " + mode.getClass()
  100.       );
  101.     }
  102.  
  103.   static Number multiply( Number n1, Number n2, Class mode )
  104.     {
  105.     // normal subclasses
  106.     if ( mode.equals( classInteger ) )
  107.       return new Integer( n1.intValue() * n2.intValue() );
  108.     if ( mode.equals( classLong ) )
  109.       return new Long(  n1.longValue() * n2.longValue() );
  110.     if ( mode.equals( classFloat ) )
  111.       return new Float(  n1.floatValue() * n2.floatValue() );
  112.     if ( mode.equals( classDouble ) )
  113.       return new Double(  n1.doubleValue() * n2.doubleValue() );
  114.     if ( mode.equals( classByte ) )
  115.       return new Byte(  (byte)( n1.byteValue() * n2.byteValue() ) );
  116.     if ( mode.equals( classShort ) )
  117.       return new Short(  (short)( n1.shortValue() * n2.shortValue() ) );
  118.  
  119.     // compare as BigIntegers
  120.     if ( mode.equals( classBigInteger ) )
  121.       return asBigInteger( n1 ).multiply( asBigInteger( n2 ) );
  122.  
  123.     // compare as BigDecimals
  124.     if ( mode.equals( classBigDecimal ) )
  125.       return asBigDecimal( n1 ).multiply( asBigDecimal( n2 ) );
  126.  
  127.     // don't know how to deal with mode
  128.     throw new IllegalArgumentException
  129.       (
  130.       "unknown subclass of java.lang.Number: " + mode.getClass()
  131.       );
  132.     }
  133.  
  134.   static Number divides( Number n1, Number n2, Class mode, int round_mode )
  135.     {
  136.     // normal subclasses
  137.     if ( mode.equals( classInteger ) )
  138.       return new Integer( n1.intValue() / n2.intValue() );
  139.     if ( mode.equals( classLong ) )
  140.       return new Long(  n1.longValue() / n2.longValue() );
  141.     if ( mode.equals( classFloat ) )
  142.       return new Float(  n1.floatValue() / n2.floatValue() );
  143.     if ( mode.equals( classDouble ) )
  144.       return new Double(  n1.doubleValue() / n2.doubleValue() );
  145.     if ( mode.equals( classByte ) )
  146.       return new Byte(  (byte)( n1.byteValue() / n2.byteValue() ) );
  147.     if ( mode.equals( classShort ) )
  148.       return new Short(  (short)( n1.shortValue() / n2.shortValue() ) );
  149.  
  150.     // compare as BigIntegers
  151.     if ( mode.equals( classBigInteger ) )
  152.       return asBigInteger( n1 ).divide( asBigInteger( n2 ) );
  153.  
  154.     // compare as BigDecimals
  155.     if ( mode.equals( classBigDecimal ) )
  156.       return asBigDecimal( n1 ).divide( asBigDecimal( n2 ), round_mode );
  157.  
  158.     // don't know how to deal with mode
  159.     throw new IllegalArgumentException
  160.       (
  161.       "unknown subclass of java.lang.Number: " + mode.getClass()
  162.       );
  163.     }
  164.  
  165.   static Number modulus( Number n1, Number n2, Class mode, int round_mode )
  166.     {
  167.     // normal subclasses
  168.     if ( mode.equals( classInteger ) )
  169.       return new Integer( n1.intValue() % n2.intValue() );
  170.     if ( mode.equals( classLong ) )
  171.       return new Long(  n1.longValue() % n2.longValue() );
  172.     if ( mode.equals( classFloat ) )
  173.       return new Float(  n1.floatValue() % n2.floatValue() );
  174.     if ( mode.equals( classDouble ) )
  175.       return new Double(  n1.doubleValue() % n2.doubleValue() );
  176.     if ( mode.equals( classByte ) )
  177.       return new Byte(  (byte)( n1.byteValue() % n2.byteValue() ) );
  178.     if ( mode.equals( classShort ) )
  179.       return new Short(  (short)( n1.shortValue() % n2.shortValue() ) );
  180.  
  181.     // compare as BigIntegers
  182.     if ( mode.equals( classBigInteger ) )
  183.       return asBigInteger( n1 ).mod( asBigInteger( n2 ) );
  184.  
  185.     // compare as BigDecimals
  186.     if ( mode.equals( classBigDecimal ) )
  187.       {
  188.       // return rounding difference
  189.       BigDecimal b1 = asBigDecimal( n1 );
  190.       BigDecimal b2 = asBigDecimal( n2 );
  191.       BigDecimal d = b1.divide( b2, round_mode );
  192.       return b1.subtract( d.multiply( b2 ) );
  193.       }
  194.  
  195.     // don't know how to deal with mode
  196.     throw new IllegalArgumentException
  197.       (
  198.       "unknown subclass of java.lang.Number: " + mode.getClass()
  199.       );
  200.     }
  201.  
  202.   static int compare( Number n1, Number n2, Class mode )
  203.     {
  204.     // normal subclasses
  205.     if ( mode.equals( classInteger ) )
  206.       return n1.intValue() < n2.intValue()
  207.         ? -1
  208.         : n1.intValue() > n2.intValue()
  209.           ? 1
  210.           : 0;
  211.     if ( mode.equals( classLong ) )
  212.       return n1.longValue() < n2.longValue()
  213.         ? -1
  214.         : n1.longValue() > n2.longValue()
  215.           ? 1
  216.           : 0;
  217.     if ( mode.equals( classFloat ) )
  218.       return n1.floatValue() < n2.floatValue()
  219.         ? -1
  220.         : n1.floatValue() > n2.floatValue()
  221.           ? 1
  222.           : 0;
  223.     if ( mode.equals( classDouble ) )
  224.       return n1.doubleValue() < n2.doubleValue()
  225.         ? -1
  226.         : n1.doubleValue() > n2.doubleValue()
  227.           ? 1
  228.           : 0;
  229.     if ( mode.equals( classByte ) )
  230.       return n1.byteValue() < n2.byteValue()
  231.         ? -1
  232.         : n1.byteValue() > n2.byteValue()
  233.           ? 1
  234.           : 0;
  235.     if ( mode.equals( classShort ) )
  236.       return n1.shortValue() < n2.shortValue()
  237.         ? -1
  238.         : n1.shortValue() > n2.shortValue()
  239.           ? 1
  240.           : 0;
  241.  
  242.     // compare as BigIntegers
  243.     if ( mode.equals( classBigInteger ) )
  244.       return asBigInteger( n1 ).compareTo( asBigInteger( n2 ) );
  245.  
  246.     // compare as BigDecimals
  247.     if ( mode.equals( classBigDecimal ) )
  248.       return asBigDecimal( n1 ).compareTo( asBigDecimal( n2 ) );
  249.  
  250.     // don't know how to deal with mode
  251.     throw new IllegalArgumentException
  252.       (
  253.       "unknown subclass of java.lang.Number: " + mode.getClass()
  254.       );
  255.     }
  256.   }
  257.