LineType.java
Download filepackage de.joshuagleitze.tilinggame;
/**
* Represents all possible types of lines that can be present on tiles.
*
* <p>
* The type contains values for three colors ({@link #RED}, {@link #GREEN} and
* {@link #YELLOW}) and one value that represents the absence of a line
* ({@link #NONE}).
*
* <p>
* Each value has an <em>abbreviation</em> that can be obtained using the
* {@link LineType#getAbbreviation()} method.
*
* @author ARE
* @author Joshua Gleitze
* @version 1.1
*/
public enum LineType {
/** Non-existent line. */
NONE('-'),
/** Line with color red. */
RED('R'),
/** Line with color green. */
GREEN('G'),
/** Line with color yellow. */
YELLOW('Y');
/**
* The abbreviation letter for this line type.
*/
private final char abbreviation;
/**
* Caches the array of line types that are colors.
*/
private static final LineType[] COLOR_LINE_TYPES = LineType.getColorLineTypesArray();
LineType(final char abbreviation) {
this.abbreviation = abbreviation;
}
/**
* Returns the abbreviation of the line type.
*
* <p>
* Each color is represented by the first character of its
* {@linkplain #name() name} ({@linkplain #RED R}, {@linkplain #GREEN G} or
* {@linkplain #YELLOW Y}), the line type {@linkplain LineType#NONE NONE} is
* represented by an ASCII minus sign '-' ('\u002D').
*
* @return the abbreviation
*/
public char getAbbreviation() {
return this.abbreviation;
}
/**
* Returns whether this is a color.
*
* @return {@code true} if this is a color, {@code false} if this is
* {@link #NONE}
*/
public boolean isColor() {
return this != NONE;
}
/**
* Creates the array of line types that are colors.
*
* @return All line types for which {@link #isColor()} returns {@code true}.
*/
private static LineType[] getColorLineTypesArray() {
final LineType[] colorLineTypes = new LineType[values().length - 1];
int colorIndex = 0;
for (final LineType type : values()) {
if (type == LineType.NONE) {
continue;
}
colorLineTypes[colorIndex++] = type;
}
return colorLineTypes;
}
/**
* Queries the line types that are colors.
*
* @return All line types for which {@link #isColor()} returns {@code true}.
*/
static LineType[] colors() {
return COLOR_LINE_TYPES;
}
}