Package adams.core
Class Range
- java.lang.Object
-
- adams.core.Range
-
- All Implemented Interfaces:
CloneHandler<Range>
,CustomDisplayStringProvider
,ExampleProvider
,HelpProvider
,Serializable
,Comparable<Range>
- Direct Known Subclasses:
AbstractDataBackedRange
public class Range extends Object implements Serializable, CustomDisplayStringProvider, Comparable<Range>, ExampleProvider, HelpProvider, CloneHandler<Range>
A class for managing a range of 1-based indices, e.g., 1-5, 3,7,9 or 1-7,9 (including "first", "second", "third", "last_2", "last_1" and "last"). A range can be inverted by surrounding it with "inv(...)". Numeric indices can be forced by using a "#" at start (eg "#12").- Version:
- $Revision$
- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Range.SubRange
Represents a sub-range, either a single number of from-to.
-
Field Summary
Fields Modifier and Type Field Description static String
ALL
"first-last" constant.static String
FIRST
the special string "first".static String
INV_END
the end string for inversion.static String
INV_START
the start string for inversion.static String
LAST
the special string "last".static String
LAST_1
the special string "last_1" (2nd to last).static String
LAST_2
the special string "last_2" (3rd to last).protected String
m_ActualRange
the actual range, without the inversion.protected Boolean
m_Inverted
whether the range is inverted.protected int
m_Max
the maximum for the 1-based range.protected String
m_Range
the range string.protected String
m_Raw
the uncleaned range string.protected List<Range.SubRange>
m_SubRanges
the range parts.static String
NUMERIC_START
the indicator for numeric indices.static String
RANGE
the special string "-".static String
SECOND
the special string "second".static String
SEPARATOR
the special string ",".static String
THIRD
the special string "third".
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
canReplaceInvalidChars()
Returns whether invalid characters should get removed.protected String
clean(String s)
Cleanses the given string.int
compareTo(Range o)
Compares this subrange with the specified subrange for order.boolean
equals(Object obj)
Indicates whether some other object is "equal to" this one.protected String
getActualRange()
Returns the actual range, without inversion.Range
getClone()
Returns a clone of the object.String
getExample()
Returns the example.String
getHelpDescription()
Returns a long help description, e.g., used in tiptexts.String
getHelpIcon()
Returns the name of a help icon, e.g., used for buttons.String
getHelpTitle()
Returns a short title for the help, e.g., used for buttons.String
getHelpURL()
Returns a URL with additional information.int[]
getIntIndices()
Returns the integer indices.int[][]
getIntSegments()
Turns the range into a list of from-to segements.int
getMax()
Returns the maximum.String
getRange()
Returns the currently set range.protected List<Range.SubRange>
getSubRanges()
Returns the sub-ranges, initializes them if necessary.int
hashCode()
Hashcode so can be used as hashtable key.boolean
hasRange()
Checks whether a valid range has been supplied.protected void
initialize()
Initializes the object.boolean
isAllRange()
Returns whether the range encompasses all.boolean
isEmpty()
Returns whether the range is empty.boolean
isInRange(int index)
Checks whether the provided 0-based index is within the range.boolean
isInverted()
Checks whether the range is inverted.protected boolean
isPlaceholder(String s)
Checks whether the string represents a placeholder.static boolean
isValid(String s, int max)
Returns whether the range string is valid.protected int
parse(String s)
Parses the 1-based index, 'first' and 'last' are accepted as well.protected List<Range.SubRange>
parse(StringBuilder errors)
Parses the string and generates the sub-ranges.protected int
parse(String s, int max)
Parses the 1-based index, 'first' and 'last' are accepted as well.protected int
parsePlaceholder(String s, int max)
Parses the placeholder.protected void
reset()
Resets the object.void
setIndices(int[] indices)
Sets the selected indices.void
setIndices(Integer[] indices)
Sets the selected indices.void
setInverted(boolean value)
Sets whether the range is inverted or not.void
setMax(int value)
Sets the maximum (1-max will be allowed).void
setRange(String value)
Sets the range.protected String[]
splitList(String s)
Attempts to split a list into the parts resembling it.protected String[]
splitRange(String s)
Attempts to split a range into the parts resembling it.String
toDisplay()
Returns the custom display string.String
toExplicitRange()
Returns the explicit range, i.e., just comma-separated (1-based) indices.static Range
toRange(int[] indices)
Turns the integer indices into a range object.String
toString()
Returns a string representation of the range.
-
-
-
Field Detail
-
RANGE
public static final String RANGE
the special string "-".- See Also:
- Constant Field Values
-
SEPARATOR
public static final String SEPARATOR
the special string ",".- See Also:
- Constant Field Values
-
FIRST
public static final String FIRST
the special string "first".- See Also:
- Constant Field Values
-
SECOND
public static final String SECOND
the special string "second".- See Also:
- Constant Field Values
-
THIRD
public static final String THIRD
the special string "third".- See Also:
- Constant Field Values
-
LAST_1
public static final String LAST_1
the special string "last_1" (2nd to last).- See Also:
- Constant Field Values
-
LAST_2
public static final String LAST_2
the special string "last_2" (3rd to last).- See Also:
- Constant Field Values
-
LAST
public static final String LAST
the special string "last".- See Also:
- Constant Field Values
-
INV_START
public static final String INV_START
the start string for inversion.- See Also:
- Constant Field Values
-
INV_END
public static final String INV_END
the end string for inversion.- See Also:
- Constant Field Values
-
ALL
public static final String ALL
"first-last" constant.- See Also:
- Constant Field Values
-
NUMERIC_START
public static final String NUMERIC_START
the indicator for numeric indices.- See Also:
- Constant Field Values
-
m_Raw
protected String m_Raw
the uncleaned range string.
-
m_Range
protected String m_Range
the range string.
-
m_ActualRange
protected String m_ActualRange
the actual range, without the inversion.
-
m_Max
protected int m_Max
the maximum for the 1-based range.
-
m_Inverted
protected Boolean m_Inverted
whether the range is inverted.
-
m_SubRanges
protected List<Range.SubRange> m_SubRanges
the range parts.
-
-
Constructor Detail
-
Range
public Range()
Initializes with no range.
-
Range
public Range(String range)
Initializes with the given range, but no maximum.- Parameters:
range
- the range to use
-
Range
public Range(String range, int max)
Initializes with the given range and maximum.- Parameters:
range
- the range to usemax
- the maximum of the 1-based index (e.g., use "10" to allow "1-10" or -1 for uninitialized)
-
-
Method Detail
-
initialize
protected void initialize()
Initializes the object.
-
reset
protected void reset()
Resets the object.
-
setRange
public void setRange(String value)
Sets the range.- Parameters:
value
- the range to use
-
setIndices
public void setIndices(Integer[] indices)
Sets the selected indices. Generates a range string out of the array.- Parameters:
indices
- the indices (0-based)
-
setIndices
public void setIndices(int[] indices)
Sets the selected indices. Generates a range string out of the array.- Parameters:
indices
- the indices (0-based)
-
isInverted
public boolean isInverted()
Checks whether the range is inverted.- Returns:
- true if inverted
-
setInverted
public void setInverted(boolean value)
Sets whether the range is inverted or not.- Parameters:
value
- if true then the range is inverted
-
getRange
public String getRange()
Returns the currently set range.- Returns:
- the range in use
-
getActualRange
protected String getActualRange()
Returns the actual range, without inversion.- Returns:
- the actual range
-
setMax
public void setMax(int value)
Sets the maximum (1-max will be allowed).- Parameters:
value
- the maximum for the 1-based index
-
getMax
public int getMax()
Returns the maximum.- Returns:
- the maximum for the 1-based index
-
hasRange
public boolean hasRange()
Checks whether a valid range has been supplied.- Returns:
- true if a valid range is available
-
canReplaceInvalidChars
protected boolean canReplaceInvalidChars()
Returns whether invalid characters should get removed.
Default implementation always returns true.- Returns:
- true if to replace invalid chars
-
splitList
protected String[] splitList(String s)
Attempts to split a list into the parts resembling it.- Parameters:
s
- the string to split- Returns:
- the parts (single array element if no list)
-
splitRange
protected String[] splitRange(String s)
Attempts to split a range into the parts resembling it.- Parameters:
s
- the string to split- Returns:
- the parts (single array element if no range)
-
clean
protected String clean(String s)
Cleanses the given string.- Parameters:
s
- the string to clean- Returns:
- the cleansed string, "" if invalid one provided
-
isPlaceholder
protected boolean isPlaceholder(String s)
Checks whether the string represents a placeholder.- Parameters:
s
- the string to check- Returns:
- true if a placeholder
-
parsePlaceholder
protected int parsePlaceholder(String s, int max)
Parses the placeholder.- Parameters:
s
- the placeholder to parsemax
- the max to use- Returns:
- the placeholder's integer equivalent, -1 if not a placeholder
-
parse
protected int parse(String s, int max)
Parses the 1-based index, 'first' and 'last' are accepted as well.- Parameters:
s
- the string to parsemax
- the maximum value to use- Returns:
- the 0-based index
-
parse
protected int parse(String s)
Parses the 1-based index, 'first' and 'last' are accepted as well.- Parameters:
s
- the string to parse- Returns:
- the 0-based index
-
parse
protected List<Range.SubRange> parse(StringBuilder errors)
Parses the string and generates the sub-ranges.- Parameters:
errors
- for adding errors to it- Returns:
- the parsed sub-ranges
-
getSubRanges
protected List<Range.SubRange> getSubRanges()
Returns the sub-ranges, initializes them if necessary. NB: Does not check for inverted flag!- Returns:
- the sub-ranges
-
isInRange
public boolean isInRange(int index)
Checks whether the provided 0-based index is within the range.- Parameters:
index
- the index to check- Returns:
- true if in range
-
getIntIndices
public int[] getIntIndices()
Returns the integer indices. Gets always generated on-the-fly!- Returns:
- the indices, 0-length array if not possible
-
getIntSegments
public int[][] getIntSegments()
Turns the range into a list of from-to segements. The indices are 0-based. In case a subrange consists only of a single index, the second one is the same. NB: Does not check for inverted flag!- Returns:
- the segments
-
isAllRange
public boolean isAllRange()
Returns whether the range encompasses all.
-
isEmpty
public boolean isEmpty()
Returns whether the range is empty.- Returns:
- true if empty
-
compareTo
public int compareTo(Range o)
Compares this subrange with the specified subrange for order. Returns a negative integer, zero, or a positive integer as this subrange is less than, equal to, or greater than the specified subrange. Uses the "from" as point of comparison and if those are equal, then the "to" (if available).- Specified by:
compareTo
in interfaceComparable<Range>
- Parameters:
o
- the subrange to be compared.- Returns:
- a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
-
equals
public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
-
hashCode
public int hashCode()
Hashcode so can be used as hashtable key. Returns the hashcode of the range string.
-
getClone
public Range getClone()
Returns a clone of the object.- Specified by:
getClone
in interfaceCloneHandler<Range>
- Returns:
- the clone
-
toString
public String toString()
Returns a string representation of the range.
-
toDisplay
public String toDisplay()
Returns the custom display string.- Specified by:
toDisplay
in interfaceCustomDisplayStringProvider
- Returns:
- the string
-
toExplicitRange
public String toExplicitRange()
Returns the explicit range, i.e., just comma-separated (1-based) indices.- Returns:
- the string
-
getExample
public String getExample()
Returns the example.- Specified by:
getExample
in interfaceExampleProvider
- Returns:
- the example
-
getHelpURL
public String getHelpURL()
Returns a URL with additional information.- Specified by:
getHelpURL
in interfaceHelpProvider
- Returns:
- the URL, null if not available
-
getHelpDescription
public String getHelpDescription()
Returns a long help description, e.g., used in tiptexts.- Specified by:
getHelpDescription
in interfaceHelpProvider
- Returns:
- the help text, null if not available
-
getHelpTitle
public String getHelpTitle()
Returns a short title for the help, e.g., used for buttons.- Specified by:
getHelpTitle
in interfaceHelpProvider
- Returns:
- the short title, null if not available
-
getHelpIcon
public String getHelpIcon()
Returns the name of a help icon, e.g., used for buttons.- Specified by:
getHelpIcon
in interfaceHelpProvider
- Returns:
- the icon name, null if not available
-
toRange
public static Range toRange(int[] indices)
Turns the integer indices into a range object.- Parameters:
indices
- the 0-based indices- Returns:
- the generated range
-
isValid
public static boolean isValid(String s, int max)
Returns whether the range string is valid.- Parameters:
s
- the range to checkmax
- the maximum- Returns:
- true if a valid range string
-
-