public List < UnitValue > sequence ( UnitValue value , UnitFactorSet factorSet ) { value = convert ( value , factorSet . base ( ) ) ; boolean negative = false ; BigDecimal n = value . amount ( ) ; // Use absolute value for all comparisons. if ( n . signum ( ) == - 1 ) { negative = true ; n = n . abs ( ) ; } List < UnitValue > result = new ArrayList <> ( ) ; List < UnitValue > factors = factorSet . factors ( ) ; int size = factors . size ( ) ; int last = size - 1 ; for ( int i = 0 ; i < size ; i ++ ) { UnitValue factor = factors . get ( i ) ; BigDecimal divisor = factor . amount ( ) ; if ( divisor == null ) { continue ; } if ( i == last && ( n . compareTo ( BigDecimal . ZERO ) != 0 || result . isEmpty ( ) ) ) { // Include decimals on the last unit. int scale = n . precision ( ) + divisor . precision ( ) ; BigDecimal res = n . divide ( divisor , scale , RoundingMode . HALF_EVEN ) ; if ( negative && result . isEmpty ( ) ) { res = res . negate ( ) ; } result . add ( new UnitValue ( res . stripTrailingZeros ( ) , factor . unit ( ) ) ) ; } else if ( divisor . compareTo ( n ) <= 0 ) { BigDecimal [ ] res = n . divideAndRemainder ( divisor ) ; if ( negative && result . isEmpty ( ) ) { res [ 0 ] = res [ 0 ] . negate ( ) ; } result . add ( new UnitValue ( res [ 0 ] . stripTrailingZeros ( ) , factor . unit ( ) ) ) ; n = res [ 1 ] ; } } return result ; }
Breaks down the unit value into a sequence of units using the given divisors and allowed units .