From 4cef115e3fbd228a84ad48eed7af5403e8c8c46e Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sat, 13 Apr 2019 20:01:26 -0400 Subject: 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. --- src/org/unitConverter/UnitsDatabase.java | 98 +++++++++++++------------------- 1 file changed, 38 insertions(+), 60 deletions(-) (limited to 'src/org/unitConverter/UnitsDatabase.java') 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 { *
  • 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.
  • - *
  • 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".
  • + *
  • 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".
  • * *

    * @@ -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[] 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[] 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); } -- cgit v1.2.3