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 StringFIRSTthe special index for "first".static StringLASTthe special index for "last".static StringLAST_1the special string "last_1" (2nd to last).static StringLAST_2the special string "last_2" (3rd to last).protected Stringm_Indexthe underlying index.protected Integerm_IntIndexthe parsed integer index.protected intm_Maxthe maximum number for the 1-based index.protected Stringm_Rawthe uncleaned index.static StringNUMERIC_STARTthe indicator for numeric indices.static StringSECONDthe special string "second".static StringTHIRDthe special string "third".
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Stringclean(String s)Cleanses the given string.intcompareTo(Index o)Compares this index with the specified index for order.booleanequals(Object obj)Indicates whether some other object is "equal to" this one.IndexgetClone()Returns a clone of the object.StringgetExample()Returns the example.StringgetHelpDescription()Returns a long help description, e.g., used in tiptexts.StringgetHelpIcon()Returns the name of a help icon, e.g., used for buttons.StringgetHelpTitle()Returns a short title for the help, e.g., used for buttons.StringgetHelpURL()Returns a URL with additional information.StringgetIndex()Returns the currently set index.intgetIntIndex()Returns the integer representation of the index.intgetMax()Returns the maximum.inthashCode()Hashcode so can be used as hashtable key.booleanhasIndex()Checks whether a valid index has been supplied.protected voidinitialize()For initializing the object.booleanisEmpty()Returns whether the index is empty.protected booleanisPlaceholder(String s)Checks whether the string represents a placeholder.protected intparse(String s, int max)Parses the string and checks it against the maximum.protected intparsePlaceholder(String s, int max)Parses the placeholder.protected voidreset()Resets the parsed data.voidsetIndex(String value)Sets the index.voidsetMax(int value)Sets the maximum (1-max will be allowed).StringtoDisplay()Returns the custom display string.StringtoString()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:
getClonein 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:
compareToin 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:
toDisplayin interfaceCustomDisplayStringProvider- Returns:
- the string
-
getExample
public String getExample()
Returns the example.- Specified by:
getExamplein interfaceExampleProvider- Returns:
- the example
-
getHelpURL
public String getHelpURL()
Returns a URL with additional information.- Specified by:
getHelpURLin interfaceHelpProvider- Returns:
- the URL, null if not available
-
getHelpDescription
public String getHelpDescription()
Returns a long help description, e.g., used in tiptexts.- Specified by:
getHelpDescriptionin 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:
getHelpTitlein 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:
getHelpIconin interfaceHelpProvider- Returns:
- the icon name, null if not available
-
-