All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gw.util.science.AbstractBinaryUnit.gs Maven / Gradle / Ivy

There is a newer version: 1.18.2
Show newest version
package gw.util.science

uses gw.util.Rational
uses gw.util.concurrent.Cache

abstract class AbstractBinaryUnit,
                                  B extends IUnit,
                                  D extends IDimension,
                                  U extends AbstractBinaryUnit> implements IUnit {
  final var _leftUnit: A
  final var _rightUnit: B
  final var _factor : Rational as Factor
  final var _name: String as UnitName
  final var _symbol: String as UnitSymbol

  protected construct( leftUnit: A, rightUnit: B, factor: Rational = null, name: String = null, symbol: String = null ) {
    _leftUnit = leftUnit
    _rightUnit = rightUnit
    _factor = factor ?: 1
    _name = name
    _symbol = symbol
  }
  
  protected property get LeftUnit() : A {
    return _leftUnit
  }
  protected property get RightUnit() : B {
    return _rightUnit  
  }
  
  override function from( r: D ) : Rational {
    return r.toBaseNumber() / toBaseUnits( 1 )
  }   

  override function toString() : String {
    return UnitName
  }

  override function hashCode() : int {
    return 31*(31*_leftUnit.hashCode() + _rightUnit.hashCode()) + _factor.hashCode()
  }
  
  override function equals( obj: Object ) : boolean {
    if( obj.Class != Class ) {
      return false
    }
    
    var that = obj as AbstractBinaryUnit
    
    return _leftUnit == that.LeftUnit
           && _rightUnit == that.RightUnit
           && _factor == that._factor
           // note we don't want name here since we look up based on just left & right units 
           // i.e., we don't want to have to name units all the time, just look them up by left & right units
           // && _name == that._name
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy