Class FunctionOfUnit
- java.lang.Object
-
- uk.me.nxg.unity.OneUnit
-
- uk.me.nxg.unity.FunctionOfUnit
-
- All Implemented Interfaces:
java.lang.Comparable<OneUnit>
public class FunctionOfUnit extends OneUnit
Represents a function of a unit, such aslog(m)
. This consists of a function (‘log’' in this case) and an operand (‘m’' in this case). The interpretation oflog(m)
is that it represents the log of the dimensionless number obtained by dividing a quantity (which presumably has the dimensions of length) by the quantity 1 m.The functions
getBaseUnitName()
,getBaseUnitDefinition()
, andgetBaseUnitString()
, apply to the operand. In particular, they give the information about the first unit in the function operand, even if it has multiple units (such aslog(V^2/mm)
. This isn't ideal, but it's probably roughly what is wanted in the majority of cases where this function is used.The functions
isRecognisedUnit(uk.me.nxg.unity.Syntax)
andisRecommendedUnit(uk.me.nxg.unity.Syntax)
are true if they would be true when applied to the operand and the function is a recognised in this syntax; there are no usage constraints on functions, so the functionsatisfiesUsageConstraints(uk.me.nxg.unity.Syntax)
is true if it would be true when applied to the operand.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(OneUnit o)
boolean
equals(java.lang.Object o)
UnitDefinition
getBaseUnitDefinition()
Returns the known base unit.java.lang.String
getBaseUnitName()
Returns the name of this unit.java.lang.String
getBaseUnitString()
Dimensions
getDimensions()
Return the dimensions of the unit, if it is a recognised one.FunctionDefinition
getFunctionDefinition()
The function which is applied to the operand.java.lang.String
getFunctionName()
The function which is applied to the operand.java.lang.String
getOriginalUnitString()
Obtains the representation of the unit, as it originally appeared in the parsed input.int
getPrefix()
Returns the prefix of the unit, as a base-ten log.OneUnit
getUnit(UnitDefinition reqUnit)
Returns the unit in the operand which corresponds to the required unit.int
hashCode()
boolean
isRecognisedUnit()
Indicates whether the base unit is one of those recognised in any syntax.boolean
isRecognisedUnit(Syntax syntax)
Indicates whether the base unit is one of those recognised within the specification of the given syntax.boolean
isRecommendedUnit(Syntax syntax)
Indicates whether the base unit is one of those recommended within the specification of the given syntax.boolean
satisfiesUsageConstraints(Syntax syntax)
Indicates whether the unit is being used in a way which satisfies any usage constraints.java.lang.String
toDebugString()
Write out the unit in a testable format.java.lang.String
toString()
Format this unit in readable form.java.lang.String
toString(Syntax syntax)
Format this unit in readable form, appropriate to the given syntax.java.lang.String
unitString(Syntax syntax)
Obtains the string representation of the unit, including prefix, in the given syntax.-
Methods inherited from class uk.me.nxg.unity.OneUnit
divide, getExponent, isQuoted, reciprocate, wasGuessed
-
-
-
-
Method Detail
-
getPrefix
public int getPrefix()
Description copied from class:OneUnit
Returns the prefix of the unit, as a base-ten log. Thus a prefix of"m"
, for ‘milli’, would produce a prefix of -3.
-
getDimensions
public Dimensions getDimensions()
Description copied from class:OneUnit
Return the dimensions of the unit, if it is a recognised one. If this isn't a recognised unit, return null.- Specified by:
getDimensions
in classOneUnit
- Returns:
- the dimensions of the unit, or null if these aren't avaiable
-
getBaseUnitDefinition
public UnitDefinition getBaseUnitDefinition()
Description copied from class:OneUnit
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, thoughOneUnit.getBaseUnitName()
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.- Specified by:
getBaseUnitDefinition
in classOneUnit
- Returns:
- a base unit
-
getBaseUnitName
public java.lang.String getBaseUnitName()
Description copied from class:OneUnit
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()
.- Specified by:
getBaseUnitName
in classOneUnit
- Returns:
- the string name of this unit
-
getBaseUnitString
public java.lang.String getBaseUnitString()
-
getUnit
public OneUnit getUnit(UnitDefinition reqUnit)
Returns the unit in the operand which corresponds to the required unit.- Parameters:
reqUnit
- a unit representing the one we are to extract- Returns:
- a unit
-
isRecognisedUnit
public boolean isRecognisedUnit(Syntax syntax)
Description copied from class:OneUnit
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.
- Specified by:
isRecognisedUnit
in classOneUnit
- Parameters:
syntax
- one of the syntaxes ofSyntax
- Returns:
- true if the unit is recognised
- See Also:
OneUnit.isRecommendedUnit(uk.me.nxg.unity.Syntax)
-
isRecognisedUnit
public boolean isRecognisedUnit()
Description copied from class:OneUnit
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, thenOneUnit.getBaseUnitDefinition()
would return non-null, and vice versa if this method returns false.- Specified by:
isRecognisedUnit
in classOneUnit
- Returns:
- true if the unit is recognised
-
isRecommendedUnit
public boolean isRecommendedUnit(Syntax syntax)
Description copied from class:OneUnit
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.
- Specified by:
isRecommendedUnit
in classOneUnit
- Parameters:
syntax
- one of the syntaxes ofSyntax
- Returns:
- true if the unit is a recommended one
- See Also:
OneUnit.isRecognisedUnit(uk.me.nxg.unity.Syntax)
-
satisfiesUsageConstraints
public boolean satisfiesUsageConstraints(Syntax syntax)
Description copied from class:OneUnit
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.
- Specified by:
satisfiesUsageConstraints
in classOneUnit
- Parameters:
syntax
- one of the syntaxes ofSyntax
- Returns:
- true if the unit satisfies its usage constraints
-
toDebugString
public java.lang.String toDebugString()
Description copied from class:OneUnit
Write out the unit in a testable format. This is for debugging and testing.- Specified by:
toDebugString
in classOneUnit
- Returns:
- a string representation of the unit
-
toString
public java.lang.String toString()
Description copied from class:OneUnit
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
public java.lang.String toString(Syntax syntax)
Description copied from class:OneUnit
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()
.
-
unitString
public java.lang.String unitString(Syntax syntax) throws UnwritableExpression
Description copied from class:OneUnit
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.
- Specified by:
unitString
in classOneUnit
- 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
-
getFunctionDefinition
public FunctionDefinition getFunctionDefinition()
The function which is applied to the operand. If this is not a recognised function in the originating syntax, then this returns null. Either this function orgetFunctionName()
returns non-null, and not both.- Returns:
- a function definition
-
getFunctionName
public java.lang.String getFunctionName()
The function which is applied to the operand. If this is not a recognised function in the originating syntax, then this returns null. Either this function orgetFunctionDefinition()
returns non-null, and not both.- Returns:
- a string naming the function
-
getOriginalUnitString
public java.lang.String getOriginalUnitString()
Description copied from class:OneUnit
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.- Specified by:
getOriginalUnitString
in classOneUnit
- Returns:
- the original input representation of the unit
-
compareTo
public int compareTo(OneUnit o)
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
-