Refactor CoordinateUtils

Change-Id: I607b803273a4d7983e9e25a81b6941a3b59194c6
This commit is contained in:
Tadashi G. Takaoka 2014-10-27 18:02:04 +09:00
parent 5547b49b17
commit f6a6371096

View file

@ -16,7 +16,7 @@
package com.android.inputmethod.latin.utils; package com.android.inputmethod.latin.utils;
import java.util.Arrays; import javax.annotation.Nonnull;
public final class CoordinateUtils { public final class CoordinateUtils {
private static final int INDEX_X = 0; private static final int INDEX_X = 0;
@ -27,32 +27,35 @@ public final class CoordinateUtils {
// This utility class is not publicly instantiable. // This utility class is not publicly instantiable.
} }
@Nonnull
public static int[] newInstance() { public static int[] newInstance() {
return new int[ELEMENT_SIZE]; return new int[ELEMENT_SIZE];
} }
public static int x(final int[] coords) { public static int x(@Nonnull final int[] coords) {
return coords[INDEX_X]; return coords[INDEX_X];
} }
public static int y(final int[] coords) { public static int y(@Nonnull final int[] coords) {
return coords[INDEX_Y]; return coords[INDEX_Y];
} }
public static void set(final int[] coords, final int x, final int y) { public static void set(@Nonnull final int[] coords, final int x, final int y) {
coords[INDEX_X] = x; coords[INDEX_X] = x;
coords[INDEX_Y] = y; coords[INDEX_Y] = y;
} }
public static void copy(final int[] destination, final int[] source) { public static void copy(@Nonnull final int[] destination, @Nonnull final int[] source) {
destination[INDEX_X] = source[INDEX_X]; destination[INDEX_X] = source[INDEX_X];
destination[INDEX_Y] = source[INDEX_Y]; destination[INDEX_Y] = source[INDEX_Y];
} }
@Nonnull
public static int[] newCoordinateArray(final int arraySize) { public static int[] newCoordinateArray(final int arraySize) {
return new int[ELEMENT_SIZE * arraySize]; return new int[ELEMENT_SIZE * arraySize];
} }
@Nonnull
public static int[] newCoordinateArray(final int arraySize, public static int[] newCoordinateArray(final int arraySize,
final int defaultX, final int defaultY) { final int defaultX, final int defaultY) {
final int[] result = new int[ELEMENT_SIZE * arraySize]; final int[] result = new int[ELEMENT_SIZE * arraySize];
@ -62,30 +65,30 @@ public final class CoordinateUtils {
return result; return result;
} }
public static int xFromArray(final int[] coordsArray, final int index) { public static int xFromArray(@Nonnull final int[] coordsArray, final int index) {
return coordsArray[ELEMENT_SIZE * index + INDEX_X]; return coordsArray[ELEMENT_SIZE * index + INDEX_X];
} }
public static int yFromArray(final int[] coordsArray, final int index) { public static int yFromArray(@Nonnull final int[] coordsArray, final int index) {
return coordsArray[ELEMENT_SIZE * index + INDEX_Y]; return coordsArray[ELEMENT_SIZE * index + INDEX_Y];
} }
public static int[] coordinateFromArray(final int[] coordsArray, final int index) { @Nonnull
final int baseIndex = ELEMENT_SIZE * index; public static int[] coordinateFromArray(@Nonnull final int[] coordsArray, final int index) {
return Arrays.copyOfRange(coordsArray, baseIndex, baseIndex + ELEMENT_SIZE); final int[] coords = newInstance();
set(coords, xFromArray(coordsArray, index), yFromArray(coordsArray, index));
return coords;
} }
public static void setXYInArray(final int[] coordsArray, final int index, public static void setXYInArray(@Nonnull final int[] coordsArray, final int index,
final int x, final int y) { final int x, final int y) {
final int baseIndex = ELEMENT_SIZE * index; final int baseIndex = ELEMENT_SIZE * index;
coordsArray[baseIndex + INDEX_X] = x; coordsArray[baseIndex + INDEX_X] = x;
coordsArray[baseIndex + INDEX_Y] = y; coordsArray[baseIndex + INDEX_Y] = y;
} }
public static void setCoordinateInArray(final int[] coordsArray, final int index, public static void setCoordinateInArray(@Nonnull final int[] coordsArray, final int index,
final int[] coords) { @Nonnull final int[] coords) {
final int baseIndex = ELEMENT_SIZE * index; setXYInArray(coordsArray, index, x(coords), y(coords));
coordsArray[baseIndex + INDEX_X] = coords[INDEX_X];
coordsArray[baseIndex + INDEX_Y] = coords[INDEX_Y];
} }
} }