|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcds.astro.Unit
public class Unit
Class able to deal with scientific units and make the appropriate
conversions.
This class is an application of the
"Adopted Standards for Astronomical Catalogues"
(http://vizier.u-strasbg.fr/doc/catstd.htx).
It is tightly related to the class Converter
which can
define non-standard conversions.
Basically, a unit consists in a value associated to a
unit symbol which is itself related to the SI
(Système International). Most of the unit symbols in use in
astronomy are included, but it is possible to add the definition of new units
with their equivalence via the addSymbol(java.lang.String, java.lang.String, java.lang.String)
method.
The class can also deal with units expressed in log scale like [km/s] equivalent to log(km/s), or in mag scale which is defined as -2.5log. Values may be transformed between the linear and log scales with the log, mag and dexp methods; conversions may also be performed via the convert methods.
The operations on units include the basic operations plus, minus, mult, div and power and the square root sqrt. Addition or multiplications on physical values would however better use the sum or prod methods, which take care of converting the log scales. To illustrate the differences, assuming that u1 and u2 have the same content of 5mag, the operation u1.add(u2) gives the result 10mag, while u1.sum(u2) gives a value around 4.25mag corresponding to the brightness of a source merging the two brightnesses.
For repetitive operations, like working on values from a table, it is suggested to use the setValue method which assigns the value but keeps the unit symbol and definition.
The numbers with their associated unit are edited as a single word (without embedded blanks); the SI equivalent may be edited with the toStringInSI method. The numbers in their edited form use an exponential notation mx10+n; the classical form mE+n is also correctly interpreted. Values in sexagesimal notation are also interpreted correctly in setValue provided their associated unit is "h:m:s" (time) or "d:m:s" (angle) --- note that the quotes " around the unit symbol are required!
An example of a program reading data all with the same km/s unit could be:
Unit aUnit = new Unit("km/s") ; while (true) { aUnit.setValue(stdin.readLine()) ; System.out.println("Complete Value: " + aUnit); System.out.println(" SI Equivalent: " + aUnit.toStringInSI()); System.out.println(" Unit Meaning: " + aUnit.explainUnit()); }
The recursive (BNF) definition of a Unit is as follows:
Full_Unit = factor Complex_unit Complex_Unit = single_Unit | single_Unit OP single_Unit | "log[" single_Unit OP single_Unit "]" | "mag[" single_Unit OP single_Unit "]" single_Unit = extended_UnitSymbol | extended_UnitSymbol power extended_UnitSymbol = UnitSymbol | Magnitude_Prefix UnitSymbol | "(" Full_Unit ")" power = Number | +Number | -Number OP = . | /
Field Summary | |
---|---|
java.lang.String |
symbol
The symbolic unit, like km/s |
double |
value
The value, expressed in unit symbol |
Constructor Summary | |
---|---|
Unit()
Define the default unit: unitless undefined value |
|
Unit(java.lang.String symbol)
Define a Value+Unit by interpreting a String |
|
Unit(Unit u)
Define a Unit and Value from an existing one (clone) |
Method Summary | |
---|---|
static Unit |
addSymbol(java.lang.String symbol,
java.lang.String equiv)
Introduce a new Symbol into the list of Symbols. |
static Unit |
addSymbol(java.lang.String symbol,
java.lang.String equiv,
java.lang.String explain)
Introduce a new Symbol into the list of Symbols. |
static boolean |
checkSymbol(java.lang.String symbol)
Check whether a symbol exists. |
static void |
convert(Unit source_unit,
Unit target_unit)
Convert a unit+value into another unit+value. |
void |
convertFrom(Unit source_unit)
Convert the value in argument to the target Unit. |
void |
convertTo(Unit unit)
Convert the value into another unit. |
static void |
convertUnit(Unit source_unit,
Unit target_unit)
Convert a unit+value into another unit+value. |
void |
dexp()
Convert a value to its linear scale |
java.lang.String |
dimension()
Physical dimension of a unit. |
void |
div(Unit unit)
Division of 2 quantities. |
void |
dump(java.lang.String title)
Dump the details. |
java.lang.StringBuffer |
edit(java.lang.StringBuffer buf)
Edition of the quantity. |
java.lang.StringBuffer |
editDimension(java.lang.StringBuffer buf)
Edit the physical dimension of a unit. |
java.lang.String |
editedValue()
String represntation of the value. |
java.lang.StringBuffer |
editValue(java.lang.StringBuffer buf)
Edit the value in a StringBuffer |
boolean |
equals(java.lang.Object o)
Check whether 2 quantities are equal. |
java.lang.String |
explainUnit()
Give a full explanation of the unit. |
static java.lang.String |
explainUnit(java.lang.String text)
Explain the unit given in argument. |
static Converter |
getConverter(java.lang.String source_unit,
java.lang.String target_unit)
Retrieve a special converter. |
long |
getSIdim()
Get a representation of the unit dimension. |
java.lang.String |
getSIunit()
Get the unit expressed in terms of SI units. |
double |
getSIvalue()
Get the value converted to SI system |
java.lang.String |
getUnit()
Get the unit symbol (identical to unit.symbol) |
double |
getValue()
Get the value of a unit (identical to unit.value) |
boolean |
isCompatibleWith(Unit unit)
Checks whether 2 units are compatible. |
boolean |
isUnitless()
Checks whether a number has no unit (is unitless) |
void |
log()
Convert a value to a decimal log scale |
void |
mag()
Convert a value to a mag scale |
void |
minus(Unit unit)
Subtraction of 2 quantities. |
void |
mult(double s)
Multiplication by a Scalar. |
void |
mult(Unit unit)
Multiplication of 2 quantities. |
int |
parse(java.lang.String text,
int offset)
Examine a String for a Unit + Value |
int |
parseUnit(java.lang.String text,
int offset)
Look in the input string for a Unit |
int |
parseValue(java.lang.String text,
int offset)
Look in the input string for a value |
boolean |
parsing(Parsing t)
Interpret a string for a value + Unit. |
void |
plus(Unit unit)
Addition of 2 quantities. |
void |
power(int expo)
Compute the Power of a number/unit |
void |
prod(Unit unit)
Product of 2 quantities. |
static void |
registerConverter(java.lang.String source_unit,
java.lang.String target_unit,
Converter special_converter)
Introduce a special converter. |
void |
set()
Reset a unit to unitless. |
void |
set(java.lang.String text)
Interpret a String to a Unit + Value |
void |
set(Unit u)
Copy a quantity. |
void |
setUnit()
Convert the current number+Unit into a Unit. |
void |
setUnit(java.lang.String text)
Assigns the Unit. |
void |
setValue(double value)
Assign the value. |
void |
setValue(java.lang.String text)
Assign the value. |
void |
sqrt()
Take Square Root of a Value/Unit |
void |
sum(Unit unit)
Sum of 2 quantities. |
static java.util.Enumeration |
symbols()
List all symbols. |
java.lang.String |
toString()
Standard Edition of the Unit. |
java.lang.String |
toStringInSI()
Explain the Unit+Value in terms of SI units (Convert to non-log) |
static long |
unitless()
Get a representation of the unitless dimension |
Methods inherited from class java.lang.Object |
---|
getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public double value
public java.lang.String symbol
Constructor Detail |
---|
public Unit()
public Unit(Unit u)
u
- the Unit to clonepublic Unit(java.lang.String symbol) throws java.text.ParseException
symbol
- a text to interpret, e.g. 20km/s, or 2(75km/s/Mpc)
java.text.ParseException
- when the argument contains non-interpretable
textMethod Detail |
---|
public static final java.util.Enumeration symbols()
checkSymbol(java.lang.String)
to verify one symbol.
public static Unit addSymbol(java.lang.String symbol, java.lang.String equiv, java.lang.String explain) throws java.text.ParseException
symbol
- the text of the symbol -- made of letters only,
or letters preceded by a backslash, or surrounded by quotesequiv
- the definition of the symbol in terms of existing symbolsexplain
- the text to be used for explanation
(should be terminated by a blank)
java.text.ParseException
- when equiv can't be interpreted.public static Unit addSymbol(java.lang.String symbol, java.lang.String equiv) throws java.text.ParseException
symbol
- the text of the symbol -- made of letters only,
or letters preceded by a backslash, or surrounded by quotesequiv
- the definition of the symbol in terms of existing symbols
java.text.ParseException
- when equiv can't be interpreted.public static void registerConverter(java.lang.String source_unit, java.lang.String target_unit, Converter special_converter)
Converter
class
are automatically registered (no need to register them).
Once registered, the converter is used by convert(cds.astro.Unit, cds.astro.Unit)
,
convertFrom(cds.astro.Unit)
and convertTo(cds.astro.Unit)
preferably to
the standard conversion rules.
source_unit
- symbol of source unit, e.g. magtarget_unit
- symbol of target unit, e.g. Jyspecial_converter
- a Converter
objectpublic static Converter getConverter(java.lang.String source_unit, java.lang.String target_unit)
registerConverter(java.lang.String, java.lang.String, cds.astro.Converter)
)
source_unit
- symbol of source unit, e.g. "d:m:s"target_unit
- symbol of target unit, e.g. "h:m:s"
Converter
object.
Object which know how to convert source_unit into
target_unitpublic static boolean checkSymbol(java.lang.String symbol)
symbols()
to get all symbols.
symbol
- the text of the symbol -- made of letters only,
or letters preceded by a backslash, or surrounded by quotes
public double getValue()
public double getSIvalue()
public long getSIdim()
public static long unitless()
public java.lang.String getUnit()
public java.lang.String getSIunit()
public final void log() throws java.lang.ArithmeticException
java.lang.ArithmeticException
- when the unit contains already logspublic final void mag() throws java.lang.ArithmeticException
java.lang.ArithmeticException
- when the unit contains already logspublic final void dexp() throws java.lang.ArithmeticException
java.lang.ArithmeticException
- when the unit is not a logpublic final void power(int expo) throws java.lang.ArithmeticException
expo
- Power value
java.lang.ArithmeticException
- when the power is too largepublic final void sqrt() throws java.lang.ArithmeticException
java.lang.ArithmeticException
- when the unit is not a log, or not-evenpublic final boolean isUnitless()
public final boolean isCompatibleWith(Unit unit)
unit
- another Unit, to be verified.public final boolean equals(java.lang.Object o)
equals
in class java.lang.Object
o
- any object
public static void convert(Unit source_unit, Unit target_unit) throws java.lang.ArithmeticException
registerConverter(java.lang.String, java.lang.String, cds.astro.Converter)
)
source_unit
- the value+unit to convert.
This value is unchanged.target_unit
- the resulting value+unit.
On return, its value part is modified.
java.lang.ArithmeticException
- when source_unit is not compatible with
target_unitpublic static void convertUnit(Unit source_unit, Unit target_unit) throws java.lang.ArithmeticException
registerConverter(java.lang.String, java.lang.String, cds.astro.Converter)
are not used.
source_unit
- the value+unit to convert.
This value is unchanged.target_unit
- the resulting value+unit.
On return, the value part is modified.
java.lang.ArithmeticException
- when source_unit is not compatible with
target_unitpublic void convertFrom(Unit source_unit) throws java.lang.ArithmeticException
source_unit
- the value+unit to convert.
This value is unchanged.
java.lang.ArithmeticException
- when source_unit is not compatible with
the unit of this.public void convertTo(Unit unit) throws java.lang.ArithmeticException
unit
- the target unit (unchanged)
On return, this contains the value in another unit.
java.lang.ArithmeticException
- when the units are not compatiblepublic final void plus(Unit unit) throws java.lang.ArithmeticException
unit
- 2nd Unit, to add.
java.lang.ArithmeticException
- when the units are not compatiblepublic final void minus(Unit unit) throws java.lang.ArithmeticException
unit
- 2nd Unit to be subtracted
java.lang.ArithmeticException
- when the units are not compatiblepublic final void sum(Unit unit) throws java.lang.ArithmeticException
unit
- 2nd Unit
java.lang.ArithmeticException
- when the units are not compatiblepublic final void mult(double s) throws java.lang.ArithmeticException
s
- Scalar value for the multiplication
java.lang.ArithmeticException
- when the units can't be combinedpublic final void mult(Unit unit) throws java.lang.ArithmeticException
unit
- 2nd Unit
java.lang.ArithmeticException
- when the units can't be combinedpublic final void div(Unit unit) throws java.lang.ArithmeticException
unit
- 2nd Unit
java.lang.ArithmeticException
- when the units can't be combinedpublic final void prod(Unit unit) throws java.lang.ArithmeticException
unit
- 2nd Unit
java.lang.ArithmeticException
- when the units are not compatiblepublic final void set()
public final void set(Unit u)
u
- the Unit to clonepublic boolean parsing(Parsing t)
t
- text to interpret. For instance public void set(java.lang.String text) throws java.text.ParseException
text
- text to interpret. For instance java.text.ParseException
- when the argument contains non-interpretable
textpublic int parse(java.lang.String text, int offset)
text
- text to interpret; for instance offset
- where to start in the text string.
public int parseUnit(java.lang.String text, int offset)
text
- to be interpreted as a Unit.
The value part is always set to NaN.offset
- where to start in the text string.
public int parseValue(java.lang.String text, int offset)
text
- value (set to NaN when not correct), which can use the
exponential notation like 10+8 to express
108offset
- where to start in the text string.
public void setUnit()
public void setUnit(java.lang.String text) throws java.text.ParseException
text
- to be interpreted as a Unit.
The value part is always set to NaN.
java.text.ParseException
- when the argument text which does not
represent a numberpublic void setValue(java.lang.String text) throws java.text.ParseException
text
- value (set to NaN when not correct), which can use the
exponential notation like 10+8 to express
108
java.text.ParseException
- when the argument text which does not
represent a numberpublic void setValue(double value)
value
- value to setpublic void dump(java.lang.String title)
title
- title of dumppublic final java.lang.StringBuffer editDimension(java.lang.StringBuffer buf)
buf
- Buffer where the result is appended. A question mark (?)
indicates problems.
public final java.lang.String dimension()
public final java.lang.StringBuffer editValue(java.lang.StringBuffer buf)
buf
- Buffer where the result is appended
public final java.lang.StringBuffer edit(java.lang.StringBuffer buf)
buf
- Buffer where the result is appended
public static final java.lang.String explainUnit(java.lang.String text)
text
- the text containing the units to explain
public final java.lang.String explainUnit()
public final java.lang.String toStringInSI()
public java.lang.String toString()
edit(java.lang.StringBuffer)
toString
in class java.lang.Object
editedValue()
public java.lang.String editedValue()
toString()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |