Package net.sf.basedb.core
Class Unit
java.lang.Object
net.sf.basedb.core.BasicItem
net.sf.basedb.core.Unit
- All Implemented Interfaces:
AccessControlled
,Identifiable
,Nameable
,Removable
This class is used to represent a specific unit that can be
used with annotations. A unit must be related to the
reference unit of the quantity. See
Quantity.getReferenceUnit()
for examples.- Version:
- 2.9
- Author:
- Nicklas
- See Also:
- Last modified
- $Date: 2017-05-22 14:35:27 +0200 (må, 22 maj 2017) $
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The maximum length of the display symbol that can be stored in the database.static final Item
The type of item represented by this class.Fields inherited from interface net.sf.basedb.core.Nameable
MAX_DESCRIPTION_LENGTH, MAX_NAME_LENGTH
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Add a symbol to this unit.void
changeReferenceFactorAndOffset
(double factor, double offset) Changes the factor and offset used for converting values in this unit into the reference unit.(package private) static void
changeReferenceFactorAndOffset
(SessionControl sc, Session session, UnitData unit, double factor, double offset) (package private) UnitData
findOtherUnit
(Session session, String symbol) Find the unit with a given symbol that has the same quantity as this unit.double
Convert a value from another unit to this unit.double
fromReferenceUnit
(double value) Convert a value from the reference unit to this unit.static Unit
Get aUnit
object when you know the ID.(package private) UnitData
getData()
Get theBasicData
object that holds all data for this item.Get the description for the item.Get the display symbol to use when displaying values with this unit.<T> Formatter<T>
getFormatter
(Formatter<T> parent) Create a formatter for formatting values with this unit.getName()
Get the name of the item.static Unit
Create a newUnit
item.Get the quantity this unit belongs to.getQuery()
Get a query configured to retrieve units.double
Get the factor that is used for converting values in this unit into the reference unit of the quantity.double
Get the offset that is used for converting values in this unit into the reference unit of the quantity.Get the user that flagged this item for removal.Get all symbols that are registered for this unit.getType()
Get the type of item represented by the object.getUnitConverter
(Unit other) Create a unit converter for converting from the reference unit (other) to this unit.Get all:AnnotationType
:s using this unit as default unitAnnotation
:s using this unitboolean
Check if the removed flag is set for this item.boolean
isUsed()
Check if: Annotation types are using this unit as default unit Annotations are using this unit(package private) void
onBeforeCommit
(Transactional.Action action) Set property filters that use this unit to null when the unit is deleted.void
removeSymbol
(String symbol) Remove a symbol from this unit.void
setDescription
(String description) Set the description for the item.void
setDisplaySymbol
(String symbol) Set the display symbol for this unit.void
Set the name of the item.private void
setQuantity
(Quantity quantity) void
setRemoved
(boolean removed) Set the removed flag for this item.void
setSymbols
(Set<String> symbols) Set the symbols that should be registered for this unit.double
toReferenceUnit
(double value) Convert a value from this unit to the reference unit.double
Convert a value from this unit to another unit.Methods inherited from class net.sf.basedb.core.BasicItem
addAnnotatableParents, addUsingItems, addUsingItems, breakCircularReferences, checkBatchAnnotatableUsage, checkPermission, equals, getBasicData, getDbControl, getId, getPermissions, getPluginPermissions, getSessionControl, getVersion, hashCode, hasPermission, initPermissions, isDetached, isInDatabase, onAfterCommit, onAfterInsert, onRollback, setDbControl, setProjectDefaults, toString, validate
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface net.sf.basedb.core.AccessControlled
checkPermission, getPermissions, hasPermission
Methods inherited from interface net.sf.basedb.core.Identifiable
getId, getVersion
-
Field Details
-
TYPE
The type of item represented by this class. -
MAX_SYMBOL_LENGTH
public static final int MAX_SYMBOL_LENGTHThe maximum length of the display symbol that can be stored in the database.
-
-
Constructor Details
-
Unit
Unit(UnitData data) Creates a unit item.- Parameters:
data
- the data
-
-
Method Details
-
getNew
public static Unit getNew(DbControl dc, Quantity quantity, String displaySymbol) throws BaseException Create a newUnit
item. The unit will have default values for the reference factor (1.0) and offset (0.0). UsechangeReferenceFactorAndOffset(double, double)
to change those.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database accessquantity
- The quantity the unit belongs todisplaySymbol
- The symbol used when displaying values with this unit- Returns:
- The new
Unit
item - Throws:
BaseException
- If there is an error
-
getById
public static Unit getById(DbControl dc, int id) throws ItemNotFoundException, PermissionDeniedException, BaseException Get aUnit
object when you know the ID.- Parameters:
dc
- TheDbControl
which will be used for permission checking and database access.id
- The system ID of the item to load.- Returns:
- The
Unit
item - Throws:
ItemNotFoundException
- This exception is thrown if an item with the specified ID is not foundPermissionDeniedException
- This exception is thrown if the logged in user doesn't have read permission to the itemBaseException
- This exception is thrown if there is another error
-
getQuery
Get a query configured to retrieve units.- Returns:
- An
ItemQuery
object
-
getData
UnitData getData()Description copied from class:BasicItem
Get theBasicData
object that holds all data for this item. -
isUsed
Check if:- Annotation types are using this unit as default unit
- Annotations are using this unit
- Overrides:
isUsed
in classBasicItem
- Returns:
- TRUE if this item is used, FALSE otherwise
- Throws:
BaseException
- If not able to tell if item is used or not.- See Also:
-
getUsingItems
Get all:AnnotationType
:s using this unit as default unitAnnotation
:s using this unit
- Overrides:
getUsingItems
in classBasicItem
- Returns:
- A set containing proxies for the items, or an empty set if no items are using this item
- See Also:
-
onBeforeCommit
Set property filters that use this unit to null when the unit is deleted.- Overrides:
onBeforeCommit
in classBasicItem
- Throws:
BaseException
- If there is an error- See Also:
-
getType
Description copied from interface:Identifiable
Get the type of item represented by the object. The returned value is one of the values defined in theItem
enumeration.- Specified by:
getType
in interfaceIdentifiable
- Returns:
- A value indicating the type of item
-
isRemoved
public boolean isRemoved()Description copied from interface:Removable
Check if the removed flag is set for this item. -
setRemoved
Description copied from interface:Removable
Set the removed flag for this item.- Specified by:
setRemoved
in interfaceRemovable
- Parameters:
removed
- TRUE if the item should be flagged as removed, FALSE otherwise- Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.DELETE
permission for setting the flag to TRUE orPermission.WRITE
permission for setting the flag to FALSE
-
getRemovedBy
Description copied from interface:Removable
Get the user that flagged this item for removal.- Specified by:
getRemovedBy
in interfaceRemovable
- Returns:
- A User object, or null if this item has not been flagged
- Throws:
PermissionDeniedException
- If the logged in user doesn't havePermission.READ
permission for the userItemNotFoundException
- If the user that removed this item can't be found
-
getName
Description copied from interface:Nameable
Get the name of the item. -
setName
Description copied from interface:Nameable
Set the name of the item. The name cannot be null and mustn't be longer than the value specified by theNameable.MAX_NAME_LENGTH
constant.- Specified by:
setName
in interfaceNameable
- Parameters:
name
- The new name for the item- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permissionInvalidDataException
- If the name is null or longer than specified by theNameable.MAX_NAME_LENGTH
constant
-
getDescription
Description copied from interface:Nameable
Get the description for the item.- Specified by:
getDescription
in interfaceNameable
- Returns:
- A
String
with a description of the item
-
setDescription
public void setDescription(String description) throws PermissionDeniedException, InvalidDataException Description copied from interface:Nameable
Set the description for the item. The description can be null but mustn't be longer than the value specified by theNameable.MAX_DESCRIPTION_LENGTH
constant.- Specified by:
setDescription
in interfaceNameable
- Parameters:
description
- The new description for the item- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permissionInvalidDataException
- If the description longer than specified by theNameable.MAX_DESCRIPTION_LENGTH
constant
-
getQuantity
Get the quantity this unit belongs to. -
setQuantity
-
getDisplaySymbol
Get the display symbol to use when displaying values with this unit.- Returns:
- A String object
-
setDisplaySymbol
Set the display symbol for this unit. The symbol is also registered as an alias symbol for this unit. The old symbol is NOT removed as an alias. UseremoveSymbol(String)
to do that.- Parameters:
symbol
- The symbol- Throws:
PermissionDeniedException
- If the logged in user doesn't have write permissionInvalidDataException
- If the new value null or longer thanMAX_SYMBOL_LENGTH
-
getSymbols
Get all symbols that are registered for this unit.- Returns:
- All symbols as a set
-
setSymbols
Set the symbols that should be registered for this unit. Note that all units must be unique within a givenQuantity
. Symbols that are not in the given set will be unregistered except for the display symbol.- Parameters:
symbols
- A set with the symbols
-
addSymbol
Add a symbol to this unit.- Parameters:
symbol
- The symbol to add
-
removeSymbol
Remove a symbol from this unit. It is not possible to remove the display symbol.- Parameters:
symbol
- The symbol to remove
-
getReferenceOffset
public double getReferenceOffset()Get the offset that is used for converting values in this unit into the reference unit of the quantity. SeeQuantity.getReferenceUnit()
for a description of how the values are converted.- Returns:
- The reference offset
- See Also:
-
getReferenceFactor
public double getReferenceFactor()Get the factor that is used for converting values in this unit into the reference unit of the quantity. SeeQuantity.getReferenceUnit()
for a description of how the values are converted.- Returns:
- The reference factor
- See Also:
-
changeReferenceFactorAndOffset
public void changeReferenceFactorAndOffset(double factor, double offset) Changes the factor and offset used for converting values in this unit into the reference unit. Calling this method with values that are different from the old ones will result in a re-calculation of all annotation values that has used this unit to make sure that the actual values stays the same.- Parameters:
factor
- The new factoroffset
- The new offset
-
changeReferenceFactorAndOffset
static void changeReferenceFactorAndOffset(SessionControl sc, Session session, UnitData unit, double factor, double offset) -
toReferenceUnit
public double toReferenceUnit(double value) Convert a value from this unit to the reference unit.- Parameters:
value
- The value in this unit- Returns:
- The value converted to the reference unit
-
fromReferenceUnit
public double fromReferenceUnit(double value) Convert a value from the reference unit to this unit.- Parameters:
value
- The reference value- Returns:
- The value converted to this unit
-
toUnit
Convert a value from this unit to another unit. This method is equivalent to callingother.fromReferenceUnit(this.toReferenceUnit(value))
.- Parameters:
value
- The value in this unitother
- The other unit- Returns:
- The value converted to the other unit
-
fromOther
Convert a value from another unit to this unit. This method is equivalent to callingthis.fromReferenceUnit(other.toReferenceUnit(value))
- Parameters:
value
- The value in the other unitother
- The other unit- Returns:
- The value converted to this unit
-
getFormatter
Create a formatter for formatting values with this unit. The returned formatter delegates the formatting of the values to a parent formatter and uses aPrefixSuffixFormatter
to append the unit symbol.- Parameters:
parent
- The parent formatter for formatting the actual values- Returns:
- The new formatter
-
getUnitConverter
Create a unit converter for converting from the reference unit (other) to this unit.- Parameters:
other
- The reference unit- Returns:
- A unit converter
- Since:
- 3.4
-
findOtherUnit
Find the unit with a given symbol that has the same quantity as this unit.- Returns:
- The unit, or null if no unit is found
-