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 / DividesNumber.java < prev    next >
Encoding:
Java Source  |  1997-03-14  |  3.1 KB  |  80 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.BigDecimal;
  7.  
  8. /**
  9.  * DividesNumber is a binary function that assumes that both of its operands are
  10.  * instances of Number and returns the first operand divided by the second operand.
  11.  * <p>
  12.  * @see java.lang.Number
  13.  * @see java.math.BigInteger
  14.  * @see java.math.BigDecimal
  15.  * @version 2.0.2
  16.  * @author ObjectSpace, Inc.
  17.  */
  18.  
  19. public final class DividesNumber implements BinaryFunction
  20.   {
  21.   private Class mode;
  22.   private int rounding;
  23.  
  24.   /**
  25.    * Construct myself to use intValue() for operation.
  26.    */
  27.   public DividesNumber()
  28.     {
  29.     mode = new Integer( 0 ).getClass();
  30.     rounding = java.math.BigDecimal.ROUND_DOWN;
  31.     }
  32.  
  33.   /**
  34.    * Construct myself to operate on objects of the given class.  The class must
  35.    * be derived from java.lang.Number.  If the discriminating class is
  36.    * java.math.BigDecimal, use ROUND_DOWN as the rounding mode.
  37.    * @param discriminator The class of objects on which I will be operating.
  38.    * @exception java.lang.IllegalArgumentException Throw if discriminator is not an instance of java.lang.Number.
  39.    * @see java.math.BigDecimal
  40.    */
  41.   public DividesNumber( Class discriminator )
  42.     {
  43.     if ( !NumberHelper.classNumber.isAssignableFrom( discriminator ) )
  44.       throw new IllegalArgumentException( "discriminator must be an instance of java.lang.Number" );
  45.     mode = discriminator;
  46.     rounding = java.math.BigDecimal.ROUND_DOWN;
  47.     }
  48.  
  49.   /**
  50.    * Construct myself to operate on objects of the given class.  The class must
  51.    * be derived from java.lang.Number.  If the class is an instance of
  52.    * java.math.BigDecimal, use the given rounding mode.
  53.    * @param discriminator The class of objects on which I will be operating.
  54.    * @param rounding The specified rounding mode. Only used when the discriminator is a java.math.BigDecimal.
  55.    * @exception java.lang.IllegalArgumentException Throw if discriminator is not an instance of java.lang.Number.
  56.    * @see java.math.BigDecimal#divide
  57.    */
  58.   public DividesNumber( Class discriminator, int rounding )
  59.     {
  60.     if ( !NumberHelper.classNumber.isAssignableFrom( discriminator ) )
  61.       throw new IllegalArgumentException( "discriminator must be an instance of java.lang.Number" );
  62.     mode = discriminator;
  63.     this.rounding = rounding;
  64.     }
  65.  
  66.   /**
  67.    * Return the result of dividing the first operand by the second operand.
  68.    * Be aware that some floating point conversions are not exact, and may
  69.    * cause unexpected results due to rounding.
  70.    * @param first The first operand, which must be an instance of Number.
  71.    * @param second The second operand, which must be an instance of Number.
  72.    * @exception COM.objectspace.jgl.InvalidOperationException Throw if I don't know how to interpret the values.
  73.    * @return first / second
  74.    */
  75.   public Object execute( Object first, Object second )
  76.     {
  77.     return NumberHelper.divides( (Number)first, (Number)second, mode, rounding );
  78.     }
  79.   }
  80.