Class OneUnit
- All Implemented Interfaces:
Comparable<OneUnit>
- Direct Known Subclasses:
FunctionOfUnit
,SimpleUnit
The class's instances are immutable.
This class has no public constructors. To obtain instances of
this class, parse a unit string using a UnitParser
.
A unit can be ‘quoted’, indicating that it is to be parsed
as an ‘unknown’ unit even if its name matches that of a ‘known’ unit
(for example, 'B'
is a unit of ‘B’, and neither byte
nor Bel, and the 'furlong'
is a ‘furlong’ and not, as
it would otherwise be parsed, a femto-urlong).
This mechanism is syntactically permitted only in the VOUnits syntax,
and is used only for output and validity checks
(see isRecognisedUnit(uk.me.nxg.unity.Syntax)
), and not for processing.
All ‘quoted’ units are classed as not ‘recognised’.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract int
Divide one list of units by anotherabstract UnitDefinition
Returns the known base unit.abstract String
Returns the name of this unit.abstract Dimensions
Return the dimensions of the unit, if it is a recognised one.float
Obtains the power the unit is raised to.abstract String
Obtains the representation of the unit, as it originally appeared in the parsed input.abstract int
Returns the prefix of the unit, as a base-ten log.boolean
isQuoted()
Is this a ‘quoted’ unit?abstract boolean
Indicates whether the base unit is one of those recognised in any syntax.abstract boolean
isRecognisedUnit
(Syntax syntax) Indicates whether the base unit is one of those recognised within the specification of the given syntax.abstract boolean
isRecommendedUnit
(Syntax syntax) Indicates whether the base unit is one of those recommended within the specification of the given syntax.reciprocate
(Iterable<OneUnit> ul) Produce the reciprocal of the unit, which is a unit which has the negative of the exponent of this one.abstract boolean
satisfiesUsageConstraints
(Syntax syntax) Indicates whether the unit is being used in a way which satisfies any usage constraints.abstract String
Write out the unit in a testable format.abstract String
toString()
Format this unit in readable form.abstract String
Format this unit in readable form, appropriate to the given syntax.abstract String
unitString
(Syntax syntax) Obtains the string representation of the unit, including prefix, in the given syntax.boolean
Return true if this unit was guessed.
-
Constructor Details
-
OneUnit
protected OneUnit(float exponent, boolean isQuoted) OneUnit constructor is protected – it should only be invoked by subclass constructors, and they may be invoked from theOneUnit.Maker
class.- Parameters:
exponent
- the power of the base unitisQuoted
- true, if this is an explicitly quoted unit (VOUnits syntax only)
-
OneUnit
protected OneUnit(float exponent)
-
-
Method Details
-
isQuoted
public boolean isQuoted()Is this a ‘quoted’ unit?- Returns:
- true if this unit is quoted
-
getPrefix
public abstract int getPrefix()Returns the prefix of the unit, as a base-ten log. Thus a prefix of"m"
, for ‘milli’, would produce a prefix of -3.- Returns:
- the power of ten which multiplies the unit
-
getBaseUnitDefinition
Returns the known base unit. If the unit wasn't recognised as a known unit in the syntax in which the string was parsed, then this returns null, thoughgetBaseUnitName()
will not.Note that the ‘base unit’ is simply the unit without the prefix, and doesn't refer to the fundamental SI base units. Thus in the expression
"MW"
, it is ‘W’, Watt, that is the base unit.- Returns:
- a base unit
-
getBaseUnitName
Returns the name of this unit. If the unit is a known one, then this will return a name for the unit such as ‘Metre’, or ‘Julian year’; if it is not, then this can do no more than return the unit symbol.This should be used for identification or similar purposes. To write out the unit you should generally use
UnitExpr.toString()
.- Returns:
- the string name of this unit
-
getDimensions
Return the dimensions of the unit, if it is a recognised one. If this isn't a recognised unit, return null.- Returns:
- the dimensions of the unit, or null if these aren't avaiable
-
getExponent
public float getExponent()Obtains the power the unit is raised to. For example, for the unitmm^2
, return 2.- Returns:
- the power the unit is raised to
-
wasGuessed
public boolean wasGuessed()Return true if this unit was guessed. That is, if it was identified heuristically, by using a parser configured withUnitParser.setGuessing(boolean)
- Returns:
- true if the unit was guessed
-
isRecognisedUnit
Indicates whether the base unit is one of those recognised within the specification of the given syntax. In this context, ‘recognised’ means ‘mentioned in the specification’, so deprecated units count as recognised ones.Note that this checks that the unit is a recognised one: we don't (currently) check whether the abbreviation that got us here is a recommended one (for example, ‘pixel’ is a valid FITS/CDS name for pixels, and ‘pix’ is a FITS and OGIP one). This also means that if we guessed a unit, and that unit is a recognised one in this syntax, then this method returns true.
If the syntax is Syntax.ALL, then this checks whether this is a recognised unit in any syntax.
- Parameters:
syntax
- one of the syntaxes ofSyntax
- Returns:
- true if the unit is recognised
- See Also:
-
isRecognisedUnit
public abstract boolean isRecognisedUnit()Indicates whether the base unit is one of those recognised in any syntax.This is equivalent to
isRecognisedUnit(Syntax.ALL)
. If this method returns true, thengetBaseUnitDefinition()
would return non-null, and vice versa if this method returns false.- Returns:
- true if the unit is recognised
-
isRecommendedUnit
Indicates whether the base unit is one of those recommended within the specification of the given syntax. In this context, ‘recommended’ means ‘mentioned in the specification’ and not deprecated. Thus all ‘recommended’ units are also a fortiori ‘recognised’.Note that this checks that the unit is a recommended one: we don't (currently) check whether the abbreviation that got us here is a recommended one (for example, "pixel" is a valid FITS/CDS name for pixels, and "pix" is a FITS and OGIP one). This also means that if we guessed a unit, and that unit is a recommended one in this syntax, then this method returns true.
- Parameters:
syntax
- one of the syntaxes ofSyntax
- Returns:
- true if the unit is a recommended one
- See Also:
-
satisfiesUsageConstraints
Indicates whether the unit is being used in a way which satisfies any usage constraints. Principally, this tests whether a unit which may not be used with SI prefixes was provided with a prefix, but there may be other constraints present.An unrecognised unit has no constraints, and so will always satisfy them; this extends to units which are unrecognised in a particular syntax.
- Parameters:
syntax
- one of the syntaxes ofSyntax
- Returns:
- true if the unit satisfies its usage constraints
-
toString
Format this unit in readable form. The form is unspecified.This should not generally be used for formatted output – for that, it will more often be better to use
UnitExpr.toString()
. -
toString
Format this unit in readable form, appropriate to the given syntax. The form is unspecified.This should not generally be used for formatted output – for that, it will more often be better to use
UnitExpr.toString()
.- Parameters:
syntax
- one of the known syntaxes- Returns:
- a string representation of the unit
- Throws:
UnwritableExpression
- if the unit can't be written in the given syntax
-
unitString
Obtains the string representation of the unit, including prefix, in the given syntax. That is, return ‘mm’ not ‘m’ or ‘metre’. If a unit has more than one representation in a given syntax, then this will produced the ‘preferred one’.This will fail, with a UnitParserException, in the rare cases where the unit string is inexpressible in the given syntax: for example the CDS syntax permits only the log function to be applied to a unit, and will fail if it is asked to display a different function.
- Parameters:
syntax
- one of the syntaxes ofSyntax
- Returns:
- a non-null string representation of the unit
- Throws:
UnwritableExpression
- if the unit string is inexpressible in the given syntax
-
getOriginalUnitString
Obtains the representation of the unit, as it originally appeared in the parsed input. This will differ from what is produced byunitString
if (a) the original was a dispreferred representation of a recognised unit (for example ‘yr’ vs ‘a’), or (b) if the unit was (successfully) guessed from an otherwise unrecognised string.- Returns:
- the original input representation of the unit
-
toDebugString
Write out the unit in a testable format. This is for debugging and testing.- Returns:
- a string representation of the unit
-
reciprocate
Produce the reciprocal of the unit, which is a unit which has the negative of the exponent of this one.- Parameters:
ul
- the expression to be reciprocated- Returns:
- a new instance
-
divide
Divide one list of units by another- Parameters:
num
- the units in the numeratorden
- the units in the denominator- Returns:
- a list of units
-
compareTo
- Specified by:
compareTo
in interfaceComparable<OneUnit>
-