Class VolumeKeepPriority
- java.lang.Object
-
- org.daisy.dotify.formatter.impl.search.VolumeKeepPriority
-
- All Implemented Interfaces:
Comparable<VolumeKeepPriority>
public final class VolumeKeepPriority extends Object implements Comparable<VolumeKeepPriority>
Provides a simple data type for volume keep priority, where 1 represents the highest priority and 9 the lowest. A high priority means that we do more effort to avoid splitting a volume within the area the priority is declared on.
This class is modeled on Java's Optional and provides similar capabilities. As with Optional, setting a field with this type to null is strongly discouraged. Instead, use
empty()
to create an empty instance.The primary reason for creating a special class is to make the meaning of the number contained in it clearer. But in addition, it makes it possible to enforce internal bounds checks. The caller can therefore rely on this fact when getting the value.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareTo(VolumeKeepPriority o)
Compares this priority with the specified priority.static VolumeKeepPriority
empty()
Returns an instance with no priority (lowest).boolean
equals(Object obj)
int
getValue()
Gets the priority value, if present.int
hashCode()
boolean
hasValue()
Returns true if a priority is set, false otherwise.static Comparator<VolumeKeepPriority>
naturalOrder()
Returns a comparator that imposes the natural ordering onVolumeKeepPriority
objects.static VolumeKeepPriority
of(int value)
Creates an new instance with the specified priority.static VolumeKeepPriority
ofNullable(Number value)
Creates a new instance with the specified priority (may be null).int
orElse(int other)
Gets the priority if set, otherwise returns the supplied value.
-
-
-
Method Detail
-
of
public static VolumeKeepPriority of(int value)
Creates an new instance with the specified priority.- Parameters:
value
- the priority- Returns:
- returns a new instance
-
ofNullable
public static VolumeKeepPriority ofNullable(Number value)
Creates a new instance with the specified priority (may be null).- Parameters:
value
- the priority, or null- Returns:
- returns a new instance
-
empty
public static VolumeKeepPriority empty()
Returns an instance with no priority (lowest).- Returns:
- returns an instance with no priority
-
getValue
public int getValue()
Gets the priority value, if present. The value is in the range [1, 9] where 1 represents the highest priority and 9 the lowest.- Returns:
- returns the priority
- Throws:
NoSuchElementException
- if no priority is set.
-
hasValue
public boolean hasValue()
Returns true if a priority is set, false otherwise. If a priority is set, it can be retrieved usinggetValue()
.- Returns:
- returns true if a priority is available, false otherwise
-
orElse
public int orElse(int other)
Gets the priority if set, otherwise returns the supplied value.- Parameters:
other
- the value to return if not set- Returns:
- returns the priority, or the supplied value
-
compareTo
public int compareTo(VolumeKeepPriority o)
Compares this priority with the specified priority.
Returns a negative integer, zero, or a positive integer as this priority is lower, equal to, or higher than the specified priority. An
absent
priority is considered lower than the lowest priority.Note that this method does not compare the integer values returned by
getValue()
, but rather represents the "natural" ordering. (The fact that a higher priority is represented by a lower integer value may cause some confusion in this regard.)- Specified by:
compareTo
in interfaceComparable<VolumeKeepPriority>
- Parameters:
o
- The other priority- Returns:
- The value
0
if the priority ofthis
is equal too
; a value less than0
if the priority ofthis
is lower than the priority ofo
; and a value greater than0
if the priority ofthis
is greater than the priority ofo
; ifthis
does not have a priority value ando
does, a value greater than0
is returned; ifthis
has a priority value ando
does not, a value less than0
is returned; if neitherthis
noro
has a priority value, the value0
is returned.
-
naturalOrder
public static Comparator<VolumeKeepPriority> naturalOrder()
Returns a comparator that imposes the natural ordering on
VolumeKeepPriority
objects.Objects are ordered according to
compareTo(VolumeKeepPriority)
, from lower to higher priority.- Returns:
- a comparator that imposes the natural ordering on
VolumeKeepPriority
objects.
-
-