diff options
author | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2019-04-13 20:01:26 -0400 |
---|---|---|
committer | Adrien Hopkins <adrien.p.hopkins@gmail.com> | 2019-04-13 20:01:26 -0400 |
commit | 4cef115e3fbd228a84ad48eed7af5403e8c8c46e (patch) | |
tree | 721143524a0f7a467df7fddbee163158ea7335a2 | |
parent | 63dd50e5d7a5daa0bcbdd00608543d4572c870ea (diff) |
Longer prefixes are now favoured over shorter prefixes.
Added 'da-' to the unit file, which was previously missing because it
was interpreted as 'deciatto'. 'D-' can still be used.
-rwxr-xr-x | src/org/unitConverter/UnitsDatabase.java | 98 | ||||
-rwxr-xr-x | unitsfile.txt | 1 |
2 files changed, 39 insertions, 60 deletions
diff --git a/src/org/unitConverter/UnitsDatabase.java b/src/org/unitConverter/UnitsDatabase.java index 9749e9c..901c6ef 100755 --- a/src/org/unitConverter/UnitsDatabase.java +++ b/src/org/unitConverter/UnitsDatabase.java @@ -67,8 +67,8 @@ public final class UnitsDatabase { * <li>Before attempting to search for prefixes in a unit name, this map will first search for a unit name. So, if * there are two units, "B" and "AB", and a prefix "A", this map will favour the unit "AB" over the unit "B" with * the prefix "A", even though they have the same string.</li> - * <li>Shorter prefixes are preferred to longer prefixes. So, if you have units "BC" and "C", and prefixes "AB" and - * "A", inputting "ABC" will return the unit "BC" with the prefix "A", not "C" with the prefix "AB".</li> + * <li>Longer prefixes are preferred to shorter prefixes. So, if you have units "BC" and "C", and prefixes "AB" and + * "A", inputting "ABC" will return the unit "C" with the prefix "AB", not "BC" with the prefix "A".</li> * </ul> * </p> * @@ -194,6 +194,7 @@ public final class UnitsDatabase { } } + // create the unit name final StringBuilder unitNameBuilder = new StringBuilder(); for (final int i : this.prefixCoordinates) { unitNameBuilder.append(this.prefixNames.get(i)); @@ -256,32 +257,20 @@ public final class UnitsDatabase { @Override public Object[] toArray() { - if (this.map.units.isEmpty()) - // finite, it will work + if (this.map.units.isEmpty() || this.map.prefixes.isEmpty()) return super.toArray(); - else { - if (this.map.prefixes.isEmpty()) - // finite, it will work - return super.toArray(); - else - // infinite set - throw new UnsupportedOperationException("Cannot make an infinite set into an array."); - } + else + // infinite set + throw new UnsupportedOperationException("Cannot make an infinite set into an array."); } @Override public <T> T[] toArray(final T[] a) { - if (this.map.units.isEmpty()) - // finite, it will work + if (this.map.units.isEmpty() || this.map.prefixes.isEmpty()) return super.toArray(a); - else { - if (this.map.prefixes.isEmpty()) - // finite, it will work - return super.toArray(a); - else - // infinite set - throw new UnsupportedOperationException("Cannot make an infinite set into an array."); - } + else + // infinite set + throw new UnsupportedOperationException("Cannot make an infinite set into an array."); } } @@ -437,32 +426,21 @@ public final class UnitsDatabase { @Override public Object[] toArray() { - if (this.map.units.isEmpty()) - // finite, it will work + if (this.map.units.isEmpty() || this.map.prefixes.isEmpty()) return super.toArray(); - else { - if (this.map.prefixes.isEmpty()) - // finite, it will work - return super.toArray(); - else - // infinite set - throw new UnsupportedOperationException("Cannot make an infinite set into an array."); - } + else + // infinite set + throw new UnsupportedOperationException("Cannot make an infinite set into an array."); + } @Override public <T> T[] toArray(final T[] a) { - if (this.map.units.isEmpty()) - // finite, it will work + if (this.map.units.isEmpty() || this.map.prefixes.isEmpty()) return super.toArray(a); - else { - if (this.map.prefixes.isEmpty()) - // finite, it will work - return super.toArray(a); - else - // infinite set - throw new UnsupportedOperationException("Cannot make an infinite set into an array."); - } + else + // infinite set + throw new UnsupportedOperationException("Cannot make an infinite set into an array."); } } @@ -532,26 +510,26 @@ public final class UnitsDatabase { throw new IllegalArgumentException("Attempted to test for a unit using a non-string name."); final String unitName = (String) key; - // Then, look for the shortest prefix that is attached to a valid unit - String shortestPrefix = null; - int shortestLength = Integer.MAX_VALUE; + // Then, look for the longest prefix that is attached to a valid unit + String longestPrefix = null; + int longestLength = 0; for (final String prefixName : this.prefixes.keySet()) { // a prefix name is valid if: // - it is prefixed (i.e. the unit name starts with it) - // - it is shorter than the existing largest prefix (since I am looking for the smallest valid prefix) + // - it is longer than the existing largest prefix (since I am looking for the longest valid prefix) // - the part after the prefix is a valid unit name // - the unit described that name is a linear unit (since only linear units can have prefixes) - if (unitName.startsWith(prefixName) && prefixName.length() < shortestLength) { + if (unitName.startsWith(prefixName) && prefixName.length() > longestLength) { final String rest = unitName.substring(prefixName.length()); if (this.containsKey(rest) && this.get(rest) instanceof LinearUnit) { - shortestPrefix = prefixName; - shortestLength = prefixName.length(); + longestPrefix = prefixName; + longestLength = prefixName.length(); } } } - return shortestPrefix != null; + return longestPrefix != null; } @Override @@ -578,34 +556,34 @@ public final class UnitsDatabase { throw new IllegalArgumentException("Attempted to obtain a unit using a non-string name."); final String unitName = (String) key; - // Then, look for the shortest prefix that is attached to a valid unit - String shortestPrefix = null; - int shortestLength = Integer.MAX_VALUE; + // Then, look for the longest prefix that is attached to a valid unit + String longestPrefix = null; + int longestLength = 0; for (final String prefixName : this.prefixes.keySet()) { // a prefix name is valid if: // - it is prefixed (i.e. the unit name starts with it) - // - it is shorter than the existing largest prefix (since I am looking for the smallest valid prefix) + // - it is longer than the existing largest prefix (since I am looking for the longest valid prefix) // - the part after the prefix is a valid unit name // - the unit described that name is a linear unit (since only linear units can have prefixes) - if (unitName.startsWith(prefixName) && prefixName.length() < shortestLength) { + if (unitName.startsWith(prefixName) && prefixName.length() > longestLength) { final String rest = unitName.substring(prefixName.length()); if (this.containsKey(rest) && this.get(rest) instanceof LinearUnit) { - shortestPrefix = prefixName; - shortestLength = prefixName.length(); + longestPrefix = prefixName; + longestLength = prefixName.length(); } } } // if none found, returns null - if (shortestPrefix == null) + if (longestPrefix == null) return null; else { // get necessary data - final String rest = unitName.substring(shortestLength); + final String rest = unitName.substring(longestLength); // this cast will not fail because I verified that it would work before selecting this prefix final LinearUnit unit = (LinearUnit) this.get(rest); - final UnitPrefix prefix = this.prefixes.get(shortestPrefix); + final UnitPrefix prefix = this.prefixes.get(longestPrefix); return unit.withPrefix(prefix); } diff --git a/unitsfile.txt b/unitsfile.txt index 14fb6fb..78f8117 100755 --- a/unitsfile.txt +++ b/unitsfile.txt @@ -55,6 +55,7 @@ atto- 1e-18 zepto- 1e-21 yocto- 1e-24 +da- deca D- deca h- hecto H- hecto |