Skip to content

Commit

Permalink
unitsofmeasurement#124: fix handling of powered units (creation and s…
Browse files Browse the repository at this point in the history
…imple formatting)
  • Loading branch information
Alexandre Dufour committed Aug 8, 2018
1 parent 8ec8e28 commit 75da478
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/tech/units/indriya/AbstractUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ else if (n == 0)
* @return the result of raising this unit to the exponent.
*/
@Override
public final Unit<?> pow(int n) {
public Unit<?> pow(int n) {
if (n > 0)
return this.multiply(this.pow(n - 1));
else if (n == 0)
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/tech/units/indriya/format/SimpleUnitFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,16 @@ public Appendable format(Unit<?> unit, Appendable appendable) throws IOException

// Product unit.
ProductUnit<?> productUnit = (ProductUnit<?>) unit;

// Special case: self-powered product unit
if (productUnit.getUnitCount() == 1 && productUnit.getUnit(0) instanceof ProductUnit) {
final ProductUnit<?> powerUnit = (ProductUnit<?>) productUnit.getUnit(0);
// is the sub-unit known under a given label?
if (nameFor(powerUnit) == null)
// apply the power to the sub-units and format those instead
return format(ProductUnit.ofPow(powerUnit, productUnit.getUnitPow(0)), appendable);
}

int invNbr = 0;

// Write positive exponents first.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/tech/units/indriya/unit/ProductUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ public static Unit<?> ofPow(Unit<?> unit, int n) {
return getInstance(unitElems, new Element[0]);
}

@Override
public Unit<?> pow(int n) {
return new ProductUnit<>(new Element[] { new Element(this, n, 1) });
}

/**
* Returns the number of unit elements in this product.
*
Expand Down

0 comments on commit 75da478

Please sign in to comment.