Package adams.core
Class Index
- java.lang.Object
-
- adams.core.Index
-
- All Implemented Interfaces:
CloneHandler<Index>
,CustomDisplayStringProvider
,ExampleProvider
,HelpProvider
,Serializable
,Comparable<Index>
- Direct Known Subclasses:
AbstractDataBackedIndex
public class Index extends Object implements Serializable, CustomDisplayStringProvider, Comparable<Index>, ExampleProvider, HelpProvider, CloneHandler<Index>
A simple class that translates human-readable 1-based index strings (including "first", "second", "third", "last_2", "last_1" and "last") into integer indices. Numeric indices can be forced by using a "#" at start (eg "#12").- Author:
- fracpete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static String
FIRST
the special index for "first".static String
LAST
the special index for "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_Index
the underlying index.protected Integer
m_IntIndex
the parsed integer index.protected int
m_Max
the maximum number for the 1-based index.protected String
m_Raw
the uncleaned index.static String
NUMERIC_START
the indicator for numeric indices.static String
SECOND
the special string "second".static String
THIRD
the special string "third".
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected String
clean(String s)
Cleanses the given string.int
compareTo(Index o)
Compares this index with the specified index for order.boolean
equals(Object obj)
Indicates whether some other object is "equal to" this one.Index
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.String
getIndex()
Returns the currently set index.int
getIntIndex()
Returns the integer representation of the index.int
getMax()
Returns the maximum.int
hashCode()
Hashcode so can be used as hashtable key.boolean
hasIndex()
Checks whether a valid index has been supplied.protected void
initialize()
For initializing the object.boolean
isEmpty()
Returns whether the index is empty.protected boolean
isPlaceholder(String s)
Checks whether the string represents a placeholder.protected int
parse(String s, int max)
Parses the string and checks it against the maximum.protected int
parsePlaceholder(String s, int max)
Parses the placeholder.protected void
reset()
Resets the parsed data.void
setIndex(String value)
Sets the index.void
setMax(int value)
Sets the maximum (1-max will be allowed).String
toDisplay()
Returns the custom display string.String
toString()
Returns a string representation of the index object.
-
-
-
Field Detail
-
FIRST
public static final String FIRST
the special index for "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 index for "last".- 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 index.
-
m_Index
protected String m_Index
the underlying index.
-
m_IntIndex
protected Integer m_IntIndex
the parsed integer index.
-
m_Max
protected int m_Max
the maximum number for the 1-based index.
-
-
Constructor Detail
-
Index
public Index()
Initializes with no index.
-
Index
public Index(String index)
Initializes with the given index, but no maximum.- Parameters:
index
- the index to use
-
Index
public Index(String index, int max)
Initializes with the given index and maximum.- Parameters:
index
- the index 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()
For initializing the object.
-
reset
protected void reset()
Resets the parsed data.
-
setIndex
public void setIndex(String value)
Sets the index.- Parameters:
value
- the index to use
-
getIndex
public String getIndex()
Returns the currently set index.- Returns:
- the index in use
-
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
-
hasIndex
public boolean hasIndex()
Checks whether a valid index has been supplied.- Returns:
- true if a valid index is available
-
isEmpty
public boolean isEmpty()
Returns whether the index is empty.- Returns:
- true if empty
-
getClone
public Index getClone()
Returns a clone of the object.- Specified by:
getClone
in interfaceCloneHandler<Index>
- Returns:
- the clone
-
isPlaceholder
protected boolean isPlaceholder(String s)
Checks whether the string represents a placeholder.- Parameters:
s
- the string to check- Returns:
- true if a placeholder
-
clean
protected String clean(String s)
Cleanses the given string. Only allows "first", "last" and numbers.- Parameters:
s
- the string to clean- Returns:
- the cleansed string, "" if invalid one provided
-
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 string and checks it against the maximum.- Parameters:
s
- the string to parsemax
- the maximum to allow- Returns:
- the parsed value, -1 if invalid
-
getIntIndex
public int getIntIndex()
Returns the integer representation of the index.- Returns:
- the integer index, -1 if not possible
-
compareTo
public int compareTo(Index o)
Compares this index with the specified index for order. Returns a negative integer, zero, or a positive integer as this index is less than, equal to, or greater than the specified index.- Specified by:
compareTo
in interfaceComparable<Index>
- 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 index string.
-
toString
public String toString()
Returns a string representation of the index object.
-
toDisplay
public String toDisplay()
Returns the custom display string.- Specified by:
toDisplay
in interfaceCustomDisplayStringProvider
- 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
-
-