* Each line in the file should consist of a name and an expression (parsed by getUnitFromExpression) separated by * any number of tab characters. *
*
* Allowed exceptions: *
* This method accepts exponents, like "L^3" *
* * @param name * dimension's name * @return dimension * @since 2019-03-14 */ public UnitDimension getDimension(final String name) { Objects.requireNonNull(name, "name must not be null."); if (name.contains("^")) { final String[] baseAndExponent = name.split("\\^"); final UnitDimension base = this.getDimension(baseAndExponent[0]); final int exponent; try { exponent = Integer.parseInt(baseAndExponent[baseAndExponent.length - 1]); } catch (final NumberFormatException e2) { throw new IllegalArgumentException("Exponent must be an integer."); } return base.toExponent(exponent); } return this.dimensions.get(name); } /** * Gets a unit. If it is linear, cast it to a LinearUnit and return it. Otherwise, throw an * {@code IllegalArgumentException}. * * @param name * unit's name * @return unit * @since 2019-03-22 */ private LinearUnit getLinearUnit(final String name) { // see if I am using a function-unit like tempC(100) if (name.contains("(") && name.contains(")")) { // break it into function name and value final List* Currently, prefix expressions are much simpler than unit expressions: They are either a number or the name of * another prefix *
* * @param expression * expression to input * @return prefix * @throws IllegalArgumentException * if expression cannot be parsed * @throws NullPointerException * if any argument is null * @since 2019-01-14 * @since v0.1.0 */ public UnitPrefix getPrefixFromExpression(final String expression) { Objects.requireNonNull(expression, "expression must not be null."); try { return new DefaultUnitPrefix(Double.parseDouble(expression)); } catch (final NumberFormatException e) { if (expression.contains("^")) { final String[] baseAndExponent = expression.split("\\^"); final double base; try { base = Double.parseDouble(baseAndExponent[0]); } catch (final NumberFormatException e2) { throw new IllegalArgumentException("Base of exponientation must be a number."); } final int exponent; try { exponent = Integer.parseInt(baseAndExponent[baseAndExponent.length - 1]); } catch (final NumberFormatException e2) { throw new IllegalArgumentException("Exponent must be an integer."); } return new DefaultUnitPrefix(Math.pow(base, exponent)); } else { if (!this.containsPrefixName(expression)) throw new IllegalArgumentException("Unrecognized prefix name \"" + expression + "\"."); return this.getPrefix(expression); } } } /** * Gets a unit from the database from its name, ignoring prefixes. * * @param name * unit's name * @return unit * @since 2019-01-10 * @since v0.1.0 */ public Unit getPrefixlessUnit(final String name) { return this.units.get(name); } /** * Gets a unit from the database from its name, looking for prefixes. * * @param name * unit's name * @return unit * @since 2019-01-10 * @since v0.1.0 */ public Unit getUnit(final String name) { try { final double value = Double.parseDouble(name); return SI.SI.getBaseUnit(UnitDimension.EMPTY).times(value); } catch (final NumberFormatException e) { if (name.contains("^")) { final String[] baseAndExponent = name.split("\\^"); LinearUnit base; try { base = SI.SI.getBaseUnit(UnitDimension.EMPTY).times(Double.parseDouble(baseAndExponent[0])); } catch (final NumberFormatException e2) { final Unit unit = this.getUnit(baseAndExponent[0]); if (unit instanceof LinearUnit) { base = (LinearUnit) unit; } else throw new IllegalArgumentException("Base of exponientation must be a linear or base unit."); } final int exponent; try { exponent = Integer.parseInt(baseAndExponent[baseAndExponent.length - 1]); } catch (final NumberFormatException e2) { throw new IllegalArgumentException("Exponent must be an integer."); } final LinearUnit exponentiated = base.toExponent(exponent); if (exponentiated.getConversionFactor() == 1) return exponentiated.getSystem().getBaseUnit(exponentiated.getDimension()); else return exponentiated; } else { for (final String prefixName : this.prefixNameSet()) { // check for a prefix if (name.startsWith(prefixName)) { // prefix found! Make sure what comes after it is actually a unit! final String prefixless = name.substring(prefixName.length()); if (this.containsUnitName(prefixless)) { // yep, it's a proper prefix! Get the unit! final Unit unit = this.getUnit(prefixless); final UnitPrefix prefix = this.getPrefix(prefixName); // Prefixes only work with linear and base units, so make sure it's one of those if (unit instanceof LinearUnit) { final LinearUnit linearUnit = (LinearUnit) unit; return linearUnit.times(prefix.getMultiplier()); } else if (unit instanceof BaseUnit) { final BaseUnit baseUnit = (BaseUnit) unit; return baseUnit.times(prefix.getMultiplier()); } } } } return this.units.get(name); } } } /** * Uses the database's unit data to parse an expression into a unit ** The expression is a series of any of the following: *