/** * Extracts the row number from a box position reference in String form ("A01") * * @param position * the position reference * @return the row number, between 0 and 25 inclusive * @throws IllegalArgumentException * if this is not a valid String reference to a box position */ public static int getRowNumber(String position) { validateReference(position); return fromRowChar(position.charAt(0)); }
private void validate(String position) { if (!position.matches("[A-Z][0-9][0-9]")) throw new IllegalArgumentException("Position " + position + " does not match [A-Z][0-9][0-9]"); if (BoxUtils.fromRowChar(position.charAt(0)) >= getSize().getRows()) throw new IndexOutOfBoundsException("Row letter too large!" + position); int col = BoxUtils.tryParseInt(position.substring(1, 3)); if (col <= 0 || col > getSize().getColumns()) throw new IndexOutOfBoundsException("Column value too large!"); }
@Override public boolean isValidPosition(String position) { if (!position.matches("[A-Z][0-9][0-9]")) return false; if (BoxUtils.fromRowChar(position.charAt(0)) >= getSize().getRows()) return false; int col = BoxUtils.tryParseInt(position.substring(1, 3)); if (col < 0 || col > getSize().getColumns()) return false; // columns are zero-indexed in database return true; }