No need to specify dimensions for AbstractKeyboardBuilder

Bug: 13017434
Change-Id: I1cce6d9f072dff8ce2a53b8089f09105ba812a2b
main
Tadashi G. Takaoka 2014-03-14 18:37:40 +09:00
parent 746f94c671
commit cd15cfdaab
6 changed files with 22 additions and 25 deletions

View File

@ -35,7 +35,7 @@ public final class Qwerty extends LayoutBase {
@Override
ExpectedKey[][] getCommonAlphabetLayout(final boolean isPhone) { return ALPHABET_COMMON; }
private static final ExpectedKey[][] ALPHABET_COMMON = new ExpectedKeyboardBuilder(10, 9, 7, 3)
private static final ExpectedKey[][] ALPHABET_COMMON = new ExpectedKeyboardBuilder()
.setLabelsOfRow(1, "q", "w", "e", "r", "t", "y", "u", "i", "o", "p")
.setMoreKeysOf("q", "1")
.setMoreKeysOf("w", "2")

View File

@ -114,7 +114,7 @@ public class Symbols extends AbstractLayoutBase {
public static ExpectedKey[] SINGLE_ANGLE_QUOTES_RL = { SAQUOTE_RIGHT, SAQUOTE_LEFT };
// Common symbols keyboard layout.
private static final ExpectedKey[][] SYMBOLS_COMMON = new ExpectedKeyboardBuilder(10, 9, 7, 5)
private static final ExpectedKey[][] SYMBOLS_COMMON = new ExpectedKeyboardBuilder()
.setKeysOfRow(1,
// U+00B9: "¹" SUPERSCRIPT ONE
// U+00BD: "½" VULGAR FRACTION ONE HALF

View File

@ -70,8 +70,7 @@ public class SymbolsShifted extends AbstractLayoutBase {
};
// Common symbols shifted keyboard layout.
private static final ExpectedKey[][] SYMBOLS_SHIFTED_COMMON = new ExpectedKeyboardBuilder(
10, 1 /* other_currencies */+ 5, 7, 5)
private static final ExpectedKey[][] SYMBOLS_SHIFTED_COMMON = new ExpectedKeyboardBuilder()
.setKeysOfRow(1,
key("~"),
// U+0060: "`" GRAVE ACCENT

View File

@ -29,7 +29,7 @@ import java.util.Arrays;
*/
abstract class AbstractKeyboardBuilder<E> {
// A building array of rows.
private final E[][] mRows;
private E[][] mRows;
// Returns an instance of default element.
abstract E defaultElement();
@ -42,12 +42,8 @@ abstract class AbstractKeyboardBuilder<E> {
* Construct a builder filled with the default element.
* @param dimensions the integer array of each row's size.
*/
AbstractKeyboardBuilder(final int ... dimensions) {
mRows = newArrayOfArray(dimensions.length);
for (int rowIndex = 0; rowIndex < dimensions.length; rowIndex++) {
mRows[rowIndex] = newArray(dimensions[rowIndex]);
Arrays.fill(mRows[rowIndex], defaultElement());
}
AbstractKeyboardBuilder() {
mRows = newArrayOfArray(0);
}
/**
@ -102,10 +98,13 @@ abstract class AbstractKeyboardBuilder<E> {
*/
void setRowAt(final int row, final E[] elements) {
final int rowIndex = row - 1;
if (rowIndex < 0 || rowIndex >= mRows.length) {
if (rowIndex < 0) {
throw new RuntimeException("Illegal row number: " + row);
}
mRows[rowIndex] = elements;
final E[][] newRows = (rowIndex < mRows.length) ? mRows
: Arrays.copyOf(mRows, rowIndex + 1);
newRows[rowIndex] = elements;
mRows = newRows;
}
/**
@ -120,8 +119,11 @@ abstract class AbstractKeyboardBuilder<E> {
void setElementAt(final int row, final int column, final E element, final boolean insert) {
final E[] elements = getRowAt(row);
final int columnIndex = column - 1;
if (columnIndex < 0) {
throw new RuntimeException("Illegal column number: " + column);
}
if (insert) {
if (columnIndex < 0 || columnIndex >= elements.length + 1) {
if (columnIndex >= elements.length + 1) {
throw new RuntimeException("Illegal column number: " + column);
}
final E[] newElements = Arrays.copyOf(elements, elements.length + 1);
@ -134,9 +136,9 @@ abstract class AbstractKeyboardBuilder<E> {
setRowAt(row, newElements);
return;
}
if (columnIndex < 0 || columnIndex >= elements.length) {
throw new RuntimeException("Illegal column number: " + column);
}
elements[columnIndex] = element;
final E[] newElements = (columnIndex < elements.length) ? elements
: Arrays.copyOf(elements, columnIndex + 1);
newElements[columnIndex] = element;
setRowAt(row, newElements);
}
}

View File

@ -85,7 +85,7 @@ public final class ActualKeyboardBuilder extends AbstractKeyboardBuilder<Key> {
for (int rowIndex = 0; rowIndex < dimensions.length; rowIndex++) {
dimensions[rowIndex] = rows.get(rowIndex).size();
}
final ActualKeyboardBuilder builder = new ActualKeyboardBuilder(dimensions);
final ActualKeyboardBuilder builder = new ActualKeyboardBuilder();
for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) {
final int row = rowIndex + 1;
@ -95,10 +95,6 @@ public final class ActualKeyboardBuilder extends AbstractKeyboardBuilder<Key> {
return builder.build();
}
private ActualKeyboardBuilder(final int ... dimensions) {
super(dimensions);
}
@Override
Key defaultElement() { return null; }

View File

@ -23,8 +23,8 @@ import java.util.Locale;
* This class builds an expected keyboard for unit test.
*/
public final class ExpectedKeyboardBuilder extends AbstractKeyboardBuilder<ExpectedKey> {
public ExpectedKeyboardBuilder(final int ... dimensions) {
super(dimensions);
public ExpectedKeyboardBuilder() {
super();
}
public ExpectedKeyboardBuilder(final ExpectedKey[][] rows) {