summaryrefslogtreecommitdiff
path: root/src/org/unitConverter/unit/LinearUnit.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/unitConverter/unit/LinearUnit.java')
-rw-r--r--src/org/unitConverter/unit/LinearUnit.java26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/org/unitConverter/unit/LinearUnit.java b/src/org/unitConverter/unit/LinearUnit.java
index 1532fc4..1e5ae53 100644
--- a/src/org/unitConverter/unit/LinearUnit.java
+++ b/src/org/unitConverter/unit/LinearUnit.java
@@ -350,14 +350,38 @@ public final class LinearUnit extends Unit {
/**
* Returns the result of applying {@code prefix} to this unit.
+ * <p>
+ * If this unit and the provided prefix have a primary name, the returned unit will have a primary name (prefix's
+ * name + unit's name). <br>
+ * If this unit and the provided prefix have a symbol, the returned unit will have a symbol. <br>
+ * This method ignores alternate names of both this unit and the provided prefix.
*
* @param prefix
* prefix to apply
* @return unit with prefix
* @since 2019-03-18
* @since v0.2.0
+ * @throws NullPointerException
+ * if prefix is null
*/
public LinearUnit withPrefix(final UnitPrefix prefix) {
- return this.times(prefix.getMultiplier());
+ final LinearUnit unit = this.times(prefix.getMultiplier());
+
+ // create new name and symbol, if possible
+ final String name;
+ if (this.getPrimaryName().isPresent() && prefix.getPrimaryName().isPresent()) {
+ name = prefix.getPrimaryName().get() + this.getPrimaryName().get();
+ } else {
+ name = null;
+ }
+
+ final String symbol;
+ if (this.getSymbol().isPresent() && prefix.getSymbol().isPresent()) {
+ symbol = prefix.getSymbol().get() + this.getSymbol().get();
+ } else {
+ symbol = null;
+ }
+
+ return unit.withName(NameSymbol.ofNullable(name, symbol));
}
}